array_matrix/matrix/
submatrix.rs

1use crate::matrix::matrix_init;
2
3use super::Matrix;
4
5pub trait Submatrix<O>: Matrix
6where
7    O: Matrix
8{
9    type Index;
10    /// Returns the submatrix of a given cell
11    /// 
12    /// Mᵢⱼ
13    /// 
14    /// # Arguments
15    /// 
16    /// * `index` - Row and/or collumn to neglect
17    /// 
18    /// # Examples
19    /// 
20    /// ```rust
21    /// let a = [
22    ///     [1.0, 2.0, 3.0],
23    ///     [4.0, 5.0, 6.0],
24    ///     [7.0, 8.0, 9.0]
25    /// ];
26    /// let s = [
27    ///     [5.0, 6.0],
28    ///     [8.0, 9.0]
29    /// ];
30    /// assert_eq!(a.submatrix((0, 0)), s);
31    /// ```
32    fn submatrix(&self, index: Self::Index) -> O;
33}
34
35
36impl<F: Clone, const L: usize, const H: usize> Submatrix<[[F; L - 1]; H]> for [[F; L]; H]
37where
38    Self: Matrix,
39    [[F; L - 1]; H]: Matrix
40{
41    type Index = usize;
42
43    fn submatrix(&self, r: usize) -> [[F; L - 1]; H]
44    {
45        matrix_init(|i, j| self[(i + r + 1)%H][j].clone())
46    }
47}
48
49impl<F: Clone, const L: usize, const H: usize> Submatrix<[[F; L]; H - 1]> for [[F; L]; H]
50where
51    Self: Matrix,
52    [[F; L]; H - 1]: Matrix
53{
54    type Index = usize;
55
56    fn submatrix(&self, c: usize) -> [[F; L]; H - 1]
57    {
58        matrix_init(|i, j| self[i][(j + c + 1)%L].clone())
59    }
60}
61
62impl<F: Clone, const L: usize, const H: usize> Submatrix<[[F; L - 1]; H - 1]> for [[F; L]; H]
63where
64    Self: Matrix,
65    [[F; L - 1]; H - 1]: Matrix
66{
67    type Index = (usize, usize);
68
69    fn submatrix(&self, rc: (usize, usize)) -> [[F; L - 1]; H - 1]
70    {
71        matrix_init(|i, j| self[(i + rc.0 + 1)%H][(j + rc.1 + 1)%L].clone())
72    }
73}