tensor_macros/
lib.rs

1#![feature(try_from)]
2#![feature(test)]
3
4extern crate test;
5
6// #[macro_use]
7// pub mod functional;
8
9pub mod defaults;
10pub mod traits;
11
12#[macro_use]
13pub mod debug;
14
15#[macro_use]
16pub mod index;
17
18#[macro_use]
19pub mod tensor;
20
21#[macro_use]
22pub mod transpose;
23
24#[macro_use]
25pub mod dot;
26
27#[cfg(test)]
28mod tests {
29    use crate as tensor_macros;
30    tensor!(T243: 2 x 4 x 3);
31    tensor!(M43: 4 x 3 x 1);
32    tensor!(V2: 2 x 1);
33
34    dot!(T243: 2 x 4 x 3 * M43: 4 x 3 x 1 => V2: 2 x 1);
35
36    use test::{black_box, Bencher};
37    #[bench]
38    fn tensor_macros_dot_bench(b: &mut Bencher) {
39        let l = T243([
40            0.0f64, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0,
41            15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0,
42        ]);
43        let r = M43([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0]);
44
45        assert_eq!(l * r, V2([506.0, 1298.0]));
46
47        let mut o = V2::<f64>::new();
48
49        b.iter(|| {
50            for _i in 0..100 {
51                o += black_box(l * r);
52            }
53
54            assert_eq!(o, V2([100.0 * 506.0, 100.0 * 1298.0]));
55            o = V2::<f64>::new()
56        });
57
58        assert_eq!(o, V2([0.0, 0.0]));
59    }
60}