argmin/core/math/
dot.rs

1// Copyright 2018-2020 argmin developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use crate::core::math::ArgminDot;
9use num_complex::Complex;
10
11macro_rules! make_dot_vec {
12    ($t:ty) => {
13        impl<'a> ArgminDot<$t, $t> for $t {
14            #[inline]
15            fn dot(&self, other: &$t) -> $t {
16                self * other
17            }
18        }
19    };
20}
21
22make_dot_vec!(f32);
23make_dot_vec!(f64);
24make_dot_vec!(i8);
25make_dot_vec!(i16);
26make_dot_vec!(i32);
27make_dot_vec!(i64);
28make_dot_vec!(u8);
29make_dot_vec!(u16);
30make_dot_vec!(u32);
31make_dot_vec!(u64);
32make_dot_vec!(isize);
33make_dot_vec!(usize);
34make_dot_vec!(Complex<f32>);
35make_dot_vec!(Complex<f64>);
36make_dot_vec!(Complex<i8>);
37make_dot_vec!(Complex<i16>);
38make_dot_vec!(Complex<i32>);
39make_dot_vec!(Complex<i64>);
40make_dot_vec!(Complex<u8>);
41make_dot_vec!(Complex<u16>);
42make_dot_vec!(Complex<u32>);
43make_dot_vec!(Complex<u64>);
44make_dot_vec!(Complex<isize>);
45make_dot_vec!(Complex<usize>);
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use paste::item;
51
52    macro_rules! make_test {
53        ($t:ty) => {
54            item! {
55                #[test]
56                fn [<test_vec_vec_ $t>]() {
57                    let a = 21 as $t;
58                    let b = 2 as $t;
59                    let res = a.dot(&b);
60                    assert!((((res - 42 as $t) as f64).abs()) < std::f64::EPSILON);
61                }
62            }
63        };
64    }
65
66    make_test!(isize);
67    make_test!(usize);
68    make_test!(i8);
69    make_test!(u8);
70    make_test!(i16);
71    make_test!(u16);
72    make_test!(i32);
73    make_test!(u32);
74    make_test!(i64);
75    make_test!(u64);
76    make_test!(f32);
77    make_test!(f64);
78}