mini_matrix/
utility.rs

1use crate::Matrix;
2use num::Num;
3use std::ops::{AddAssign, Mul, Neg};
4
5impl<T, const M: usize, const N: usize> Matrix<T, M, N>
6where
7    T: Copy + Default + Mul + Num + Neg<Output = T> + AddAssign + PartialEq,
8{
9    #[allow(dead_code)]
10    fn cofactor3x3(&self, row: usize, col: usize) -> Matrix<T, 3, 3> {
11        let mut cofactor_matrix = Matrix::<T, 3, 3>::zero();
12        let mut row_index = 0;
13
14        for r in 0..row {
15            if r == row {
16                continue;
17            }
18            let mut col_index = 0;
19            for c in 0..col {
20                if c == col {
21                    continue;
22                }
23                cofactor_matrix[(row_index, col_index)] = self[(r, c)];
24                col_index += 1;
25            }
26            row_index += 1;
27        }
28        cofactor_matrix
29    }
30
31    pub fn cofactor2x2(&self, row: usize, col: usize) -> Matrix<T, 2, 2> {
32        let mut cofactor_matrix = Matrix::<T, 2, 2>::zero();
33        let mut row_index = 0;
34
35        for r in 0..M {
36            if r == row {
37                continue;
38            }
39            let mut col_index = 0;
40            for c in 0..N {
41                if c == col {
42                    continue;
43                }
44                cofactor_matrix[(row_index, col_index)] = self[(r, c)];
45                col_index += 1;
46            }
47            row_index += 1;
48        }
49        cofactor_matrix
50    }
51
52    pub fn cofactor1x1(&self, row: usize, col: usize) -> Matrix<T, 1, 1> {
53        let mut cofactor_matrix = Matrix::<T, 1, 1>::zero();
54        let mut row_index = 0;
55
56        for r in 0..M {
57            if r == row {
58                continue;
59            }
60            let mut col_index = 0;
61            for c in 0..N {
62                if c == col {
63                    continue;
64                }
65                cofactor_matrix[(row_index, col_index)] = self[(r, c)];
66                col_index += 1;
67            }
68            row_index += 1;
69        }
70        cofactor_matrix
71    }
72}