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