peroxide/traits/
fp.rs

1/// Functional Programming tools for Vector
2pub trait FPVector {
3    type Scalar;
4
5    fn fmap<F>(&self, f: F) -> Self
6    where
7        F: Fn(Self::Scalar) -> Self::Scalar;
8    fn reduce<F, T>(&self, init: T, f: F) -> Self::Scalar
9    where
10        F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar,
11        T: Into<Self::Scalar> + Copy;
12    fn zip_with<F>(&self, f: F, other: &Self) -> Self
13    where
14        F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar;
15    fn filter<F>(&self, f: F) -> Self
16    where
17        F: Fn(Self::Scalar) -> bool;
18    fn take(&self, n: usize) -> Self;
19    fn skip(&self, n: usize) -> Self;
20    fn sum(&self) -> Self::Scalar;
21    fn prod(&self) -> Self::Scalar;
22}
23
24/// Functional Programming for Matrix and ComplexMatrix
25pub trait FPMatrix {
26    type Scalar;
27
28    fn take_row(&self, n: usize) -> Self;
29    fn take_col(&self, n: usize) -> Self;
30    fn skip_row(&self, n: usize) -> Self;
31    fn skip_col(&self, n: usize) -> Self;
32    fn fmap<F>(&self, f: F) -> Self
33    where
34        F: Fn(Self::Scalar) -> Self::Scalar;
35    fn col_map<F>(&self, f: F) -> Self
36    where
37        F: Fn(Vec<Self::Scalar>) -> Vec<Self::Scalar>;
38    fn row_map<F>(&self, f: F) -> Self
39    where
40        F: Fn(Vec<Self::Scalar>) -> Vec<Self::Scalar>;
41    fn col_mut_map<F>(&mut self, f: F)
42    where
43        F: Fn(Vec<Self::Scalar>) -> Vec<Self::Scalar>;
44    fn row_mut_map<F>(&mut self, f: F)
45    where
46        F: Fn(Vec<Self::Scalar>) -> Vec<Self::Scalar>;
47    fn reduce<F, T>(&self, init: T, f: F) -> Self::Scalar
48    where
49        F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar,
50        T: Into<Self::Scalar>;
51    fn zip_with<F>(&self, f: F, other: &Self) -> Self
52    where
53        F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar;
54    fn col_reduce<F>(&self, f: F) -> Vec<Self::Scalar>
55    where
56        F: Fn(Vec<Self::Scalar>) -> Self::Scalar;
57    fn row_reduce<F>(&self, f: F) -> Vec<Self::Scalar>
58    where
59        F: Fn(Vec<Self::Scalar>) -> Self::Scalar;
60}
61
62/// Functional Programming tools for Vector in Parallel (Uses Rayon crate)
63#[cfg(feature = "parallel")]
64pub trait ParallelFPVector {
65    type Scalar;
66
67    fn par_fmap<F>(&self, f: F) -> Self
68    where
69        F: Fn(Self::Scalar) -> Self::Scalar + Send + Sync;
70    fn par_reduce<F, T>(&self, init: T, f: F) -> Self::Scalar
71    where
72        F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar + Send + Sync,
73        T: Into<Self::Scalar> + Send + Sync + Copy;
74    fn par_zip_with<F>(&self, f: F, other: &Self) -> Self
75    where
76        F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar + Send + Sync;
77    fn par_filter<F>(&self, f: F) -> Self
78    where
79        F: Fn(Self::Scalar) -> bool + Send + Sync;
80}
81
82/// Functional Programming for Matrix in Parallel (Uses Rayon crate)
83#[cfg(feature = "parallel")]
84pub trait ParallelFPMatrix {
85    type Scalar;
86
87    fn par_fmap<F>(&self, f: F) -> Self
88    where
89        F: Fn(Self::Scalar) -> Self::Scalar + Send + Sync;
90    fn par_reduce<F, T>(&self, init: T, f: F) -> Self::Scalar
91    where
92        F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar + Send + Sync,
93        T: Into<Self::Scalar> + Copy + Clone + Send + Sync;
94    fn par_zip_with<F>(&self, f: F, other: &Self) -> Self
95    where
96        F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar + Send + Sync;
97    fn par_col_reduce<F>(&self, f: F) -> Vec<Self::Scalar>
98    where
99        F: Fn(Vec<Self::Scalar>) -> Self::Scalar + Send + Sync;
100    fn par_row_reduce<F>(&self, f: F) -> Vec<Self::Scalar>
101    where
102        F: Fn(Vec<Self::Scalar>) -> Self::Scalar + Send + Sync;
103}