matrix-basic/src/lib.rs

33 lines
1.1 KiB
Rust
Raw Normal View History

2023-05-24 21:46:25 -05:00
pub mod matrix;
#[cfg(test)]
mod tests {
use super::*;
use matrix::Matrix;
#[test]
fn mul_test() {
let a = Matrix::from(vec![vec![1, 2, 4], vec![3, 4, 9]]).unwrap();
let b = Matrix::from(vec![vec![1, 2], vec![2, 3], vec![5, 1]]).unwrap();
let c = Matrix::from(vec![vec![25, 12], vec![56, 27]]).unwrap();
assert_eq!(a * b, c);
}
#[test]
2023-05-24 23:39:46 -05:00
fn add_sub_test() {
2023-05-24 21:46:25 -05:00
let a = Matrix::from(vec![vec![1, 2, 3], vec![0, 1, 2]]).unwrap();
let b = Matrix::from(vec![vec![0, 0, 1], vec![2, 1, 3]]).unwrap();
let c = Matrix::from(vec![vec![1, 2, 4], vec![2, 2, 5]]).unwrap();
2023-05-24 23:39:46 -05:00
let d = Matrix::from(vec![vec![1, 2, 2], vec![-2, 0, -1]]).unwrap();
assert_eq!(a.clone() + b.clone(), c);
assert_eq!(a - b, d);
2023-05-24 21:46:25 -05:00
}
2023-05-24 22:50:52 -05:00
#[test]
fn det_test() {
let a = Matrix::from(vec![vec![1, 2, 0], vec![0, 3, 5], vec![0, 0, 10]]).unwrap();
let b = Matrix::from(vec![vec![1, 2, 0], vec![0, 3, 5]]).unwrap();
assert_eq!(a.det(), Ok(30));
assert!(b.det().is_err());
}
2023-05-24 21:46:25 -05:00
}