argmin/core/math/
mul.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::ArgminMul;
9use num_complex::Complex;
10
11macro_rules! make_mul {
12    ($t:ty) => {
13        impl ArgminMul<$t, $t> for $t {
14            #[inline]
15            fn mul(&self, other: &$t) -> $t {
16                self * other
17            }
18        }
19    };
20}
21
22make_mul!(isize);
23make_mul!(usize);
24make_mul!(i8);
25make_mul!(u8);
26make_mul!(i16);
27make_mul!(u16);
28make_mul!(i32);
29make_mul!(u32);
30make_mul!(i64);
31make_mul!(u64);
32make_mul!(f32);
33make_mul!(f64);
34make_mul!(Complex<isize>);
35make_mul!(Complex<usize>);
36make_mul!(Complex<i8>);
37make_mul!(Complex<u8>);
38make_mul!(Complex<i16>);
39make_mul!(Complex<u16>);
40make_mul!(Complex<i32>);
41make_mul!(Complex<u32>);
42make_mul!(Complex<i64>);
43make_mul!(Complex<u64>);
44make_mul!(Complex<f32>);
45make_mul!(Complex<f64>);
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_mul_ $t>]() {
57                    let a = 2 as $t;
58                    let b = 21 as $t;
59                    let res = <$t as ArgminMul<$t, $t>>::mul(&a, &b);
60                    assert!(((42 as $t - res) 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}