argmin_math/vec/
conj.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::ArgminConj;
9use num_complex::Complex;
10
11macro_rules! make_conj {
12    ($t:ty) => {
13        impl ArgminConj for Vec<$t> {
14            #[inline]
15            fn conj(&self) -> Vec<$t> {
16                self.iter().map(|a| <$t as ArgminConj>::conj(a)).collect()
17            }
18        }
19
20        impl ArgminConj for Vec<Vec<$t>> {
21            #[inline]
22            fn conj(&self) -> Vec<Vec<$t>> {
23                self.iter()
24                    .map(|a| a.iter().map(|b| <$t as ArgminConj>::conj(b)).collect())
25                    .collect()
26            }
27        }
28    };
29}
30
31make_conj!(i8);
32make_conj!(i16);
33make_conj!(i32);
34make_conj!(i64);
35make_conj!(f32);
36make_conj!(f64);
37make_conj!(Complex<i8>);
38make_conj!(Complex<i16>);
39make_conj!(Complex<i32>);
40make_conj!(Complex<i64>);
41make_conj!(Complex<f32>);
42make_conj!(Complex<f64>);
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47    use paste::item;
48
49    macro_rules! make_test {
50        ($t:ty) => {
51            item! {
52                #[test]
53                fn [<test_conj_complex_vec_ $t>]() {
54                    let a = vec![
55                        Complex::new(1 as $t, 2 as $t),
56                        Complex::new(4 as $t, -3 as $t),
57                        Complex::new(8 as $t, 0 as $t)
58                    ];
59                    let b = vec![
60                        Complex::new(1 as $t, -2 as $t),
61                        Complex::new(4 as $t, 3 as $t),
62                        Complex::new(8 as $t, 0 as $t)
63                    ];
64                    let res = <Vec<Complex<$t>> as ArgminConj>::conj(&a);
65                    for i in 0..3 {
66                        let tmp = b[i] - res[i];
67                        let norm = ((tmp.re * tmp.re + tmp.im * tmp.im) as f64).sqrt();
68                        assert!(norm  < f64::EPSILON);
69                    }
70                }
71            }
72
73            item! {
74                #[test]
75                fn [<test_conj_vec_ $t>]() {
76                    let a = vec![1 as $t, 4 as $t, 8 as $t];
77                    let b = vec![1 as $t, 4 as $t, 8 as $t];
78                    let res = <Vec<$t> as ArgminConj>::conj(&a);
79                    for i in 0..3 {
80                        let diff = (b[i] as f64 - res[i] as f64).abs();
81                        assert!(diff  < f64::EPSILON);
82                    }
83                }
84            }
85
86            item! {
87                #[test]
88                fn [<test_conj_complex_vec_vec_ $t>]() {
89                    let a = vec![
90                        vec![
91                            Complex::new(1 as $t, 2 as $t),
92                            Complex::new(4 as $t, -3 as $t),
93                            Complex::new(8 as $t, 0 as $t)
94                        ],
95                        vec![
96                            Complex::new(1 as $t, -5 as $t),
97                            Complex::new(4 as $t, 6 as $t),
98                            Complex::new(8 as $t, 0 as $t)
99                        ],
100                    ];
101                    let b = vec![
102                        vec![
103                            Complex::new(1 as $t, -2 as $t),
104                            Complex::new(4 as $t, 3 as $t),
105                            Complex::new(8 as $t, 0 as $t)
106                        ],
107                        vec![
108                            Complex::new(1 as $t, 5 as $t),
109                            Complex::new(4 as $t, -6 as $t),
110                            Complex::new(8 as $t, 0 as $t)
111                        ],
112                    ];
113                    let res = <Vec<Vec<Complex<$t>>> as ArgminConj>::conj(&a);
114                    for i in 0..2 {
115                        for j in 0..3 {
116                            let tmp = b[i][j] - res[i][j];
117                            let norm = ((tmp.re * tmp.re + tmp.im * tmp.im) as f64).sqrt();
118                            assert!(norm  < f64::EPSILON);
119                        }
120                    }
121                }
122            }
123        };
124    }
125
126    make_test!(i8);
127    make_test!(i16);
128    make_test!(i32);
129    make_test!(i64);
130    make_test!(f32);
131    make_test!(f64);
132}