array_matrix/matrix/
sub.rs

1use std::ops::Sub;
2
3use crate::Matrix;
4
5use super::matrix_init;
6
7pub trait MSub<Rhs: Matrix>: Matrix
8where
9    Self::Output: Matrix
10{
11    type Output;
12
13    /// Subtracts one matrix from another of equal dimensions
14    /// 
15    /// A - B
16    /// 
17    /// # Arguments
18    /// 
19    /// * `rhs` - The subtracted matrix
20    /// 
21    /// # Examples
22    /// 
23    /// ```rust
24    /// let a = [
25    ///     [1.0, 2.0],
26    ///     [3.0, 4.0]
27    /// ];
28    /// let b = [
29    ///     [4.0, 3.0],
30    ///     [2.0, 1.0]
31    /// ];
32    /// let s = [
33    ///     [a[0][0] - b[0][0], a[0][1] - b[0][1]],
34    ///     [a[1][0] - b[1][0], a[1][1] - b[1][1]]
35    /// ];
36    /// assert_eq!(a.sub(b), s);
37    /// ```
38    fn sub(self, rhs: Rhs) -> Self::Output;
39}
40
41impl<T1, T2, const H: usize, const L: usize> MSub<[[T2; L]; H]> for [[T1; L]; H]
42where
43    Self: Matrix,
44    [[T2; L]; H]: Matrix,
45    [[<T1 as Sub<T2>>::Output; L]; H]: Matrix,
46    T1: Sub<T2> + Clone,
47    T2: Clone
48{
49    type Output = [[<T1 as Sub<T2>>::Output; L]; H];
50
51    fn sub(self, rhs: [[T2; L]; H]) -> Self::Output
52    {
53        matrix_init(|r, c| self[r][c].clone() - rhs[r][c].clone())
54    }
55}