use std::fmt;
use crate::core::math::Scalar;
#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub enum RootError<E, F: Scalar = f64> {
Evaluation(E),
InvalidInterval {
lower: F,
upper: F,
},
InvalidInitialGuess {
x: F,
},
NotBracketed {
lower: F,
upper: F,
f_lower: F,
f_upper: F,
},
NonFiniteValue {
x: F,
value: F,
},
}
impl<E: fmt::Display, F: Scalar> fmt::Display for RootError<E, F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Evaluation(error) => {
write!(f, "root callback failed: {error}")
}
Self::InvalidInterval { lower, upper } => write!(
f,
"root interval must be finite and ordered with finite width, got [{lower:?}, {upper:?}]"
),
Self::InvalidInitialGuess { x } => write!(
f,
"root initial guess must be finite and strictly inside the interval, got {x:?}"
),
Self::NotBracketed {
lower,
upper,
f_lower,
f_upper,
} => write!(
f,
"root is not bracketed on [{lower:?}, {upper:?}]: values are {f_lower:?} and {f_upper:?}"
),
Self::NonFiniteValue { x, value } => write!(
f,
"root function returned non-finite value {value:?} at {x:?}"
),
}
}
}
impl<E: std::error::Error + 'static, F: Scalar> std::error::Error
for RootError<E, F>
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Evaluation(error) => Some(error),
_ => None,
}
}
}