argmin_math/primitives/
sub.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::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!(i8);
23make_sub!(i16);
24make_sub!(i32);
25make_sub!(i64);
26make_sub!(u8);
27make_sub!(u16);
28make_sub!(u32);
29make_sub!(u64);
30make_sub!(f32);
31make_sub!(f64);
32make_sub!(Complex<i8>);
33make_sub!(Complex<i16>);
34make_sub!(Complex<i32>);
35make_sub!(Complex<i64>);
36make_sub!(Complex<u8>);
37make_sub!(Complex<u16>);
38make_sub!(Complex<u32>);
39make_sub!(Complex<u64>);
40make_sub!(Complex<f32>);
41make_sub!(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_sub_ $t>]() {
54                    let a = 50 as $t;
55                    let b = 8 as $t;
56                    let res = <$t as ArgminSub<$t, $t>>::sub(&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}