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