matrix_mul_test/
lib.rs

1pub mod tester {
2    use ndarray::{Array2, linalg};
3    use num::Complex;
4
5    pub fn test_mul(times: usize, size : usize, zeros_only : bool) {
6        let mut tot_time = 0;
7        println!("Running test_mul {} times", times);
8        for _ in 0..times {
9            let a = create_matrix(size, 1, zeros_only);
10            let b = create_matrix(size, size, zeros_only);
11            let sw = stopwatch::Stopwatch::start_new();
12            let _c = b.dot(&a);
13            let ms = sw.elapsed_ms();
14            tot_time += ms;
15            println!("Time: {} ms", ms);
16        }
17        let avg = tot_time / (times as i64);
18        println!("Ran {} times, average time: {} ms", times, avg);
19    }
20    pub fn test_kron(times: usize, size : usize, reg_size: usize, arity : usize) {
21        let mut tot_time = 0;
22        println!("Running test_kron {} times", times);
23        for _ in 0..times {
24            let a = create_matrix(size, size, false);
25            let sw = stopwatch::Stopwatch::start_new();
26            let _matrix = linalg::kron(&Array2::eye(1 << (reg_size- arity)), &a);
27            let ms = sw.elapsed_ms();
28            tot_time += ms;
29            println!("Time: {} ms", ms);
30        }
31        let avg = tot_time / (times as i64);
32        println!("Ran {} times, average time: {} ms", times, avg);
33    }
34    fn create_matrix(x: usize, y: usize, zeros_only : bool) -> Array2<Complex<f64>> {
35        let mut matrix = Array2::<Complex<f64>>::zeros((x, y));
36        if zeros_only {
37            return matrix;
38        }
39        for i in 0..x {
40            for j in 0..y {
41                matrix[[i, j]] = Complex::new(rand::random::<f64>(), rand::random::<f64>());
42            }
43        }
44        return matrix;
45    }
46}