use std::fmt;
pub const EPSILON: f32 = 1e-10;
#[derive(Debug, Clone, PartialEq)]
pub enum MatrixError {
NonSquare { rows: usize, cols: usize },
NotInvertible,
DimensionMismatch {
expected: (usize, usize),
got: (usize, usize),
},
NotAVector { cols: usize },
NumericalInstability,
EmptyMatrix,
}
impl fmt::Display for MatrixError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NonSquare { rows, cols } => {
write!(f, "Matrix must be square, got {}x{}", rows, cols)
}
Self::NotInvertible => write!(f, "Matrix is singular and cannot be inverted"),
Self::DimensionMismatch { expected, got } => {
write!(
f,
"Dimension mismatch: expected {}x{}, got {}x{}",
expected.0, expected.1, got.0, got.1
)
}
Self::NotAVector { cols } => {
write!(f, "Expected a vector (1 column), got {} columns", cols)
}
Self::NumericalInstability => {
write!(
f,
"Numerical instability: division by zero or near-zero value"
)
}
Self::EmptyMatrix => write!(f, "Empty matrix provided"),
}
}
}
impl std::error::Error for MatrixError {}
pub type MatrixResult<T> = Result<T, MatrixError>;