todo: finish matrix mul

This commit is contained in:
Egor 2024-05-18 13:19:34 +03:00
parent 79d94bf357
commit 0397811247
3 changed files with 46 additions and 5 deletions

View file

@ -28,6 +28,14 @@ impl<T: Num + Clone> Matrix<T> {
pub fn new_zeroes(width: usize, height: usize) -> Self {
Self::new_filled(T::zero(), width, height)
}
pub fn row(&self, i: usize) -> Vec<T> {
self[i].to_vec()
}
pub fn column(&self, j: usize) -> Vec<T> {
todo!()
}
}
impl<T: Num> Matrix<T> {
@ -61,7 +69,11 @@ impl<T: Num> Matrix<T> {
}
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()
}
}

View file

@ -106,6 +106,21 @@ impl<T: NumAssign + Clone> DivAssign<T> for Matrix<T> {
}
}
impl<T: Num + Clone> Mul for Matrix<T> {
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() {
}
}

View file

@ -11,11 +11,11 @@ impl<T: Num + Clone> Matrix<T> {
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<T: Num + Clone> Matrix<T> {
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<T> {
self.data.iter_mut()
}