matrix-basic/src/lib.rs

48 lines
1.6 KiB
Rust
Raw Normal View History

2023-05-25 20:20:31 -05:00
//! This is a crate for very basic matrix operations
//! with any type that supports addition, substraction,
//! and multiplication. Additional properties might be
//! needed for certain operations.
//!
//! Sayantan Santra (2023)
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-25 20:07:10 -05:00
#[test]
fn zero_one_test() {
let a = Matrix::from(vec![vec![0, 0, 0], vec![0, 0, 0]]).unwrap();
let b = Matrix::from(vec![vec![1, 0], vec![0, 1]]).unwrap();
assert_eq!(Matrix::<i32>::zero(2, 3), a);
assert_eq!(Matrix::<i32>::identity(2), b);
}
2023-05-24 21:46:25 -05:00
}