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