concision_core/ops/
reshape.rs

1/*
2    Appellation: reshape <module>
3    Contrib: @FL03
4*/
5use ndarray::{ArrayBase, Axis, Dimension, RawData, RawDataClone, RemoveAxis};
6
7/// This trait enables an array to remove an axis from itself
8pub trait DecrementAxis {
9    type Output;
10
11    fn dec(&self) -> Self::Output;
12}
13
14pub trait IncrementAxis {
15    type Output;
16
17    fn inc(&self) -> Self::Output;
18}
19
20pub trait Unsqueeze {
21    type Output;
22
23    fn unsqueeze(self, axis: usize) -> Self::Output;
24}
25/*
26 ************* Implementations *************
27*/
28impl<D> DecrementAxis for D
29where
30    D: RemoveAxis,
31{
32    type Output = D::Smaller;
33
34    fn dec(&self) -> Self::Output {
35        self.remove_axis(Axis(self.ndim() - 1))
36    }
37}
38
39impl<A, S, D> Unsqueeze for ArrayBase<S, D>
40where
41    D: Dimension,
42    S: RawData<Elem = A>,
43{
44    type Output = ArrayBase<S, D::Larger>;
45
46    fn unsqueeze(self, axis: usize) -> Self::Output {
47        self.insert_axis(Axis(axis))
48    }
49}
50
51impl<'a, A, S, D> Unsqueeze for &'a ArrayBase<S, D>
52where
53    D: Dimension,
54    S: RawDataClone<Elem = A>,
55{
56    type Output = ArrayBase<S, D::Larger>;
57
58    fn unsqueeze(self, axis: usize) -> Self::Output {
59        self.clone().insert_axis(Axis(axis))
60    }
61}