use std::fmt;
pub type OptimizeResult<T> = Result<T, OptimizeError>;
#[derive(Debug, Clone)]
pub enum OptimizeError {
DidNotConverge {
iterations: usize,
tolerance: f64,
context: String,
},
InvalidInterval { a: f64, b: f64, context: String },
SameSignBracket { fa: f64, fb: f64, context: String },
InvalidParameter { parameter: String, message: String },
NumericalError { message: String },
InvalidInput { context: String },
NumrError(String),
}
impl fmt::Display for OptimizeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DidNotConverge {
iterations,
tolerance,
context,
} => {
write!(
f,
"{}: did not converge after {} iterations (tolerance: {})",
context, iterations, tolerance
)
}
Self::InvalidInterval { a, b, context } => {
write!(
f,
"Invalid interval [{}, {}] in {}: bounds must satisfy a < b",
a, b, context
)
}
Self::SameSignBracket { fa, fb, context } => {
write!(
f,
"Function has same sign at bracket endpoints in {}: f(a)={}, f(b)={}",
context, fa, fb
)
}
Self::InvalidParameter { parameter, message } => {
write!(f, "Invalid parameter '{}': {}", parameter, message)
}
Self::NumericalError { message } => {
write!(f, "Numerical error: {}", message)
}
Self::InvalidInput { context } => {
write!(f, "Invalid input in {}", context)
}
Self::NumrError(msg) => {
write!(f, "numr error: {}", msg)
}
}
}
}
impl std::error::Error for OptimizeError {}
impl From<numr::error::Error> for OptimizeError {
fn from(err: numr::error::Error) -> Self {
Self::NumrError(err.to_string())
}
}