From 039781124745174543c2627b4219e8ce91ab0929 Mon Sep 17 00:00:00 2001 From: erius Date: Sat, 18 May 2024 13:19:34 +0300 Subject: [PATCH] todo: finish matrix mul --- src/math/matrix.rs | 14 +++++++++++++- src/math/matrix/arithemtic.rs | 25 +++++++++++++++++++++++-- src/math/matrix/iter.rs | 12 ++++++++++-- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/math/matrix.rs b/src/math/matrix.rs index 6cfda15..65f0556 100644 --- a/src/math/matrix.rs +++ b/src/math/matrix.rs @@ -28,6 +28,14 @@ impl Matrix { pub fn new_zeroes(width: usize, height: usize) -> Self { Self::new_filled(T::zero(), width, height) } + + pub fn row(&self, i: usize) -> Vec { + self[i].to_vec() + } + + pub fn column(&self, j: usize) -> Vec { + todo!() + } } impl Matrix { @@ -61,7 +69,11 @@ impl Matrix { } pub fn same_size(&self, other: &Self) -> bool { - return self.width == other.width && self.height() == other.height() + self.width == other.width && self.height() == other.height() + } + + pub fn can_mul(&self, other: &Self) -> bool { + self.width == other.height() } } diff --git a/src/math/matrix/arithemtic.rs b/src/math/matrix/arithemtic.rs index 004cf60..cbecde3 100644 --- a/src/math/matrix/arithemtic.rs +++ b/src/math/matrix/arithemtic.rs @@ -106,6 +106,21 @@ impl DivAssign for Matrix { } } +impl Mul for Matrix { + type Output = Self; + fn mul(self, rhs: Self) -> Self::Output { + if !self.can_mul(&rhs) { + panic!("Unable to multiply matrices with sizes {}x{} and {}x{}", + self.width, self.height(), rhs.width, rhs.height()); + } + let mut new = Matrix::new_zeroes(rhs.width, self.height()); + for (i, j) in new.indices() { + todo!() + } + return new; + } +} + #[cfg(test)] mod tests { use super::*; @@ -158,7 +173,7 @@ mod tests { } #[test] - fn mul() { + fn mul_num() { let mut matrix = matrix![2; 1, 2, 3, 4]; matrix *= 2; assert_eq!(matrix, matrix![2; 2, 4, 6, 8]); @@ -166,11 +181,17 @@ mod tests { } #[test] - fn div() { + fn div_num() { let mut matrix = matrix![2; 2, 5, 8, 9]; matrix /= 2; assert_eq!(matrix, matrix![2; 1, 2, 4, 4]); assert_eq!(matrix / 4, matrix![2; 0, 0, 1, 1]); } + + #[test] + #[should_panic(expected = "Unable to multiply")] + fn mul() { + + } } diff --git a/src/math/matrix/iter.rs b/src/math/matrix/iter.rs index 186509c..079eff7 100644 --- a/src/math/matrix/iter.rs +++ b/src/math/matrix/iter.rs @@ -11,11 +11,11 @@ impl Matrix { self.data.into_iter() } - pub fn into_iter_indexed(self) { + pub fn into_iter_indexed(self) -> () { todo!() } - pub fn into_iter_rows(self) { + pub fn into_iter_rows(self) -> () { todo!() } @@ -31,6 +31,14 @@ impl Matrix { self.data.chunks(self.width) } + pub fn iter_column(&self) -> () { + todo!() + } + + pub fn iter_columns(&self) -> () { + todo!() + } + pub fn iter_mut(&mut self) -> std::slice::IterMut { self.data.iter_mut() }