rootfinder/utils/
enums.rs

1/// Solver errors.
2#[derive(Debug)]
3pub enum SolverError {
4    /// An error that occurs when a bracketing interval is not found.
5    BracketingIntervalNotFound,
6
7    /// An error that occurs when the specified interval does not bracket a sign change.
8    IntervalDoesNotBracketSignChange,
9}
10
11impl std::fmt::Display for SolverError {
12    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
13        match self {
14            SolverError::BracketingIntervalNotFound => {
15                write!(
16                    f,
17                    "No interval was found that brackets a sign change of the function."
18                )
19            }
20            SolverError::IntervalDoesNotBracketSignChange => {
21                write!(
22                    f,
23                    "The provided interval does not bracket a sign change of the function."
24                )
25            }
26        }
27    }
28}
29
30/// Solver termination reasons.
31///
32/// # Note
33///
34/// We derive the `PartialEq` trait to facilitate easier unit testing.
35#[derive(Debug, Default, PartialEq)]
36pub enum TerminationReason {
37    /// Solver not yet terminated.
38    #[default]
39    NotYetTerminated,
40
41    /// Solver terminated on reaching the maximum number of iterations.
42    MaxIterationsReached,
43
44    /// Solver terminated on reaching the maximum number of function evaluations.
45    MaxFunctionEvaluationsReached,
46
47    /// Solver terminated on satisfying the absolute bracket tolerance.
48    AbsoluteBracketToleranceSatisfied,
49
50    /// Solver terminated on satisfying the relative bracket tolerance.
51    RelativeBracketToleranceSatisfied,
52
53    /// Solver terminated on satisfying the absolute step tolerance.
54    AbsoluteStepToleranceSatisfied,
55
56    /// Solver terminated on satisfying the value tolerance.
57    ValueToleranceSatisfied,
58
59    /// Solver terminated due to a zero derivative.
60    ///
61    /// # Note
62    ///
63    /// This is only used for Newton's method.
64    ZeroDerivative,
65
66    /// Solver terminated on finding a root at the lower bound of an initial interval.
67    RootAtLowerBound,
68
69    /// Solver terminated on finding a root at the upper bound of an initial interval.
70    RootAtUpperBound,
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn test_solver_error_bracketing_interval_not_found() {
79        assert_eq!(
80            format!("{}", SolverError::BracketingIntervalNotFound),
81            "No interval was found that brackets a sign change of the function."
82        );
83        assert_eq!(
84            format!("{}", SolverError::IntervalDoesNotBracketSignChange),
85            "The provided interval does not bracket a sign change of the function."
86        );
87    }
88
89    #[test]
90    fn test_termination_reason_default() {
91        let termination_reason = TerminationReason::default();
92        assert!(matches!(
93            termination_reason,
94            TerminationReason::NotYetTerminated
95        ));
96    }
97}