Skip to main content

lib_jsl_special_functions/
bessel_zeros.rs

1//! Zeros of the integer-order Bessel functions and their derivatives (SciPy's `jn_zeros`,
2//! `jnp_zeros`, `yn_zeros` and `ynp_zeros`).
3//!
4//! See the book entry [Faddeeva, Struve, Kelvin and Hankel functions](https://jwkunz.github.io/lib_jsl/book/special_functions/faddeeva-struve-kelvin.html#hankel-functions-and-bessel-zeros).
5
6use crate::bessel_functions_fractional::bessel_jy;
7
8/// Which function's zeros to find.
9#[derive(Clone, Copy)]
10enum Kind {
11    J,
12    Jp,
13    Y,
14    Yp,
15}
16
17fn evaluate(kind: Kind, n: u32, x: f64) -> f64 {
18    let v = n as f64;
19    match kind {
20        Kind::J => bessel_jy(v, x).0,
21        Kind::Y => bessel_jy(v, x).1,
22        Kind::Jp | Kind::Yp => {
23            // F'_n = (F_{n−1} − F_{n+1})/2, and F'_0 = −F_1.
24            let (j1, y1) = bessel_jy(v + 1.0, x);
25            let (a, b) = if n == 0 { (-j1, -y1) } else {
26                let (j0, y0) = bessel_jy(v - 1.0, x);
27                ((j0 - j1) / 2.0, (y0 - y1) / 2.0)
28            };
29            if matches!(kind, Kind::Jp) { a } else { b }
30        }
31    }
32}
33
34/// The Illinois variant of regula falsi on a bracket with a sign change, to full precision.
35fn refine(f: impl Fn(f64) -> f64, mut a: f64, mut b: f64) -> f64 {
36    let (mut fa, mut fb) = (f(a), f(b));
37    let mut side = 0;
38    for _ in 0..200 {
39        let c = (a * fb - b * fa) / (fb - fa);
40        let c = if c > a.min(b) && c < a.max(b) { c } else { 0.5 * (a + b) };
41        let fc = f(c);
42        if fc == 0.0 || (b - a).abs() <= 4.0 * f64::EPSILON * c.abs() {
43            return c;
44        }
45        if fc.signum() == fb.signum() {
46            b = c;
47            fb = fc;
48            if side == -1 {
49                fa /= 2.0;
50            }
51            side = -1;
52        } else {
53            a = c;
54            fa = fc;
55            if side == 1 {
56                fb /= 2.0;
57            }
58            side = 1;
59        }
60        if (b - a).abs() <= 4.0 * f64::EPSILON * b.abs() {
61            break;
62        }
63    }
64    let (fa, fb) = (f(a).abs(), f(b).abs());
65    if fa < fb { a } else { b }
66}
67
68/// The first `nt` positive zeros, scanning from `0.99 n` (every zero of these functions lies
69/// above `n`) in steps of ¼ — the zeros are at least 2.5 apart — and refining each sign change.
70fn zeros(kind: Kind, n: u32, nt: usize) -> Vec<f64> {
71    let f = |x: f64| evaluate(kind, n, x);
72    let mut out = Vec::with_capacity(nt);
73    let step = 0.25;
74    let mut a = (0.99 * n as f64).max(1e-3);
75    let mut fa = f(a);
76    while out.len() < nt {
77        let b = a + step;
78        let fb = f(b);
79        if fb == 0.0 {
80            out.push(b);
81        } else if fa.signum() != fb.signum() && fa != 0.0 {
82            out.push(refine(f, a, b));
83        }
84        a = b;
85        fa = fb;
86    }
87    out
88}
89
90// ANCHOR: zeros
91/// The first `nt` positive zeros `j_{n,k}` of `J_n` (SciPy's `jn_zeros`).
92pub fn jn_zeros(n: u32, nt: usize) -> Vec<f64> {
93    zeros(Kind::J, n, nt)
94}
95
96/// The first `nt` positive zeros `j'_{n,k}` of `J_n'` (SciPy's `jnp_zeros`); for `n = 0` the
97/// zero at the origin is not counted, so these are the zeros of `J₁`.
98pub fn jnp_zeros(n: u32, nt: usize) -> Vec<f64> {
99    zeros(Kind::Jp, n, nt)
100}
101
102/// The first `nt` positive zeros `y_{n,k}` of `Y_n` (SciPy's `yn_zeros`).
103pub fn yn_zeros(n: u32, nt: usize) -> Vec<f64> {
104    zeros(Kind::Y, n, nt)
105}
106
107/// The first `nt` positive zeros `y'_{n,k}` of `Y_n'` (SciPy's `ynp_zeros`).
108pub fn ynp_zeros(n: u32, nt: usize) -> Vec<f64> {
109    zeros(Kind::Yp, n, nt)
110}
111// ANCHOR_END: zeros
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116    use std::f64::consts::PI;
117
118    #[test]
119    fn tabulated_zeros_and_mcmahon() {
120        // Abramowitz and Stegun, table 9.5.
121        let j0 = jn_zeros(0, 3);
122        for (z, want) in j0.iter().zip([2.404825557695773, 5.520078110286311, 8.653727912911013]) {
123            assert!((z - want).abs() < 1e-14);
124        }
125        assert!((jnp_zeros(0, 1)[0] - 3.831705970207512).abs() < 1e-14);
126        assert!((jnp_zeros(1, 1)[0] - 1.841183781340659).abs() < 1e-14);
127        assert!((yn_zeros(0, 1)[0] - 0.8935769662791675).abs() < 1e-14);
128        assert!((ynp_zeros(0, 1)[0] - 2.197141326031017).abs() < 1e-14);
129        // McMahon: j_{0,k} ≈ (k − ¼)π + 1/(8(k − ¼)π) for large k.
130        let z = jn_zeros(0, 40);
131        let b = (40.0 - 0.25) * PI;
132        assert!((z[39] - (b + 1.0 / (8.0 * b))).abs() < 1e-6);
133        // Interlacing j_{n,1} < j_{n+1,1} < j_{n,2}.
134        let (a, c) = (jn_zeros(5, 2), jn_zeros(6, 1));
135        assert!(a[0] < c[0] && c[0] < a[1]);
136    }
137}