Skip to main content

lib_jsl_root_finding/
fixed_point.rs

1//! Fixed points `x = g(x)` by iteration accelerated with Steffensen's (Aitken's Δ²) method
2//! (SciPy's `optimize.fixed_point`).
3//!
4//! See the book entry [Halley's method and fixed points](https://jwkunz.github.io/lib_jsl/book/root_finding/halley-fixed-point.html).
5
6use lib_jsl_core::ErrorsJSL;
7
8/// How [`fixed_point`] steps.
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum FixedPointMethod {
12    /// Steffensen's method: Aitken's Δ² extrapolation of two plain steps (SciPy's `"del2"`).
13    Del2,
14    /// Plain iteration `x ← g(x)`.
15    Iteration,
16}
17
18// ANCHOR: fixed_point
19/// Finds a fixed point of `g` from `x0` (SciPy's `fixed_point`). With [`FixedPointMethod::Del2`]
20/// each step takes `p₁ = g(p₀)`, `p₂ = g(p₁)` and extrapolates
21/// `p = p₀ − (p₁ − p₀)²/(p₂ − 2p₁ + p₀)` (or `p₂` when the denominator vanishes), which converges
22/// quadratically even where plain iteration converges linearly — or not at all. Stops when the
23/// relative change `|p/p₀ − 1|` (`|p|` at `p₀ = 0`) is below `xtol` (SciPy: 10⁻⁸); errors after
24/// `maxiter` steps (SciPy: 500).
25pub fn fixed_point<G>(g: G, x0: f64, xtol: f64, maxiter: usize, method: FixedPointMethod) -> Result<f64, ErrorsJSL>
26where
27    G: Fn(f64) -> f64,
28{
29    let mut p0 = x0;
30    for _ in 0..maxiter {
31        let p1 = g(p0);
32        let p = match method {
33            FixedPointMethod::Iteration => p1,
34            FixedPointMethod::Del2 => {
35                let p2 = g(p1);
36                let d = p2 - 2.0 * p1 + p0;
37                if d != 0.0 { p0 - (p1 - p0) * (p1 - p0) / d } else { p2 }
38            }
39        };
40        if !p.is_finite() {
41            return Err(ErrorsJSL::RuntimeError("fixed_point: iterate is not finite"));
42        }
43        let relerr = if p0 != 0.0 { (p / p0 - 1.0).abs() } else { p.abs() };
44        if relerr < xtol {
45            return Ok(p);
46        }
47        p0 = p;
48    }
49    Err(ErrorsJSL::RuntimeError("fixed_point: failed to converge"))
50}
51// ANCHOR_END: fixed_point
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn steffensen_versus_iteration() {
59        // SciPy's documentation example: √(c₁/(x + c₂)) with c₁ = 10, c₂ = 2 → 2.2.
60        let g = |x: f64| (10.0 / (x + 2.0)).sqrt();
61        let a = fixed_point(g, 1.2, 1e-8, 500, FixedPointMethod::Del2).unwrap();
62        let b = fixed_point(g, 1.2, 1e-12, 500, FixedPointMethod::Iteration).unwrap();
63        // x²(x + 2) = 10.
64        for x in [a, b] {
65            assert!((x * x * (x + 2.0) - 10.0).abs() < 1e-9);
66        }
67        // x = 3.5 x (1 − x) has a repelling fixed point at 5/7 that only Steffensen reaches.
68        let g = |x: f64| 3.5 * x * (1.0 - x);
69        let s = fixed_point(g, 0.7, 1e-12, 500, FixedPointMethod::Del2).unwrap();
70        assert!((s - 5.0 / 7.0).abs() < 1e-12);
71        assert!(fixed_point(g, 0.7, 1e-12, 500, FixedPointMethod::Iteration).is_err());
72    }
73}