array_matrix/matrix/
mul.rs1use std::ops::{Mul, Add};
2
3use crate::{matrix_init, Matrix};
4
5pub trait MMul<Rhs>: Matrix
6where
7 Self::Output: Matrix
8{
9 type Output;
10
11 fn mul(self, rhs: Rhs) -> Self::Output;
51}
52
53impl<F, const L: usize, const H: usize> MMul<F> for [[F; L]; H]
54where
55 Self: Matrix,
56 [[<F as Mul<F>>::Output; L]; H]: Matrix,
57 F: Clone + Mul<F>
58{
59 type Output = [[<F as Mul<F>>::Output; L]; H];
60 fn mul(self, rhs: F) -> Self::Output
61 {
62 matrix_init(|r, c| self[r][c].clone()*rhs.clone())
63 }
64}
65
66impl<F, const L: usize, const H1: usize, const H2: usize> MMul<[[F; H2]; L]>
67for
68 [[F; L]; H1]
69where
70 Self: Matrix,
71 [[<F as Mul<F>>::Output; H2]; H1]: Matrix,
72 F: Clone + Mul<F>,
73 <F as Mul<F>>::Output: Add<<F as Mul<F>>::Output, Output = <F as Mul<F>>::Output>
74{
75 type Output = [[<F as Mul<F>>::Output; H2]; H1];
76 fn mul(self, rhs: [[F; H2]; L]) -> Self::Output
77 {
78 matrix_init(|r, c| (0..L).map(|i| self[r][i].clone()*rhs[i][c].clone()).reduce(|a, b| a + b).unwrap())
79 }
80}