diff --git a/rtchallenge/src/matrices.rs b/rtchallenge/src/matrices.rs index d9ea7d3..73341ad 100644 --- a/rtchallenge/src/matrices.rs +++ b/rtchallenge/src/matrices.rs @@ -10,6 +10,7 @@ pub struct Matrix4x4 { m: [[f32; 4]; 4], } +#[derive(Debug, PartialEq)] pub struct Matrix2x2 { m: [[f32; 2]; 2], } @@ -42,6 +43,7 @@ impl Index<(usize, usize)> for Matrix2x2 { } } +#[derive(Debug, PartialEq)] pub struct Matrix3x3 { m: [[f32; 3]; 3], } @@ -50,6 +52,35 @@ impl Matrix3x3 { pub fn new(r0: [f32; 3], r1: [f32; 3], r2: [f32; 3]) -> Matrix3x3 { Matrix3x3 { m: [r0, r1, r2] } } + /// submatrix extracts a 2x2 matrix ignoring the 0-based `row` and `col` given. + /// + /// # Examples + /// ``` + /// use rtchallenge::matrices::{Matrix2x2, Matrix3x3}; + /// + /// assert_eq!( + /// Matrix3x3::new([1., 5., 0.], [-3., 2., 7.], [0., 6., -3.],).submatrix(0, 2), + /// Matrix2x2::new([-3., 2.], [0., 6.]) + /// ); + /// ``` + pub fn submatrix(&self, row: usize, col: usize) -> Matrix2x2 { + assert!(0 <= row && row < 3); + assert!(0 <= col && col < 3); + let mut rows = vec![]; + for r in 0..3 { + if r != row { + let mut v = vec![]; + for c in 0..3 { + if c != col { + v.push(self[(r, c)]); + } + } + rows.push(v); + } + } + let m = [[rows[0][0], rows[0][1]], [rows[1][0], rows[1][1]]]; + Matrix2x2 { m } + } } impl Index<(usize, usize)> for Matrix3x3 { type Output = f32;