diff --git a/matrix/src/lib.rs b/matrix/src/lib.rs index 76ca8a1..1282632 100644 --- a/matrix/src/lib.rs +++ b/matrix/src/lib.rs @@ -67,9 +67,7 @@ impl Matrix { } } - pub fn minor(&self, row: usize, col: usize) -> f32 where - [(); H - 1]: , - [(); W - 1]: , + pub fn minor(&self, row: usize, col: usize) -> f32 { let t = self.zero_skip(row); let b = self.bound_skip(row, H - 1); @@ -84,6 +82,15 @@ impl Matrix { tl * br - tr * bl } + pub fn cofactor(&self, row: usize, col: usize) -> f32 { + self.minor(row, col) * if row + col % 1 == 0 { + 1.0 + } + else { + -1.0 + } + } + pub fn sub_matrix(&self, skip_row: usize, skip_col: usize) -> Matrix<{H - 1}, {W - 1}> { let mut idx_row: usize = 0; @@ -438,4 +445,18 @@ mod tests { assert_eq!(25.0, s.determinant()); assert_eq!(25.0, m.minor(1, 0)); } + + #[test] + fn cofactor_3x3() { + let m = Matrix::from_array([ + [3.0, 5.0, 0.0], + [2.0, -1.0, -7.0], + [6.0, -1.0, 5.0], + ]); + assert_eq!(-12.0, m.minor(0, 0)); + assert_eq!(-12.0, m.cofactor(0, 0)); + assert_eq!(25.0, m.minor(1, 0)); + assert_eq!(-25.0, m.cofactor(1, 0)); + + } }