test: tests for mult

This commit is contained in:
2026-04-29 17:34:26 -06:00
parent 91820b4a73
commit 9b61d89fc4

View File

@@ -522,7 +522,7 @@ impl Mul for Matrix {
let current_column = other.get_column(k)?;
let current_row = self.get_row(i)?;
let mut new_value = Fraction::new(0, 1).unwrap();
let mut new_value = Fraction::from(0);
for (a, b) in current_row.zip(current_column) {
new_value = new_value + (*a * *b);
@@ -3397,4 +3397,192 @@ mod tests {
assert_eq!(result.data, expected);
}
#[test]
fn test_mul_basic_2x2() {
let a = Matrix {
rows: 2,
columns: 2,
data: vec![
Fraction::from(1),
Fraction::from(2),
Fraction::from(3),
Fraction::from(4),
],
};
let b = Matrix {
rows: 2,
columns: 2,
data: vec![
Fraction::from(5),
Fraction::from(6),
Fraction::from(7),
Fraction::from(8),
],
};
let result = (a * b).unwrap();
let expected = vec![
Fraction::from(19),
Fraction::from(22),
Fraction::from(43),
Fraction::from(50),
];
assert_eq!(result.data, expected);
}
#[test]
fn test_mul_identity() {
let a = Matrix {
rows: 2,
columns: 2,
data: vec![
Fraction::from(3),
Fraction::from(4),
Fraction::from(5),
Fraction::from(6),
],
};
let identity = Matrix {
rows: 2,
columns: 2,
data: vec![
Fraction::from(1),
Fraction::from(0),
Fraction::from(0),
Fraction::from(1),
],
};
let result = (a.clone() * identity).unwrap();
assert_eq!(result.data, a.data);
}
#[test]
fn test_mul_not_commutative() {
let a = Matrix {
rows: 2,
columns: 2,
data: vec![
Fraction::from(1),
Fraction::from(2),
Fraction::from(3),
Fraction::from(4),
],
};
let b = Matrix {
rows: 2,
columns: 2,
data: vec![
Fraction::from(2),
Fraction::from(0),
Fraction::from(1),
Fraction::from(2),
],
};
let res1 = (a.clone() * b.clone()).unwrap();
let res2 = (b * a).unwrap();
assert_ne!(res1.data, res2.data);
}
#[test]
fn test_mul_rectangular() {
// 2x3 * 3x2
let a = Matrix {
rows: 2,
columns: 3,
data: vec![
Fraction::from(1),
Fraction::from(2),
Fraction::from(3),
Fraction::from(4),
Fraction::from(5),
Fraction::from(6),
],
};
let b = Matrix {
rows: 3,
columns: 2,
data: vec![
Fraction::from(7),
Fraction::from(8),
Fraction::from(9),
Fraction::from(10),
Fraction::from(11),
Fraction::from(12),
],
};
let result = (a * b).unwrap();
let expected = vec![
Fraction::from(58),
Fraction::from(64),
Fraction::from(139),
Fraction::from(154),
];
assert_eq!(result.data, expected);
}
#[test]
fn test_mul_invalid_size() {
let a = Matrix::new(2, 3, Fraction::from(1)).unwrap();
let b = Matrix::new(2, 2, Fraction::from(1)).unwrap();
let result = a * b;
assert!(matches!(result, Err(MatrixError::InvalidSizeForMul)));
}
#[test]
fn test_mul_zero_matrix() {
let a = Matrix::new(2, 2, Fraction::from(5)).unwrap();
let zero = Matrix::new(2, 2, Fraction::from(0)).unwrap();
let result = (a * zero).unwrap();
assert!(result.data.iter().all(|x| x.is_zero()));
}
#[test]
fn test_mul_fractions() {
let a = Matrix {
rows: 1,
columns: 2,
data: vec![Fraction::new(1, 2).unwrap(), Fraction::new(1, 3).unwrap()],
};
let b = Matrix {
rows: 2,
columns: 1,
data: vec![Fraction::new(2, 1).unwrap(), Fraction::new(3, 1).unwrap()],
};
let result = (a * b).unwrap();
// (1/2 * 2) + (1/3 * 3) = 1 + 1 = 2
assert_eq!(result.data, vec![Fraction::from(2)]);
}
#[test]
fn test_mul_associative() {
let a = Matrix::new(2, 2, Fraction::from(1)).unwrap();
let b = Matrix::new(2, 2, Fraction::from(2)).unwrap();
let c = Matrix::new(2, 2, Fraction::from(3)).unwrap();
let res1 = ((a.clone() * b.clone()).unwrap() * c.clone()).unwrap();
let res2 = (a * (b * c).unwrap()).unwrap();
assert_eq!(res1.data, res2.data);
}
}