use core::fmt;
use numra_core::NumraError;
#[derive(Clone, Debug, PartialEq)]
pub enum FitError {
InsufficientData { need: usize, got: usize },
Underdetermined { n_params: usize, n_data: usize },
OptimizationFailed(String),
LengthMismatch { expected: usize, got: usize },
InvalidParameter(String),
SingularJacobian,
}
impl fmt::Display for FitError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InsufficientData { need, got } => {
write!(f, "insufficient data: need at least {need}, got {got}")
}
Self::Underdetermined { n_params, n_data } => {
write!(
f,
"underdetermined: {n_params} parameters but only {n_data} data points"
)
}
Self::OptimizationFailed(msg) => write!(f, "optimization failed: {msg}"),
Self::LengthMismatch { expected, got } => {
write!(f, "length mismatch: expected {expected}, got {got}")
}
Self::InvalidParameter(msg) => write!(f, "invalid parameter: {msg}"),
Self::SingularJacobian => write!(f, "singular Jacobian: covariance unreliable"),
}
}
}
impl std::error::Error for FitError {}
impl From<FitError> for NumraError {
fn from(e: FitError) -> Self {
NumraError::Fit(e.to_string())
}
}