dntk_matrix/
multi_dim.rs

1/// Matrix [[N x M] x L]
2#[derive(Clone, Debug, PartialEq)]
3pub struct MultiMatrix<const N: usize, const M: usize, const L: usize, T>(pub [T; N * M * L])
4where
5    [T; N * M * L]:;
6
7impl<const N: usize, const M: usize, const L: usize, T> std::ops::Add for MultiMatrix<N, M, L, T>
8where
9    T: std::ops::AddAssign + Copy,
10    [T; N * M * L]:,
11{
12    type Output = Self;
13    fn add(self, other: Self) -> Self::Output {
14        let mut new_matrix = self.0;
15        for (l, r) in new_matrix.iter_mut().zip(other.0.into_iter()) {
16            *l += r
17        }
18        Self(new_matrix)
19    }
20}
21
22impl<const N: usize, const M: usize, const L: usize, T> std::fmt::Display
23    for MultiMatrix<N, M, L, T>
24where
25    T: std::fmt::Display + Copy + num::traits::Zero + PartialOrd,
26    [T; N * M * L]:,
27{
28    fn fmt(&self, dest: &mut std::fmt::Formatter) -> std::fmt::Result {
29        let mut string = "[[ ".to_string();
30        for k in 0..L {
31            if k != 0 {
32                string = format!("{} [ ", string);
33            }
34            for i in 0..N {
35                if i != 0 {
36                    string = format!("{}   ", string);
37                }
38                for j in 0..M {
39                    let pad = if self.0[i * M + j + k * N * M] >= T::zero() {
40                        " ".to_string()
41                    } else {
42                        "".to_string()
43                    };
44                    string = format!(
45                        "{}{}{} ",
46                        string,
47                        pad,
48                        self.0[i * M + j + k * N * M].clone()
49                    );
50                }
51                if i != N - 1 {
52                    string = format!("{}\n", string);
53                } else if k != L - 1 {
54                    string = format!("{}]\n", string);
55                } else {
56                    string = format!("{}]]", string);
57                }
58            }
59        }
60        write!(dest, "{}", string)
61    }
62}