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
#[derive(Clone, Debug, PartialEq)]
pub struct MultiMatrix<const N: usize, const M: usize, const L: usize, T>(pub [T; N * M * L])
where
[T; N * M * L]:;
impl<const N: usize, const M: usize, const L: usize, T> std::ops::Add for MultiMatrix<N, M, L, T>
where
T: std::ops::AddAssign + Copy,
[T; N * M * L]:,
{
type Output = Self;
fn add(self, other: Self) -> Self::Output {
let mut new_matrix = self.0;
for (l, r) in new_matrix.iter_mut().zip(other.0.into_iter()) {
*l += r
}
Self(new_matrix)
}
}
impl<const N: usize, const M: usize, const L: usize, T> std::fmt::Display
for MultiMatrix<N, M, L, T>
where
T: std::fmt::Display + Copy + num::traits::Zero + PartialOrd,
[T; N * M * L]:,
{
fn fmt(&self, dest: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut string = "[[ ".to_string();
for k in 0..L {
if k != 0 {
string = format!("{} [ ", string);
}
for i in 0..N {
if i != 0 {
string = format!("{} ", string);
}
for j in 0..M {
let pad = if self.0[i * M + j + k * N * M] >= T::zero() {
" ".to_string()
} else {
"".to_string()
};
string = format!(
"{}{}{} ",
string,
pad,
self.0[i * M + j + k * N * M].clone()
);
}
if i != N - 1 {
string = format!("{}\n", string);
} else if k != L - 1 {
string = format!("{}]\n", string);
} else {
string = format!("{}]]", string);
}
}
}
write!(dest, "{}", string)
}
}