diff --git a/matrix/src/lib.rs b/matrix/src/lib.rs index e4024d2..260d075 100644 --- a/matrix/src/lib.rs +++ b/matrix/src/lib.rs @@ -11,7 +11,7 @@ pub struct Matrix { } impl Matrix { - pub fn new() -> Matrix { + pub fn default() -> Self { Matrix { matrix: [[0f32; W]; H], } @@ -19,12 +19,14 @@ impl Matrix { pub fn from_array(matrix: [[f32; W]; H]) -> Matrix { Matrix { - matrix: matrix, + matrix, } } pub fn identity() -> Matrix { - let mut m = Matrix::new(); + // I can't figure out how to assign a 2d array to matrix inside the generic + // so I instead create the new and then assign 1.0 to the necessary values + let mut m = Self::default(); for i in 0..m.matrix.len() { m.matrix[i][i] = 1.0; } @@ -69,7 +71,7 @@ impl PartialEq for Matrix { } } } - return true; + true } } @@ -95,9 +97,9 @@ impl std::ops::Mul> for Matrix) -> Matrix { let mut result = [[0f32; W]; H]; - for row in 0..H { - for col in 0..W { - result[row][col] = self.calc_val_for_mul(row, &_rhs, col); + for (row, val) in result.iter_mut().enumerate().take(H) { + for (col, v) in val.iter_mut().enumerate().take(W) { + *v = self.calc_val_for_mul(row, &_rhs, col); } } Matrix::from_array(result)