pub mod element;
pub mod error;
mod initial;
pub mod operation;
mod shape;
mod util;
pub use element::NumericElement;
use std::fmt::{Debug, Display, Formatter};
use std::ops::{Add, Mul};
#[derive(Clone, Copy)]
pub struct Matrix<const ROWS: usize, const COLS: usize, T>
where
T: NumericElement,
{
data: [[T;COLS];ROWS],
}
impl<const ROWS: usize, const COLS: usize, T> Matrix<ROWS, COLS, T>
where
T: NumericElement + Display,
{
pub fn new(data:[[T;COLS];ROWS]) -> Self {
const { assert!(ROWS > 0 && COLS > 0, "Matrix 的行数和列数都必须大于 0"); }
Matrix { data }
}
}
impl<const ROWS: usize, const COLS: usize, T> Matrix<ROWS, COLS, T>
where
T: NumericElement + Display,
{
fn display(&self) -> String {
let mut digits: u8 = 0;
for value in self.data.iter() {
let list = *value;
for f in list.iter() {
let d = f.to_string().len() as u8;
if d > digits {
digits = d
}
}
}
if ROWS == 1 {
let line = util::print_single_line(&self.data, digits);
return line;
}
let mut result = String::new();
for row in 0..ROWS {
let mut line = String::new();
let (start_char, end_char) = util::get_boundary_char(row, ROWS);
line.push_str(&start_char);
let start = row * COLS;
let end = (row + 1) * COLS;
for i in start..end {
let value = self.data[row][i - start];
let str = format!("{:<digit$}", value, digit = (digits + 2) as usize);
line.push_str(&str);
}
line.push_str(&end_char);
line.push('\n');
result.push_str(&line);
}
result
}
}
impl<const ROWS: usize, const COLS: usize, T> Matrix<ROWS, COLS, T>
where
T: NumericElement,
{
pub fn get(&self, x: usize, y: usize) -> Option<T> {
if x >= COLS || y >= ROWS {
return None;
}
Some(self.data[y][x])
}
pub fn height(&self) -> usize {
ROWS
}
pub fn width(&self) -> usize {
COLS
}
pub fn size(&self) -> (usize, usize) {
(ROWS, COLS)
}
pub fn set(&mut self, x: usize, y: usize, value: T) -> Result<(), error::OperationError> {
if x >= COLS || y >= ROWS {
return Err(error::OperationError {
message: "index out of bounds".to_string(),
});
}
self.data[y][x] = value;
Ok(())
}
}
impl<const N: usize, T> Matrix<N, N, T>
where
T: NumericElement + Display + Default + Add<Output = T> + From<i8> + Mul<Output = T>,
{
pub fn det(&self) -> Result<T, error::OperationError> {
let mut sum = T::default();
let permutation = util::permutation(N)?;
for perm in permutation {
let res = util::determinant_in_one_permutation(&self.data, &perm);
match res {
Ok(x) => sum = sum + x,
Err(e) => return Err(e),
}
}
Ok(sum)
}
}
impl<const N: usize> Matrix<N, N, f64> {
pub fn solve(&self, b: [f64; N]) -> Result<[f64; N], error::OperationError> {
let (lu, perm) = self.lu_decompose()?;
Ok(Self::lu_solve(&lu.data, &perm, b))
}
pub fn lu_decompose(&self) -> Result<(Self, [usize; N]), error::OperationError> {
let mut a = self.data;
let mut perm = [0usize; N];
for (i, p) in perm.iter_mut().enumerate() {
*p = i;
}
let tol = 1e-12 * (N as f64);
for k in 0..N {
let mut pivot = k;
let mut max_val = a[k][k].abs();
for i in (k + 1)..N {
if a[i][k].abs() > max_val {
max_val = a[i][k].abs();
pivot = i;
}
}
if max_val <= tol {
return Err(error::OperationError::new(
"solve: matrix is singular (or numerically indistinguishable from singular)",
));
}
if pivot != k {
a.swap(k, pivot);
perm.swap(k, pivot);
}
let pivot_val = a[k][k];
for i in (k + 1)..N {
let factor = a[i][k] / pivot_val;
a[i][k] = factor; for j in (k + 1)..N {
a[i][j] -= factor * a[k][j];
}
}
}
Ok((Matrix { data: a }, perm))
}
fn lu_solve(lu: &[[f64; N]; N], perm: &[usize; N], b: [f64; N]) -> [f64; N] {
let mut y = [0.0_f64; N];
for i in 0..N {
y[i] = b[perm[i]];
}
for i in 0..N {
for k in 0..i {
y[i] -= lu[i][k] * y[k];
}
}
let mut x = [0.0_f64; N];
for i in (0..N).rev() {
let mut sum = y[i];
for k in (i + 1)..N {
sum -= lu[i][k] * x[k];
}
debug_assert!(
lu[i][i] != 0.0,
"lu_decompose guarantees non-zero pivots"
);
x[i] = sum / lu[i][i];
}
x
}
pub fn det_via_lu(&self) -> Result<f64, error::OperationError> {
let (lu, perm) = self.lu_decompose()?;
let mut det = 1.0;
for i in 0..N {
det *= lu.data[i][i];
}
let mut visited = [false; N];
let mut swaps = 0usize;
for i in 0..N {
if visited[i] {
continue;
}
let mut j = i;
let mut cycle_len = 0usize;
while !visited[j] {
visited[j] = true;
j = perm[j];
cycle_len += 1;
}
if cycle_len > 1 {
swaps += cycle_len - 1;
}
}
if swaps % 2 == 1 {
det = -det;
}
Ok(det)
}
pub fn inverse(&self) -> Result<Self, error::OperationError> {
let mut cols = [[0.0_f64; N]; N];
for c in 0..N {
let mut e = [0.0_f64; N];
e[c] = 1.0;
let col = self.solve(e)?;
for r in 0..N {
cols[r][c] = col[r];
}
}
Ok(Matrix { data: cols })
}
}
impl<const ROWS: usize, const COLS: usize, T> Debug for Matrix<ROWS, COLS, T>
where
T: NumericElement + Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.display())
}
}impl<const ROWS: usize, const COLS: usize, T> Display for Matrix<ROWS, COLS, T>
where
T: NumericElement + Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.display())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn new_ok() {
let m = Matrix::<3, 2, i32>::new([[1, 2], [3, 4], [5, 6]]);
assert_eq!(m.height(), 3);
assert_eq!(m.width(), 2);
assert_eq!(m.data, [[1, 2], [3, 4], [5, 6]]);
}
#[test]
fn smallest_valid_size() {
let one = Matrix::<1, 1, i32>::new([[7]]);
assert_eq!(one.size(), (1, 1));
assert_eq!(one.get(0, 0), Some(7));
let row = Matrix::<1, 5, i32>::new([[1, 2, 3, 4, 5]]);
assert_eq!(row.size(), (1, 5));
let col = Matrix::<5, 1, i32>::new([[1], [2], [3], [4], [5]]);
assert_eq!(col.size(), (5, 1));
}
#[test]
fn test_print_single_line_matrix() {
let m = Matrix::<1, 3, i32>::new([[1, 2, 3]]);
assert_eq!(format!("{}", m), "[1 2 3 ]\n");
}
#[test]
fn test_print_multi_line_matrix() {
let vec = [[1, 2, 3], [4, 5, 6], [7, 8, 90]];
let m = Matrix::<3,3,i32>::new(vec);
let result = format!("{}", m);
println!("{}", result.clone());
assert_eq!("┌1 2 3 ┐\n|4 5 6 |\n└7 8 90 ┘\n", result);
let result = format!("{:?}", m);
println!("{}", result.clone());
assert_eq!("┌1 2 3 ┐\n|4 5 6 |\n└7 8 90 ┘\n", result);
let vec = [
[1.12,
2.231,
3.1],
[4.123123,
5.123123,
6.43422342323],
[7.121,
8.1,
90.0],
];
let m = Matrix::<3,3,f64>::new( vec);
let result = format!("{}", m);
println!("{}", result.clone());
assert_eq!(
r#"┌1.12 2.231 3.1 ┐
|4.123123 5.123123 6.43422342323 |
└7.121 8.1 90 ┘
"#,
result
);
}
#[test]
fn test_get() {
let m = Matrix::<2, 2, i32>::new([[1, 2], [3, 4]]);
assert_eq!(m.get(0, 1), Some(3));
assert_eq!(m.get(2, 1), None);
}
#[test]
fn test_clone() {
let m1 = Matrix::<2, 2, i32>::new([[1, 2], [3, 4]]);
let m2 = m1.clone();
assert_eq!(m1.data, m2.data);
}
#[test]
fn test_size() {
let m = Matrix::<3, 4, i32>::new([[0;4];3]);
assert_eq!(m.size(), (3, 4));
}
#[test]
fn test_set() {
let mut m = Matrix::<2, 2, i32>::new([[1, 2], [3, 4]]);
assert!(m.set(1, 1, 5).is_ok());
assert_eq!(m.data[1][1], 5);
}
#[test]
fn test_set_out_of_bounds() {
let mut m = Matrix::<2, 2, i32>::new([[1, 2], [3, 4]]);
assert!(m.set(2, 1, 5).is_err());
}
#[test]
fn test_det_2x2() {
let m = Matrix::<2, 2, i32>::new([[1, 2], [3, 4]]);
assert_eq!(m.det().unwrap(), -2);
}
#[test]
fn test_det_3x3() {
let m = Matrix::<3, 3, i32>::new([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
assert_eq!(m.det().unwrap(), 0);
}
fn assert_close(a: f64, b: f64, eps: f64) {
assert!((a - b).abs() < eps, "left = {a}, right = {b}");
}
#[test]
fn test_solve_2x2() {
let a = Matrix::<2, 2, f64>::new([[2.0, 1.0], [1.0, 3.0]]);
let x = a.solve([5.0, 10.0]).unwrap();
assert_close(x[0], 1.0, 1e-12);
assert_close(x[1], 3.0, 1e-12);
}
#[test]
fn test_solve_3x3_diagonal() {
let a = Matrix::<3, 3, f64>::new([
[2.0, 0.0, 0.0],
[0.0, 4.0, 0.0],
[0.0, 0.0, 8.0],
]);
let x = a.solve([6.0, 8.0, 16.0]).unwrap();
assert_close(x[0], 3.0, 1e-12);
assert_close(x[1], 2.0, 1e-12);
assert_close(x[2], 2.0, 1e-12);
}
#[test]
fn test_solve_residual_is_small() {
let a = Matrix::<3, 3, f64>::new([
[120.0, 8.0, 0.0],
[8.0, 130.0, 0.0],
[0.0, 0.0, 45.0],
]);
let b = [60.0, 0.0, 90.0];
let x = a.solve(b).unwrap();
let mut ax = [0.0_f64; 3];
for i in 0..3 {
for j in 0..3 {
ax[i] += a.data[i][j] * x[j];
}
}
for i in 0..3 {
assert_close(ax[i], b[i], 1e-9);
}
}
#[test]
fn test_solve_requires_pivoting() {
let a = Matrix::<2, 2, f64>::new([[0.0, 1.0], [1.0, 0.0]]);
let x = a.solve([3.0, 5.0]).unwrap();
assert_close(x[0], 5.0, 1e-12); assert_close(x[1], 3.0, 1e-12); }
#[test]
fn test_solve_pivoting_improves_accuracy() {
let a = Matrix::<2, 2, f64>::new([[1e-18, 1.0], [1.0, 1.0]]);
let b = [1.0, 2.0];
let x = a.solve(b).unwrap();
let mut ax = [0.0_f64; 2];
for i in 0..2 {
for j in 0..2 {
ax[i] += a.data[i][j] * x[j];
}
}
assert_close(ax[0], b[0], 1e-9);
assert_close(ax[1], b[1], 1e-9);
}
#[test]
fn test_solve_singular_is_error() {
let a = Matrix::<2, 2, f64>::new([[1.0, 2.0], [2.0, 4.0]]);
assert!(a.solve([1.0, 2.0]).is_err());
let z = Matrix::<3, 3, f64>::new([[0.0; 3]; 3]);
assert!(z.solve([1.0, 2.0, 3.0]).is_err());
}
#[test]
fn test_det_via_lu_matches_det() {
let m = Matrix::<2, 2, f64>::new([[1.0, 2.0], [3.0, 4.0]]);
assert_close(m.det_via_lu().unwrap(), -2.0, 1e-12);
let m = Matrix::<3, 3, f64>::new([
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 10.0],
]);
assert_close(m.det_via_lu().unwrap(), -3.0, 1e-12);
let m2 = Matrix::<3, 3, f64>::new([
[4.0, -2.0, 1.0],
[3.0, 6.0, -4.0],
[2.0, 1.0, 8.0],
]);
assert_close(m2.det_via_lu().unwrap(), m2.det().unwrap(), 1e-9);
assert_close(m2.det_via_lu().unwrap(), 263.0, 1e-9);
}
#[test]
fn test_det_via_lu_sign_after_swap() {
let m = Matrix::<2, 2, f64>::new([[0.0, 1.0], [1.0, 0.0]]);
assert_close(m.det_via_lu().unwrap(), -1.0, 1e-12);
}
#[test]
fn test_det_via_lu_singular() {
let m = Matrix::<3, 3, f64>::new([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]);
assert!(m.det_via_lu().is_err());
}
#[test]
fn test_inverse_diagonal() {
let m = Matrix::<2, 2, f64>::new([[2.0, 0.0], [0.0, 4.0]]);
let inv = m.inverse().unwrap();
assert_close(inv.get(0, 0).unwrap(), 0.5, 1e-12);
assert_close(inv.get(1, 1).unwrap(), 0.25, 1e-12);
}
#[test]
fn test_inverse_round_trip() {
let a = Matrix::<3, 3, f64>::new([
[4.0, 7.0, 2.0],
[3.0, 6.0, 1.0],
[2.0, 5.0, 3.0],
]);
let inv = a.inverse().unwrap();
let mut prod = [[0.0_f64; 3]; 3];
for i in 0..3 {
for j in 0..3 {
for k in 0..3 {
prod[i][j] += a.data[i][k] * inv.data[k][j];
}
}
}
for i in 0..3 {
for j in 0..3 {
let expected = if i == j { 1.0 } else { 0.0 };
assert_close(prod[i][j], expected, 1e-9);
}
}
}
#[test]
fn test_inverse_singular_is_error() {
let m = Matrix::<2, 2, f64>::new([[1.0, 2.0], [2.0, 4.0]]);
assert!(m.inverse().is_err());
}
#[test]
fn test_larger_system() {
let a = Matrix::<5, 5, f64>::new([
[4.0, 1.0, 0.0, 0.0, 0.0],
[1.0, 4.0, 1.0, 0.0, 0.0],
[0.0, 1.0, 4.0, 1.0, 0.0],
[0.0, 0.0, 1.0, 4.0, 1.0],
[0.0, 0.0, 0.0, 1.0, 4.0],
]);
let b = [1.0, 2.0, 3.0, 4.0, 5.0];
let x = a.solve(b).unwrap();
let mut ax = [0.0_f64; 5];
for i in 0..5 {
for j in 0..5 {
ax[i] += a.data[i][j] * x[j];
}
}
for i in 0..5 {
assert_close(ax[i], b[i], 1e-9);
}
}
#[test]
fn test_get_out_of_bounds() {
let m = Matrix::<2, 2, f64>::new([[1.0, 2.0], [3.0, 4.0]]);
assert!(m.get(2, 0).is_none());
assert!(m.get(0, 2).is_none());
assert!(m.get(1, 1).is_some());
}
}