argmin_math/primitives/
mul.rs

1// Copyright 2018-2024 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::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!(i8);
23make_mul!(u8);
24make_mul!(i16);
25make_mul!(u16);
26make_mul!(i32);
27make_mul!(u32);
28make_mul!(i64);
29make_mul!(u64);
30make_mul!(f32);
31make_mul!(f64);
32make_mul!(Complex<i8>);
33make_mul!(Complex<u8>);
34make_mul!(Complex<i16>);
35make_mul!(Complex<u16>);
36make_mul!(Complex<i32>);
37make_mul!(Complex<u32>);
38make_mul!(Complex<i64>);
39make_mul!(Complex<u64>);
40make_mul!(Complex<f32>);
41make_mul!(Complex<f64>);
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46    use approx::assert_relative_eq;
47    use paste::item;
48
49    macro_rules! make_test {
50        ($t:ty) => {
51            item! {
52                #[test]
53                fn [<test_mul_ $t>]() {
54                    let a = 2 as $t;
55                    let b = 21 as $t;
56                    let res = <$t as ArgminMul<$t, $t>>::mul(&a, &b);
57                    assert_relative_eq!(42 as f64, res as f64, epsilon = f64::EPSILON);
58                }
59            }
60        };
61    }
62
63    make_test!(i8);
64    make_test!(u8);
65    make_test!(i16);
66    make_test!(u16);
67    make_test!(i32);
68    make_test!(u32);
69    make_test!(i64);
70    make_test!(u64);
71    make_test!(f32);
72    make_test!(f64);
73}