Skip to main content

lib_jsl_special_functions/
spheroidal.rs

1//! Spheroidal wave functions (SciPy's `pro_cv`, `obl_cv`, `pro_ang1`, `obl_ang1`, `pro_rad1`,
2//! `obl_rad1`): solutions of `(1 − x²) y'' − 2x y' + (λ − c²x² − m²/(1 − x²)) y = 0` (prolate;
3//! `c² → −c²` for oblate), in Flammer's normalisation.
4//!
5//! See the book entry [Mathieu, parabolic cylinder and spheroidal wave functions](https://jwkunz.github.io/lib_jsl/book/special_functions/wave-functions.html#spheroidal-wave-functions).
6
7use num_complex::Complex;
8use crate::bessel_functions_spherical::bessel_spherical;
9use crate::spherical_harmonics::associated_legendre;
10use crate::tridiagonal::{eigenvalues, eigenvector};
11
12/// `P_l^m(x)` without the Condon-Shortley phase (SciPy's and Flammer's convention).
13fn legendre(l: usize, m: usize, x: f64) -> f64 {
14    if l < m {
15        return 0.0;
16    }
17    let p = associated_legendre(l as u32, m as i32, x);
18    if m % 2 == 1 { -p } else { p }
19}
20
21/// `(2m + r)!/r!`.
22fn ratio(m: usize, r: usize) -> f64 {
23    ((r + 1)..=(r + 2 * m)).map(|k| k as f64).product()
24}
25
26// ANCHOR: flammer
27/// The characteristic value `λ_mn(c)` and the expansion coefficients `d_r` (`r ≡ n − m`
28/// mod 2, step 2) from Flammer's three-term recurrence
29/// `α_r d_{r+2} + (β_r − λ) d_r + γ_r d_{r−2} = 0`, with `κ = c²` (prolate) or `−c²` (oblate):
30/// `α_r = (2m+r+2)(2m+r+1) κ / ((2m+2r+3)(2m+2r+5))`,
31/// `β_r = (m+r)(m+r+1) + (2(m+r)(m+r+1) − 2m² − 1) κ / ((2m+2r−1)(2m+2r+3))`,
32/// `γ_r = r(r−1) κ / ((2m+2r−3)(2m+2r−1))`. The coefficients are normalised so that
33/// `Σ (2m+r)!/r! d_r = (n+m)!/(n−m)!`: the angular function tends to `P_n^m` as `c → 0`.
34fn solve(m: usize, n: usize, c: f64, oblate: bool) -> (f64, Vec<f64>, usize) {
35    let kappa = if oblate { -c * c } else { c * c };
36    let p = (n - m) % 2;
37    let size = (n - m) / 2 + 30 + (1.5 * c.abs()) as usize;
38    let r_of = |j: usize| p + 2 * j;
39    let (mf, kf) = (m as f64, kappa);
40    let d: Vec<f64> = (0..size)
41        .map(|j| {
42            let r = r_of(j) as f64;
43            (mf + r) * (mf + r + 1.0) + (2.0 * (mf + r) * (mf + r + 1.0) - 2.0 * mf * mf - 1.0) * kf / ((2.0 * mf + 2.0 * r - 1.0) * (2.0 * mf + 2.0 * r + 3.0))
44        })
45        .collect();
46    let sup: Vec<f64> = (0..size - 1)
47        .map(|j| {
48            let r = r_of(j) as f64;
49            (2.0 * mf + r + 2.0) * (2.0 * mf + r + 1.0) * kf / ((2.0 * mf + 2.0 * r + 3.0) * (2.0 * mf + 2.0 * r + 5.0))
50        })
51        .collect();
52    let sub: Vec<f64> = (1..size)
53        .map(|j| {
54            let r = r_of(j) as f64;
55            r * (r - 1.0) * kf / ((2.0 * mf + 2.0 * r - 3.0) * (2.0 * mf + 2.0 * r - 1.0))
56        })
57        .collect();
58    let lam = if c == 0.0 { (n * (n + 1)) as f64 } else { eigenvalues(&d, &sup, &sub)[(n - m) / 2] };
59    let mut v = if c == 0.0 {
60        let mut e = vec![0.0; size];
61        e[(n - m) / 2] = 1.0;
62        e
63    } else {
64        eigenvector(&d, &sup, &sub, lam)
65    };
66    let s: f64 = v.iter().enumerate().map(|(j, dj)| ratio(m, r_of(j)) * dj).sum();
67    let target = ratio(m, n - m);
68    v.iter_mut().for_each(|x| *x *= target / s);
69    (lam, v, p)
70}
71// ANCHOR_END: flammer
72
73/// The prolate characteristic value `λ_mn(c)` (SciPy's `pro_cv`), `n ≥ m`.
74pub fn pro_cv(m: usize, n: usize, c: f64) -> f64 {
75    if n < m { f64::NAN } else { solve(m, n, c, false).0 }
76}
77
78/// The oblate characteristic value (SciPy's `obl_cv`).
79pub fn obl_cv(m: usize, n: usize, c: f64) -> f64 {
80    if n < m { f64::NAN } else { solve(m, n, c, true).0 }
81}
82
83fn angular(m: usize, n: usize, c: f64, x: f64, oblate: bool) -> (f64, f64) {
84    if n < m || x.abs() >= 1.0 {
85        return (f64::NAN, f64::NAN);
86    }
87    let (_, d, p) = solve(m, n, c, oblate);
88    let eval = |x: f64| -> (f64, f64) {
89        let (mut s, mut ds) = (0.0, 0.0);
90        for (j, dj) in d.iter().enumerate() {
91            let l = m + p + 2 * j;
92            s += dj * legendre(l, m, x);
93            // (1 − x²) P_l^m' = (l + m) P_{l−1}^m − l x P_l^m.
94            let below = if l >= 1 { legendre(l - 1, m, x) } else { 0.0 };
95            ds += dj * ((l + m) as f64 * below - l as f64 * x * legendre(l, m, x)) / (1.0 - x * x);
96        }
97        (s, ds)
98    };
99    // Flammer's normalisation: S(c, 0) = P_n^m(0) for even n − m, S'(c, 0) = P_n^m'(0) for odd.
100    let (s0, ds0) = eval(0.0);
101    let scale = if p == 0 {
102        legendre(n, m, 0.0) / s0
103    } else {
104        let below = if n >= 1 { legendre(n - 1, m, 0.0) } else { 0.0 };
105        (n + m) as f64 * below / ds0
106    };
107    let (s, ds) = eval(x);
108    (scale * s, scale * ds)
109}
110
111/// The prolate angular function of the first kind `S_mn^{(1)}(c, x)`, `|x| < 1`, and its
112/// derivative (SciPy's `pro_ang1`): `Σ' d_r P_{m+r}^m(x)`, scaled as Flammer's so that
113/// `S(c, 0) = P_n^m(0)` (`n − m` even) or `S'(c, 0) = P_n^m'(0)` (odd).
114pub fn pro_ang1(m: usize, n: usize, c: f64, x: f64) -> (f64, f64) {
115    angular(m, n, c, x, false)
116}
117
118/// The oblate angular function of the first kind and its derivative (SciPy's `obl_ang1`).
119pub fn obl_ang1(m: usize, n: usize, c: f64, x: f64) -> (f64, f64) {
120    angular(m, n, c, x, true)
121}
122
123// ANCHOR: radial
124/// The radial function of the first kind (Flammer 4.1.17, 5.2.?): with `N = Σ' (2m+r)!/r! d_r`,
125/// `R_mn^{(1)}(c, ξ) = (1/N) (1 ∓ 1/ξ²)^{m/2} Σ' i^{r+m−n} (2m+r)!/r! d_r j_{m+r}(cξ)`, the upper
126/// sign for prolate (`ξ > 1`), the lower for oblate (`ξ ≥ 0`).
127fn radial(m: usize, n: usize, c: f64, xi: f64, oblate: bool) -> (f64, f64) {
128    if n < m || (!oblate && xi <= 1.0) || (oblate && xi < 0.0) || c <= 0.0 {
129        return (f64::NAN, f64::NAN);
130    }
131    let (_, d, p) = solve(m, n, c, oblate);
132    let norm: f64 = d.iter().enumerate().map(|(j, dj)| ratio(m, p + 2 * j) * dj).sum();
133    if oblate && xi == 0.0 {
134        // The limit ξ → 0: (1 + 1/ξ²)^{m/2} j_k(cξ) = (1 + ξ²)^{m/2} (cξ)^k ξ^{−m} (1 − …)/(2k+1)!!
135        // with k = m + r, so only r = 0 contributes to the value and r = 1 to the derivative.
136        let double_factorial = |k: usize| (1..=k).map(|i| (2 * i + 1) as f64).product::<f64>();
137        let term = |j: usize| {
138            let r = p + 2 * j;
139            let half = ((r + m) as i64 - n as i64) / 2;
140            let sign = if half.rem_euclid(2) == 0 { 1.0 } else { -1.0 };
141            sign * ratio(m, r) * d[j] * c.powi((m + r) as i32) /* (cξ)^k ξ^{−m} = c^k ξ^r */ / double_factorial(m + r)
142        };
143        return if p == 0 { (term(0) / norm, 0.0) } else { (0.0, term(0) / norm) };
144    }
145    let (mut s, mut ds) = (0.0, 0.0);
146    for (j, dj) in d.iter().enumerate() {
147        let r = p + 2 * j;
148        let k = m + r;
149        let half = ((r + m) as i64 - n as i64) / 2;
150        let sign = if half.rem_euclid(2) == 0 { 1.0 } else { -1.0 };
151        let w = sign * ratio(m, r) * dj;
152        let z = c * xi;
153        let jk = bessel_spherical(k as i32, z);
154        // j_k'(z) = j_{k−1}(z) − (k+1)/z j_k(z) (and j_0' = −j_1).
155        let djk = if k == 0 { -bessel_spherical(1, z) } else { bessel_spherical(k as i32 - 1, z) - (k + 1) as f64 / z * jk };
156        s += w * jk;
157        ds += w * c * djk;
158    }
159    let sgn = if oblate { 1.0 } else { -1.0 };
160    let base = 1.0 + sgn / (xi * xi);
161    let f = base.powf(m as f64 / 2.0);
162    let df = if m == 0 { 0.0 } else { (m as f64 / 2.0) * base.powf(m as f64 / 2.0 - 1.0) * (-2.0 * sgn / xi.powi(3)) };
163    ((f * s) / norm, (df * s + f * ds) / norm)
164}
165// ANCHOR_END: radial
166
167/// The prolate radial function of the first kind `R_mn^{(1)}(c, ξ)`, `ξ > 1`, and its
168/// derivative (SciPy's `pro_rad1`).
169pub fn pro_rad1(m: usize, n: usize, c: f64, xi: f64) -> (f64, f64) {
170    radial(m, n, c, xi, false)
171}
172
173/// The oblate radial function of the first kind, `ξ ≥ 0`, and its derivative (SciPy's
174/// `obl_rad1`).
175pub fn obl_rad1(m: usize, n: usize, c: f64, xi: f64) -> (f64, f64) {
176    radial(m, n, c, xi, true)
177}
178
179// ANCHOR: radial2
180/// The coefficients `a_k` of the large-`ξ` expansion `R^{(3)} = R^{(1)} + i R^{(2)} ~ e^{icξ} Σ a_k ξ^{−k−1}`
181/// of the outgoing radial solution, from the five-term recurrence that the radial equation
182/// `(ξ² − s) R'' + 2ξ R' − (λ − c²ξ² + s m²/(ξ² − s)) R = 0` gives (`s = 1` prolate, `−1` oblate):
183/// `2ic j a_j = A₁ a_{j−1} + A₂ a_{j−2} + A₃ a_{j−3} + A₄ a_{j−4} + A₅ a_{j−5}`, started from
184/// `a₀ = (−i)^{n+1}/c` so that `R^{(3)} ~ h_n^{(1)}(cξ)`, i.e. `R^{(1)} ~ j_n(cξ)` and
185/// `R^{(2)} ~ y_n(cξ)` (Flammer's normalisation). The series diverges, but its smallest term is of
186/// the order of `e^{−2cξ}`. Returns `R^{(3)}` and its derivative at `ξ`, or `None` if the terms
187/// do not get small enough (`cξ` too small for this `λ`).
188fn outgoing(m: usize, n: usize, c: f64, lambda: f64, xi: f64, s: f64) -> Option<(Complex<f64>, Complex<f64>)> {
189    let i = Complex::new(0.0, 1.0);
190    let a0 = Complex::new(0.0, -1.0).powi(n as i32 + 1) / c;
191    let mut a: Vec<Complex<f64>> = vec![a0];
192    let (mut f, mut fp) = (a0 / xi, -a0 / (xi * xi));
193    let mut previous = f64::INFINITY;
194    let at = |a: &[Complex<f64>], j: i64| if j >= 0 { a[j as usize] } else { Complex::new(0.0, 0.0) };
195    for j in 1..400i64 {
196        let jf = j as f64;
197        let a1 = (s * c * c - lambda) + jf * (jf - 1.0);
198        let a2 = i * (2.0 * c * s * (2.0 * jf - 3.0));
199        let a3 = s * (lambda - s * c * c - (m * m) as f64 - 2.0 * (jf - 2.0) * (jf - 2.0));
200        let a4 = i * (-2.0 * c * (jf - 3.0));
201        let a5 = (jf - 4.0) * (jf - 3.0);
202        let aj = (a1 * at(&a, j - 1) + a2 * at(&a, j - 2) + a3 * at(&a, j - 3) + a4 * at(&a, j - 4) + a5 * at(&a, j - 5)) / (i * (2.0 * c * jf));
203        a.push(aj);
204        let term = aj / xi.powi(j as i32 + 1);
205        let size = term.norm();
206        if j > 8 && size > previous {
207            // The terms have started to grow: stop at the smallest, if it is small enough.
208            return if previous < 1e-16 * f.norm() { Some(finish(c, xi, f, fp)) } else { None };
209        }
210        previous = size;
211        f += term;
212        fp += -(jf + 1.0) * aj / xi.powi(j as i32 + 2);
213        if size < 1e-17 * f.norm() {
214            return Some(finish(c, xi, f, fp));
215        }
216    }
217    None
218}
219
220fn finish(c: f64, xi: f64, f: Complex<f64>, fp: Complex<f64>) -> (Complex<f64>, Complex<f64>) {
221    let e = Complex::new(0.0, c * xi).exp();
222    (e * f, e * (fp + Complex::new(0.0, c) * f))
223}
224
225/// The radial function of the second kind and its derivative by integrating the radial equation
226/// inward from where the large-`ξ` expansion of `R^{(3)}` is accurate (`Im R^{(3)} = R^{(2)}`: the
227/// real part is `R^{(1)}`, whose asymptotic form is `j_n`, so no phase shift arises). Toward
228/// `ξ = 1` the second-kind solution is the dominant one, so the integration is stable. The
229/// integrator is Dormand and Prince's embedded 5(4) pair with step control.
230fn radial_second(m: usize, n: usize, c: f64, xi: f64, oblate: bool) -> (f64, f64) {
231    if n < m || (!oblate && xi <= 1.0) || (oblate && xi < 0.0) || c <= 0.0 || !xi.is_finite() {
232        return (f64::NAN, f64::NAN);
233    }
234    let s = if oblate { -1.0 } else { 1.0 };
235    let lambda = if oblate { obl_cv(m, n, c) } else { pro_cv(m, n, c) };
236    // The starting point: far enough that the expansion converges (2cξ above λ, and at least 30/c).
237    let mut x0 = xi.max(30.0 / c).max(lambda.abs() / c);
238    let start = loop {
239        match outgoing(m, n, c, lambda, x0, s) {
240            Some(v) => break v,
241            None if x0 < 1e7 => x0 *= 2.0,
242            None => return (f64::NAN, f64::NAN),
243        }
244    };
245    let (mut y, mut yp) = (start.0.im, start.1.im);
246    if x0 == xi {
247        return (y, yp);
248    }
249    let mm = (m * m) as f64;
250    let rhs = |x: f64, y: f64, yp: f64| -> f64 { ((lambda - c * c * x * x + s * mm / (x * x - s)) * y - 2.0 * x * yp) / (x * x - s) };
251    // Dormand-Prince 5(4), integrating from x0 down to xi (a negative step).
252    let (mut x, mut h) = (x0, -(0.05 / c).min(0.1 * (x0 - xi)));
253    let tol = 1e-13;
254    let mut guard = 0u64;
255    while x > xi {
256        guard += 1;
257        if guard > 5_000_000 {
258            return (f64::NAN, f64::NAN);
259        }
260        if x + h < xi {
261            h = xi - x;
262        }
263        let f1 = (yp, rhs(x, y, yp));
264        let st = |k: &[(f64, f64)], w: &[f64]| (y + h * k.iter().zip(w).map(|(a, b)| a.0 * b).sum::<f64>(), yp + h * k.iter().zip(w).map(|(a, b)| a.1 * b).sum::<f64>());
265        let s2 = st(&[f1], &[1.0 / 5.0]);
266        let f2 = (s2.1, rhs(x + h / 5.0, s2.0, s2.1));
267        let s3 = st(&[f1, f2], &[3.0 / 40.0, 9.0 / 40.0]);
268        let f3 = (s3.1, rhs(x + 3.0 * h / 10.0, s3.0, s3.1));
269        let s4 = st(&[f1, f2, f3], &[44.0 / 45.0, -56.0 / 15.0, 32.0 / 9.0]);
270        let f4 = (s4.1, rhs(x + 4.0 * h / 5.0, s4.0, s4.1));
271        let s5 = st(&[f1, f2, f3, f4], &[19372.0 / 6561.0, -25360.0 / 2187.0, 64448.0 / 6561.0, -212.0 / 729.0]);
272        let f5 = (s5.1, rhs(x + 8.0 * h / 9.0, s5.0, s5.1));
273        let s6 = st(&[f1, f2, f3, f4, f5], &[9017.0 / 3168.0, -355.0 / 33.0, 46732.0 / 5247.0, 49.0 / 176.0, -5103.0 / 18656.0]);
274        let f6 = (s6.1, rhs(x + h, s6.0, s6.1));
275        let new = st(&[f1, f3, f4, f5, f6], &[35.0 / 384.0, 500.0 / 1113.0, 125.0 / 192.0, -2187.0 / 6784.0, 11.0 / 84.0]);
276        let f7 = (new.1, rhs(x + h, new.0, new.1));
277        // The difference between the fifth- and fourth-order results.
278        let e = [71.0 / 57600.0, -71.0 / 16695.0, 71.0 / 1920.0, -17253.0 / 339200.0, 22.0 / 525.0, -1.0 / 40.0];
279        let ks = [f1, f3, f4, f5, f6, f7];
280        let err_y = h * ks.iter().zip(&e).map(|(k, w)| k.0 * w).sum::<f64>();
281        let err_yp = h * ks.iter().zip(&e).map(|(k, w)| k.1 * w).sum::<f64>();
282        let scale_y = tol * (y.abs().max(new.0.abs()) + 1e-300);
283        let scale_yp = tol * (yp.abs().max(new.1.abs()) + 1e-300);
284        let err = (err_y.abs() / scale_y).max(err_yp.abs() / scale_yp);
285        if err <= 1.0 {
286            x += h;
287            y = new.0;
288            yp = new.1;
289        }
290        let factor = if err == 0.0 { 5.0 } else { (0.9 * err.powf(-0.2)).clamp(0.2, 5.0) };
291        h *= factor;
292        // Near the singular point at ξ = 1 the steps shrink with the distance to it.
293        if !oblate {
294            h = -h.abs().min(0.5 * (x - 1.0).max(1e-12));
295        }
296        if x <= xi {
297            break;
298        }
299    }
300    (y, yp)
301}
302// ANCHOR_END: radial2
303
304/// The prolate radial function of the second kind `R_mn^{(2)}(c, ξ)`, `ξ > 1`, and its derivative
305/// (SciPy's `pro_rad2`), normalised like Flammer's: `R^{(2)} ~ y_n(cξ)` for large `ξ`.
306///
307/// Where SciPy's routine is accurate the two agree to about `10⁻¹⁰` (relative); it is not always
308/// accurate (see the book entry), and this one satisfies the Wronskian
309/// `R^{(1)} R^{(2)\prime} − R^{(1)\prime} R^{(2)} = 1/(c (ξ² − 1))` to the accuracy of `R^{(1)}`.
310pub fn pro_rad2(m: usize, n: usize, c: f64, xi: f64) -> (f64, f64) {
311    radial_second(m, n, c, xi, false)
312}
313
314/// The oblate radial function of the second kind, `ξ ≥ 0`, and its derivative (SciPy's
315/// `obl_rad2`); the Wronskian is `1/(c (ξ² + 1))`.
316pub fn obl_rad2(m: usize, n: usize, c: f64, xi: f64) -> (f64, f64) {
317    radial_second(m, n, c, xi, true)
318}
319
320#[cfg(test)]
321mod tests {
322    use super::*;
323
324    /// The Wronskian of the radial functions: `R1 R2' − R1' R2 = 1/(c (ξ² ∓ 1))`.
325    fn wronskian(m: usize, n: usize, c: f64, xi: f64, oblate: bool) -> (f64, f64) {
326        let (r1, r1p) = radial(m, n, c, xi, oblate);
327        let (r2, r2p) = radial_second(m, n, c, xi, oblate);
328        (r1 * r2p - r1p * r2, 1.0 / (c * (xi * xi + if oblate { 1.0 } else { -1.0 })))
329    }
330
331    #[test]
332    fn oblate_radial_function_of_the_first_kind_at_the_origin_is_the_limit() {
333        for &(m, n, c) in &[(0usize, 0usize, 1.5f64), (0, 1, 2.0), (0, 2, 0.7), (1, 1, 1.2), (1, 2, 3.0), (2, 2, 1.0), (2, 3, 2.5), (3, 5, 1.8)] {
334            let (v0, d0) = obl_rad1(m, n, c, 0.0);
335            let h = 1e-6;
336            let (v1, d1) = obl_rad1(m, n, c, h);
337            assert!(v0.is_finite() && d0.is_finite(), "m={m} n={n} c={c}: {v0} {d0}");
338            assert!((v0 - v1).abs() < 1e-5 * (1.0 + v0.abs()), "m={m} n={n} c={c}: value {v0} vs {v1}");
339            assert!((d0 - d1).abs() < 1e-4 * (1.0 + d0.abs()), "m={m} n={n} c={c}: derivative {d0} vs {d1}");
340        }
341        // For m = 0, n = 0, c → 0: R_00(c, 0) → j_0(0) = 1.
342        assert!((obl_rad1(0, 0, 1e-3, 0.0).0 - 1.0).abs() < 1e-5);
343    }
344
345    #[test]
346    fn second_kind_satisfies_the_wronskian_and_matches_scipy() {
347        for (m, n, c, xi) in [(0, 0, 1.0, 1.5), (0, 3, 2.0, 1.2), (2, 4, 3.5, 1.05), (1, 2, 0.5, 3.0), (3, 3, 8.0, 1.3), (0, 1, 15.0, 1.01), (4, 7, 2.5, 1.5), (2, 2, 20.0, 2.0)] {
348            let (w, want) = wronskian(m, n, c, xi, false);
349            assert!((w - want).abs() < 1e-8 * want, "prolate {m} {n} {c} {xi}: {w} vs {want}");
350        }
351        for (m, n, c, xi) in [(0, 0, 1.0, 0.5), (0, 3, 2.0, 1.2), (2, 4, 3.5, 0.05), (1, 2, 0.5, 3.0), (3, 3, 8.0, 0.3), (0, 1, 15.0, 0.01), (1, 5, 5.0, 0.001)] {
352            let (w, want) = wronskian(m, n, c, xi, true);
353            assert!((w - want).abs() < 1e-8 * want, "oblate {m} {n} {c} {xi}: {w} vs {want}");
354        }
355        // Values from SciPy 1.17.1's pro_rad2 and obl_rad2 where they are accurate.
356        for (m, n, c, xi, want, oblate) in [(0, 0, 1.0, 1.5, -0.2629632165298937, false), (0, 3, 2.0, 1.2, -2.1318744357685215, false), (1, 2, 0.5, 3.0, -1.4622835690095346, false), (1, 2, 0.5, 3.0, -1.2489707526212206, true)] {
357            let (r2, _) = radial_second(m, n, c, xi, oblate);
358            assert!((r2 - want).abs() < 1e-9 * want.abs().max(1.0), "{m} {n} {c} {xi}: {r2} vs {want}");
359        }
360        // At ξ = 0 the oblate second-kind functions are finite.
361        assert!(obl_rad2(1, 5, 5.0, 0.0).0.is_finite() && obl_rad2(0, 2, 1.0, 0.0).0.is_finite());
362        // R^(2) ~ y_n(cξ) far out.
363        let (r2, _) = pro_rad2(0, 2, 1.0, 60.0);
364        let y = crate::bessel_functions_spherical::bessel_spherical_y(2, 60.0);
365        assert!((r2 - y).abs() < 2e-3, "{r2} {y}");
366        assert!(pro_rad2(2, 1, 1.0, 2.0).0.is_nan() && pro_rad2(0, 0, 1.0, 1.0).0.is_nan() && obl_rad2(0, 0, 1.0, -0.1).0.is_nan());
367    }
368
369    #[test]
370    fn limits_and_ordering() {
371        // c → 0: λ = n(n+1) and S → P_n^m.
372        assert!((pro_cv(1, 3, 1e-8) - 12.0).abs() < 1e-10);
373        let (s, _) = pro_ang1(2, 3, 1e-6, 0.3);
374        assert!((s - 15.0 * 0.3 * (1.0 - 0.09)).abs() < 1e-8, "{s}");
375        // Prolate values rise and oblate values fall with c; both keep the order in n.
376        assert!(pro_cv(0, 2, 3.0) > 6.0 && obl_cv(0, 2, 3.0) < 6.0);
377        assert!(pro_cv(1, 2, 5.0) < pro_cv(1, 3, 5.0) && obl_cv(1, 2, 5.0) < obl_cv(1, 3, 5.0));
378        // R^(1) behaves like j_n(cξ) for large ξ.
379        let (r, _) = pro_rad1(0, 2, 1.0, 60.0);
380        let j = crate::bessel_functions_spherical::bessel_spherical(2, 60.0);
381        assert!((r - j).abs() < 2e-3, "{r} {j}");
382    }
383}