rapl/ops/
maps.rs

1use super::*;
2
3impl<T1: Clone, R: Unsigned> Ndarr<T1, R> {
4    pub fn map_in_place<F1: Fn(&T1) -> T1>(&mut self, f: F1) {
5        for val in self.data.iter_mut() {
6            *val = f(val)
7        }
8    }
9    pub fn map<T2: Clone + Debug, F2: Fn(&T1) -> T2>(&self, f: F2) -> Ndarr<T2, R> {
10        let out: Vec<T2> = self.data.iter().map(f).collect();
11        Ndarr {
12            data: out,
13            dim: self.dim.clone(),
14        }
15    }
16    // Bimap: is the same as Zip then map, is just a convenient way for doing diadic operations between Ndarrs
17    pub fn bimap<F: Fn(T1, T1) -> T1>(&self, other: &Self, f: F) -> Self
18    where
19        T1: Default,
20    {
21        let mut out = Vec::with_capacity(self.data.len());
22        for i in 0..self.data.len() {
23            out.push(f(self.data[i].clone(), other.data[i].clone()))
24        }
25        Ndarr {
26            data: out,
27            dim: self.dim.clone(),
28        }
29    }
30
31    pub fn bimap_in_place<F: Fn(T1, T1) -> T1>(&mut self, other: &Self, f: F) {
32        for i in 0..self.data.len() {
33            self.data[i] = f(self.data[i].clone(), other.data[i].clone())
34        }
35    }
36
37    pub fn scanr<F: Fn(T1, T1) -> T1>(&self, axis: usize, f: F) -> Self
38    where
39        T1: Default,
40        R: Sub<B1>,
41        <R as Sub<B1>>::Output: Unsigned,
42        <R as Sub<B1>>::Output: Add<B1>,
43        <<R as Sub<B1>>::Output as Add<B1>>::Output: Unsigned,
44    {
45        let mut slices = self.slice_at(axis);
46        for i in 0..slices.len() - 1 {
47            slices[i + 1] = slices[i + 1].bimap(&slices[i], &f)
48        }
49        let out = de_slice(&slices, axis);
50        Ndarr {
51            data: out.data,
52            dim: self.dim.clone(),
53        }
54    }
55
56    pub fn scanl<F: Fn(T1, T1) -> T1>(&self, axis: usize, f: F) -> Self
57    where
58        T1: Default,
59        R: Sub<B1>,
60        <R as Sub<B1>>::Output: Unsigned,
61        <R as Sub<B1>>::Output: Add<B1>,
62        <<R as Sub<B1>>::Output as Add<B1>>::Output: Unsigned,
63    {
64        let mut slices = self.slice_at(axis);
65        let l = slices.len();
66        for i in 0..slices.len() - 1 {
67            slices[l - 2 - i] = slices[l - 2 - i].bimap(&slices[l - 1 - i], &f)
68        }
69        let out = de_slice(&slices, axis);
70        Ndarr {
71            data: out.data,
72            dim: self.dim.clone(),
73        }
74    }
75}