peroxide/complex/
vector.rs1use crate::fuga::Algorithm;
2use crate::traits::fp::FPVector;
3use crate::traits::math::{InnerProduct, Norm, Normed, Vector};
4use crate::traits::sugar::VecOps;
5use num_complex::Complex;
6
7impl Vector for Complex<f64> {
8 type Scalar = Self;
9
10 fn add_vec(&self, rhs: &Self) -> Self {
11 self + rhs
12 }
13
14 fn sub_vec(&self, rhs: &Self) -> Self {
15 self - rhs
16 }
17
18 fn mul_scalar(&self, rhs: Self::Scalar) -> Self {
19 self * rhs
20 }
21}
22
23impl Normed for Complex<f64> {
24 type UnsignedScalar = f64;
25 fn norm(&self, kind: Norm) -> Self::UnsignedScalar {
26 match kind {
27 Norm::L1 => self.l1_norm(),
28 Norm::L2 => Complex::<f64>::norm(*self),
29 _ => unimplemented!(),
30 }
31 }
32
33 fn normalize(&self, kind: Norm) -> Self
34 where
35 Self: Sized,
36 {
37 let n = self.norm(kind);
38 self / n
39 }
40}
41
42impl InnerProduct for Complex<f64> {
43 fn dot(&self, rhs: &Self) -> Self::Scalar {
44 self.conj() * rhs
45 }
46}
47
48impl FPVector for Vec<Complex<f64>> {
49 type Scalar = Complex<f64>;
50
51 fn fmap<F>(&self, f: F) -> Self
52 where
53 F: Fn(Self::Scalar) -> Self::Scalar,
54 {
55 self.iter().map(|&x| f(x)).collect()
56 }
57
58 fn zip_with<F>(&self, f: F, other: &Self) -> Self
59 where
60 F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar,
61 {
62 self.iter()
63 .zip(other.iter())
64 .map(|(&x, &y)| f(x, y))
65 .collect()
66 }
67
68 fn reduce<F, T>(&self, init: T, f: F) -> Self::Scalar
69 where
70 F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar,
71 T: Into<Self::Scalar>,
72 {
73 self.iter().fold(init.into(), |x, &y| f(x, y))
74 }
75
76 fn filter<F>(&self, f: F) -> Self
77 where
78 F: Fn(Self::Scalar) -> bool,
79 {
80 self.iter().filter(|&x| f(*x)).cloned().collect()
81 }
82
83 fn take(&self, n: usize) -> Self {
84 self.iter().take(n).cloned().collect()
85 }
86
87 fn skip(&self, n: usize) -> Self {
88 self.iter().skip(n).cloned().collect()
89 }
90
91 fn sum(&self) -> Self::Scalar {
92 self.iter().sum()
93 }
94
95 fn prod(&self) -> Self::Scalar {
96 self.iter().product()
97 }
98}
99
100impl Vector for Vec<Complex<f64>> {
101 type Scalar = Complex<f64>;
102
103 fn add_vec(&self, rhs: &Self) -> Self {
104 self.zip_with(|x, y| x + y, rhs)
105 }
106
107 fn sub_vec(&self, rhs: &Self) -> Self {
108 self.zip_with(|x, y| x - y, rhs)
109 }
110
111 fn mul_scalar(&self, rhs: Self::Scalar) -> Self {
112 self.fmap(|x| x * rhs)
113 }
114}
115
116impl Normed for Vec<Complex<f64>> {
117 type UnsignedScalar = f64;
118
119 fn norm(&self, kind: Norm) -> Self::UnsignedScalar {
120 match kind {
121 Norm::L1 => self.iter().map(|x| Complex::<f64>::norm(*x).abs()).sum(),
122 _ => unimplemented!(),
123 }
124 }
125
126 fn normalize(&self, _kind: Norm) -> Self
127 where
128 Self: Sized,
129 {
130 unimplemented!()
131 }
132}
133
134impl InnerProduct for Vec<Complex<f64>> {
135 fn dot(&self, rhs: &Self) -> Self::Scalar {
136 self.zip_with(|x, y| x.conj() * y, rhs).sum()
137 }
138}
139
140impl VecOps for Vec<Complex<f64>> {}
141
142impl Algorithm for Vec<Complex<f64>> {
143 type Scalar = Complex<f64>;
144
145 fn rank(&self) -> Vec<usize> {
146 unimplemented!()
147 }
148
149 fn sign(&self) -> Complex<f64> {
150 unimplemented!()
151 }
152
153 fn arg_max(&self) -> usize {
154 unimplemented!()
155 }
156
157 fn arg_min(&self) -> usize {
158 unimplemented!()
159 }
160
161 fn max(&self) -> Complex<f64> {
162 unimplemented!()
163 }
164
165 fn min(&self) -> Complex<f64> {
166 unimplemented!()
167 }
168
169 fn swap_with_perm(&mut self, p: &Vec<(usize, usize)>) {
170 for (i, j) in p.iter() {
171 self.swap(*i, *j);
172 }
173 }
174}