argmin/core/math/
conj.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::ArgminConj;
9use num_complex::Complex;
10
11macro_rules! make_conj {
12    ($t:ty) => {
13        impl ArgminConj for $t {
14            #[inline]
15            fn conj(&self) -> $t {
16                *self
17            }
18        }
19    };
20}
21
22macro_rules! make_complex_conj {
23    ($t:ty) => {
24        impl ArgminConj for $t {
25            #[inline]
26            fn conj(&self) -> $t {
27                Complex::conj(self)
28            }
29        }
30    };
31}
32
33make_conj!(isize);
34make_conj!(usize);
35make_conj!(i8);
36make_conj!(i16);
37make_conj!(i32);
38make_conj!(i64);
39make_conj!(u8);
40make_conj!(u16);
41make_conj!(u32);
42make_conj!(u64);
43make_conj!(f32);
44make_conj!(f64);
45make_complex_conj!(Complex<isize>);
46make_complex_conj!(Complex<i8>);
47make_complex_conj!(Complex<i16>);
48make_complex_conj!(Complex<i32>);
49make_complex_conj!(Complex<i64>);
50make_complex_conj!(Complex<f32>);
51make_complex_conj!(Complex<f64>);
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56    use paste::item;
57
58    macro_rules! make_test_complex {
59        ($t:ty) => {
60            item! {
61                #[test]
62                fn [<test_complex_conj_ $t>]() {
63                    let a = 8 as $t;
64                    let b = 34 as $t;
65                    let res = <Complex<$t> as ArgminConj>::conj(&Complex::new(a, b));
66                    assert!(((a as $t - res.re) as f64).abs() < std::f64::EPSILON);
67                    assert!(((-b as $t - res.im) as f64).abs() < std::f64::EPSILON);
68                }
69            }
70        };
71    }
72
73    macro_rules! make_test {
74        ($t:ty) => {
75            item! {
76                #[test]
77                fn [<test_conj_ $t>]() {
78                    let a = 8 as $t;
79                    let res = <$t as ArgminConj>::conj(&a);
80                    assert!(((a as $t - res) as f64).abs() < std::f64::EPSILON);
81                }
82            }
83        };
84    }
85
86    make_test_complex!(isize);
87    make_test_complex!(i8);
88    make_test_complex!(i16);
89    make_test_complex!(i32);
90    make_test_complex!(i64);
91    make_test_complex!(f32);
92    make_test_complex!(f64);
93    make_test!(isize);
94    make_test!(usize);
95    make_test!(i8);
96    make_test!(u8);
97    make_test!(i16);
98    make_test!(u16);
99    make_test!(i32);
100    make_test!(u32);
101    make_test!(i64);
102    make_test!(u64);
103    make_test!(f32);
104    make_test!(f64);
105}