use nalgebra::DMatrix;
use crate::core::CalcError;
pub const MAX_MATRIX_DIM: usize = 1000;
pub fn det(m: &DMatrix<f64>) -> Result<f64, CalcError> {
if !m.is_square() {
return Err(CalcError::domain(format!(
"det() requires a square matrix, got {}x{}",
m.nrows(),
m.ncols()
))
.with_i18n(
"msg.matrix.det_square",
vec![
("rows".to_string(), m.nrows().to_string()),
("cols".to_string(), m.ncols().to_string()),
],
));
}
Ok(m.determinant())
}
pub fn transpose(m: &DMatrix<f64>) -> DMatrix<f64> {
m.transpose()
}
pub fn inverse(m: &DMatrix<f64>) -> Result<DMatrix<f64>, CalcError> {
if !m.is_square() {
return Err(CalcError::domain(format!(
"inverse() requires a square matrix, got {}x{}",
m.nrows(),
m.ncols()
))
.with_i18n(
"msg.matrix.inverse_square",
vec![
("rows".to_string(), m.nrows().to_string()),
("cols".to_string(), m.ncols().to_string()),
],
));
}
m.clone().try_inverse().ok_or_else(|| {
CalcError::domain("matrix is singular (not invertible)".to_string())
.with_i18n("msg.matrix.singular", vec![])
})
}
pub fn identity(n: usize) -> Result<DMatrix<f64>, CalcError> {
if n > MAX_MATRIX_DIM {
return Err(CalcError::domain(format!(
"identity() dimension {} exceeds maximum of {}",
n, MAX_MATRIX_DIM
))
.with_i18n(
"msg.matrix.identity_dim_exceeds",
vec![
("dim".to_string(), n.to_string()),
("max".to_string(), MAX_MATRIX_DIM.to_string()),
],
));
}
Ok(DMatrix::identity(n, n))
}
pub fn mat_add(a: &DMatrix<f64>, b: &DMatrix<f64>) -> Result<DMatrix<f64>, CalcError> {
if a.shape() != b.shape() {
return Err(CalcError::domain(format!(
"matrix dimension mismatch for add: {}x{} vs {}x{}",
a.nrows(),
a.ncols(),
b.nrows(),
b.ncols()
))
.with_i18n(
"msg.matrix.dim_mismatch_addsub",
vec![
("r1".to_string(), a.nrows().to_string()),
("c1".to_string(), a.ncols().to_string()),
("r2".to_string(), b.nrows().to_string()),
("c2".to_string(), b.ncols().to_string()),
],
));
}
Ok(a + b)
}
pub fn mat_sub(a: &DMatrix<f64>, b: &DMatrix<f64>) -> Result<DMatrix<f64>, CalcError> {
if a.shape() != b.shape() {
return Err(CalcError::domain(format!(
"matrix dimension mismatch for sub: {}x{} vs {}x{}",
a.nrows(),
a.ncols(),
b.nrows(),
b.ncols()
))
.with_i18n(
"msg.matrix.dim_mismatch_addsub",
vec![
("r1".to_string(), a.nrows().to_string()),
("c1".to_string(), a.ncols().to_string()),
("r2".to_string(), b.nrows().to_string()),
("c2".to_string(), b.ncols().to_string()),
],
));
}
Ok(a - b)
}
pub fn mat_mul(a: &DMatrix<f64>, b: &DMatrix<f64>) -> Result<DMatrix<f64>, CalcError> {
if a.ncols() != b.nrows() {
return Err(CalcError::domain(format!(
"matrix multiplication dimension mismatch: {}x{} * {}x{}",
a.nrows(),
a.ncols(),
b.nrows(),
b.ncols()
))
.with_i18n(
"msg.matrix.mul_dim_mismatch",
vec![
("r1".to_string(), a.nrows().to_string()),
("c1".to_string(), a.ncols().to_string()),
("r2".to_string(), b.nrows().to_string()),
("c2".to_string(), b.ncols().to_string()),
],
));
}
Ok(a * b)
}
pub fn scalar_mul(m: &DMatrix<f64>, s: f64) -> DMatrix<f64> {
m * s
}
pub fn scalar_div(m: &DMatrix<f64>, s: f64) -> Result<DMatrix<f64>, CalcError> {
if s == 0.0 {
return Err(CalcError::division_by_zero());
}
Ok(m / s)
}
#[cfg(test)]
mod tests {
use super::*;
fn mat2x2(data: [f64; 4]) -> DMatrix<f64> {
DMatrix::from_row_slice(2, 2, &data)
}
#[test]
fn test_det_2x2() {
let m = mat2x2([1.0, 2.0, 3.0, 4.0]);
assert!((det(&m).unwrap() - (-2.0)).abs() < 1e-10);
}
#[test]
fn test_det_identity() {
let m = DMatrix::identity(3, 3);
assert!((det(&m).unwrap() - 1.0).abs() < 1e-10);
}
#[test]
fn test_det_non_square() {
let m = DMatrix::from_row_slice(2, 3, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
assert!(det(&m).is_err());
}
#[test]
fn test_transpose() {
let m = mat2x2([1.0, 2.0, 3.0, 4.0]);
let t = transpose(&m);
assert!((t[(0, 1)] - 3.0).abs() < 1e-10);
assert!((t[(1, 0)] - 2.0).abs() < 1e-10);
}
#[test]
fn test_inverse_2x2() {
let m = mat2x2([1.0, 2.0, 3.0, 4.0]);
let inv = inverse(&m).unwrap();
let product = &m * &inv;
for i in 0..2 {
for j in 0..2 {
let expected = if i == j { 1.0 } else { 0.0 };
assert!((product[(i, j)] - expected).abs() < 1e-10);
}
}
}
#[test]
fn test_inverse_singular() {
let m = mat2x2([1.0, 2.0, 2.0, 4.0]);
assert!(inverse(&m).is_err());
}
#[test]
fn test_inverse_non_square() {
let m = DMatrix::from_row_slice(2, 3, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
assert!(inverse(&m).is_err());
}
#[test]
fn test_identity_3() {
let m = identity(3).unwrap();
assert_eq!(m.nrows(), 3);
assert_eq!(m.ncols(), 3);
assert!((m[(0, 0)] - 1.0).abs() < 1e-15);
assert!((m[(0, 1)] - 0.0).abs() < 1e-15);
}
#[test]
fn test_identity_exceeds_limit() {
assert!(identity(MAX_MATRIX_DIM + 1).is_err());
}
#[test]
fn test_mat_add() {
let a = mat2x2([1.0, 2.0, 3.0, 4.0]);
let b = mat2x2([5.0, 6.0, 7.0, 8.0]);
let c = mat_add(&a, &b).unwrap();
assert!((c[(0, 0)] - 6.0).abs() < 1e-10);
assert!((c[(1, 1)] - 12.0).abs() < 1e-10);
}
#[test]
fn test_mat_add_dim_mismatch() {
let a = DMatrix::from_row_slice(2, 2, &[1.0, 2.0, 3.0, 4.0]);
let b = DMatrix::from_row_slice(2, 3, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
assert!(mat_add(&a, &b).is_err());
}
#[test]
fn test_mat_sub() {
let a = mat2x2([5.0, 6.0, 7.0, 8.0]);
let b = mat2x2([1.0, 2.0, 3.0, 4.0]);
let c = mat_sub(&a, &b).unwrap();
assert!((c[(0, 0)] - 4.0).abs() < 1e-10);
}
#[test]
fn test_mat_mul() {
let a = mat2x2([1.0, 2.0, 3.0, 4.0]);
let b = mat2x2([5.0, 6.0, 7.0, 8.0]);
let c = mat_mul(&a, &b).unwrap();
assert!((c[(0, 0)] - 19.0).abs() < 1e-10);
assert!((c[(0, 1)] - 22.0).abs() < 1e-10);
}
#[test]
fn test_mat_mul_dim_mismatch() {
let a = DMatrix::from_row_slice(2, 3, &[1.0; 6]);
let b = DMatrix::from_row_slice(2, 2, &[1.0; 4]);
assert!(mat_mul(&a, &b).is_err());
}
#[test]
fn test_scalar_mul() {
let m = mat2x2([1.0, 2.0, 3.0, 4.0]);
let r = scalar_mul(&m, 3.0);
assert!((r[(0, 0)] - 3.0).abs() < 1e-10);
assert!((r[(1, 1)] - 12.0).abs() < 1e-10);
}
#[test]
fn test_scalar_div() {
let m = mat2x2([2.0, 4.0, 6.0, 8.0]);
let r = scalar_div(&m, 2.0).unwrap();
assert!((r[(0, 0)] - 1.0).abs() < 1e-10);
assert!((r[(1, 1)] - 4.0).abs() < 1e-10);
}
#[test]
fn test_scalar_div_zero() {
let m = mat2x2([1.0, 2.0, 3.0, 4.0]);
assert!(scalar_div(&m, 0.0).is_err());
}
}