added cofactor
This commit is contained in:
@@ -67,9 +67,7 @@ impl<const H: usize, const W: usize> Matrix<H, W> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn minor(&self, row: usize, col: usize) -> f32 where
|
pub fn minor(&self, row: usize, col: usize) -> f32
|
||||||
[(); H - 1]: ,
|
|
||||||
[(); W - 1]: ,
|
|
||||||
{
|
{
|
||||||
let t = self.zero_skip(row);
|
let t = self.zero_skip(row);
|
||||||
let b = self.bound_skip(row, H - 1);
|
let b = self.bound_skip(row, H - 1);
|
||||||
@@ -84,6 +82,15 @@ impl<const H: usize, const W: usize> Matrix<H, W> {
|
|||||||
tl * br - tr * bl
|
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}>
|
pub fn sub_matrix(&self, skip_row: usize, skip_col: usize) -> Matrix<{H - 1}, {W - 1}>
|
||||||
{
|
{
|
||||||
let mut idx_row: usize = 0;
|
let mut idx_row: usize = 0;
|
||||||
@@ -438,4 +445,18 @@ mod tests {
|
|||||||
assert_eq!(25.0, s.determinant());
|
assert_eq!(25.0, s.determinant());
|
||||||
assert_eq!(25.0, m.minor(1, 0));
|
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));
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user