differential_equations/linalg/
error.rs

1//! Linear algebra error types.
2
3use crate::{
4    error,
5    traits::{Real, State},
6};
7
8/// Errors that can occur during linear algebra operations.
9#[derive(Debug, Clone, PartialEq)]
10pub enum LinalgError {
11    /// Input validation error (e.g., non-square matrix, mismatched dimensions)
12    BadInput { message: String },
13    /// Matrix is singular at the given step (1-indexed)
14    Singular { step: usize },
15    /// Pivot vector has incorrect size
16    PivotSizeMismatch { expected: usize, actual: usize },
17}
18
19impl std::fmt::Display for LinalgError {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        match self {
22            LinalgError::BadInput { message } => {
23                write!(f, "Linear algebra input error: {}", message)
24            }
25            LinalgError::Singular { step } => write!(f, "Matrix is singular at step {}", step),
26            LinalgError::PivotSizeMismatch { expected, actual } => {
27                write!(
28                    f,
29                    "Pivot vector size mismatch: expected {}, got {}",
30                    expected, actual
31                )
32            }
33        }
34    }
35}
36
37impl std::error::Error for LinalgError {}
38
39// Allow using `?` to bubble LinalgError into the crate's generic Error<T, Y>.
40impl<T, Y> From<LinalgError> for error::Error<T, Y>
41where
42    T: Real,
43    Y: State<T>,
44{
45    fn from(err: LinalgError) -> Self {
46        // Map linear algebra errors into a user-facing message.
47        error::Error::LinearAlgebra {
48            msg: err.to_string(),
49        }
50    }
51}