-
Notifications
You must be signed in to change notification settings - Fork 32
/
fp.rs
103 lines (96 loc) · 3.47 KB
/
fp.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
/// Functional Programming tools for Vector
pub trait FPVector {
type Scalar;
fn fmap<F>(&self, f: F) -> Self
where
F: Fn(Self::Scalar) -> Self::Scalar;
fn reduce<F, T>(&self, init: T, f: F) -> Self::Scalar
where
F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar,
T: Into<Self::Scalar> + Copy;
fn zip_with<F>(&self, f: F, other: &Self) -> Self
where
F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar;
fn filter<F>(&self, f: F) -> Self
where
F: Fn(Self::Scalar) -> bool;
fn take(&self, n: usize) -> Self;
fn skip(&self, n: usize) -> Self;
fn sum(&self) -> Self::Scalar;
fn prod(&self) -> Self::Scalar;
}
/// Functional Programming for Matrix and ComplexMatrix
pub trait FPMatrix {
type Scalar;
fn take_row(&self, n: usize) -> Self;
fn take_col(&self, n: usize) -> Self;
fn skip_row(&self, n: usize) -> Self;
fn skip_col(&self, n: usize) -> Self;
fn fmap<F>(&self, f: F) -> Self
where
F: Fn(Self::Scalar) -> Self::Scalar;
fn col_map<F>(&self, f: F) -> Self
where
F: Fn(Vec<Self::Scalar>) -> Vec<Self::Scalar>;
fn row_map<F>(&self, f: F) -> Self
where
F: Fn(Vec<Self::Scalar>) -> Vec<Self::Scalar>;
fn col_mut_map<F>(&mut self, f: F)
where
F: Fn(Vec<Self::Scalar>) -> Vec<Self::Scalar>;
fn row_mut_map<F>(&mut self, f: F)
where
F: Fn(Vec<Self::Scalar>) -> Vec<Self::Scalar>;
fn reduce<F, T>(&self, init: T, f: F) -> Self::Scalar
where
F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar,
T: Into<Self::Scalar>;
fn zip_with<F>(&self, f: F, other: &Self) -> Self
where
F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar;
fn col_reduce<F>(&self, f: F) -> Vec<Self::Scalar>
where
F: Fn(Vec<Self::Scalar>) -> Self::Scalar;
fn row_reduce<F>(&self, f: F) -> Vec<Self::Scalar>
where
F: Fn(Vec<Self::Scalar>) -> Self::Scalar;
}
/// Functional Programming tools for Vector in Parallel (Uses Rayon crate)
#[cfg(feature = "parallel")]
pub trait ParallelFPVector {
type Scalar;
fn par_fmap<F>(&self, f: F) -> Self
where
F: Fn(Self::Scalar) -> Self::Scalar + Send + Sync;
fn par_reduce<F, T>(&self, init: T, f: F) -> Self::Scalar
where
F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar + Send + Sync,
T: Into<Self::Scalar> + Send + Sync + Copy;
fn par_zip_with<F>(&self, f: F, other: &Self) -> Self
where
F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar + Send + Sync;
fn par_filter<F>(&self, f: F) -> Self
where
F: Fn(Self::Scalar) -> bool + Send + Sync;
}
/// Functional Programming for Matrix in Parallel (Uses Rayon crate)
#[cfg(feature = "parallel")]
pub trait ParallelFPMatrix {
type Scalar;
fn par_fmap<F>(&self, f: F) -> Self
where
F: Fn(Self::Scalar) -> Self::Scalar + Send + Sync;
fn par_reduce<F, T>(&self, init: T, f: F) -> Self::Scalar
where
F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar + Send + Sync,
T: Into<Self::Scalar> + Copy + Clone + Send + Sync;
fn par_zip_with<F>(&self, f: F, other: &Self) -> Self
where
F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar + Send + Sync;
fn par_col_reduce<F>(&self, f: F) -> Vec<Self::Scalar>
where
F: Fn(Vec<Self::Scalar>) -> Self::Scalar + Send + Sync;
fn par_row_reduce<F>(&self, f: F) -> Vec<Self::Scalar>
where
F: Fn(Vec<Self::Scalar>) -> Self::Scalar + Send + Sync;
}