Skip to main content

lib_jsl_special_functions/
bessel_functions_spherical.rs

1//! Spherical Bessel functions.
2//!
3//! See the book entry [Bessel and Airy functions](https://jwkunz.github.io/lib_jsl/book/special_functions/bessel.html#spherical-bessel-functions).
4
5use crate::bessel_functions_fractional::bessel_jy;
6use std::f64::consts::PI;
7
8/// Returns `(j_n(x), y_n(x))` for `n >= 0` and `x > 0`.
9fn spherical_jy(n: u32, x: f64) -> (f64, f64) {
10    let (j, y) = bessel_jy(n as f64 + 0.5, x);
11    let pref = (PI / (2.0 * x)).sqrt();
12    (pref * j, pref * y)
13}
14
15/// The spherical Bessel function of the first kind j_n(x) = √(π/2x) J_{n+1/2}(x).
16///
17/// Defined for all real `x` and integer `n` (negative `n` use `j_{-n-1} = (-1)^(n+1) y_n`).
18/// `j_n(-x) = (-1)^n j_n(x)`.
19pub fn bessel_spherical(n: i32, x: f64) -> f64 {
20    if x.is_nan() {
21        return f64::NAN;
22    }
23    if n < 0 {
24        let m = (-n - 1) as u32;
25        let y = bessel_spherical_y(m as i32, x);
26        return if m.is_multiple_of(2) { -y } else { y };
27    }
28    if x == 0.0 {
29        return if n == 0 { 1.0 } else { 0.0 };
30    }
31    if x < 0.0 {
32        let r = bessel_spherical(n, -x);
33        return if n % 2 == 0 { r } else { -r };
34    }
35    let n = n as u32;
36    if n == 0 {
37        // Closed form avoids needless loss near the origin.
38        return x.sin() / x;
39    }
40    spherical_jy(n, x).0
41}
42
43/// The spherical Bessel function of the second kind y_n(x) = √(π/2x) Y_{n+1/2}(x) for `x > 0`
44/// (`-inf` at 0, `NaN` for negative `x`).
45pub fn bessel_spherical_y(n: i32, x: f64) -> f64 {
46    if x.is_nan() || x < 0.0 {
47        return f64::NAN;
48    }
49    if n < 0 {
50        let m = (-n - 1) as u32;
51        let j = bessel_spherical(m as i32, x);
52        return if m.is_multiple_of(2) { j } else { -j };
53    }
54    if x == 0.0 {
55        return f64::NEG_INFINITY;
56    }
57    if n == 0 {
58        return -x.cos() / x;
59    }
60    spherical_jy(n as u32, x).1
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn closed_forms() {
69        for &x in &[0.5f64, 1.0, 3.0, 12.0] {
70            let j1 = x.sin() / (x * x) - x.cos() / x;
71            let j2 = (3.0 / (x * x) - 1.0) * x.sin() / x - 3.0 * x.cos() / (x * x);
72            assert!((bessel_spherical(1, x) - j1).abs() < 1e-12 * (1.0 + j1.abs()) , "j1 x={x}");
73            assert!((bessel_spherical(2, x) - j2).abs() < 1e-12 * (1.0 + j2.abs()), "j2 x={x}");
74            let y1 = -x.cos() / (x * x) - x.sin() / x;
75            assert!((bessel_spherical_y(1, x) - y1).abs() < 1e-12 * (1.0 + y1.abs()), "y1 x={x}");
76        }
77    }
78
79    #[test]
80    fn recurrence() {
81        // j_{n-1} + j_{n+1} = (2n+1)/x j_n
82        let x: f64 = 2.3;
83        for n in 1..10 {
84            let lhs = bessel_spherical(n - 1, x) + bessel_spherical(n + 1, x);
85            let rhs = (2 * n + 1) as f64 / x * bessel_spherical(n, x);
86            assert!((lhs - rhs).abs() < 1e-13);
87        }
88    }
89
90    #[test]
91    fn symmetry_and_edges() {
92        assert_eq!(bessel_spherical(0, 0.0), 1.0);
93        assert_eq!(bessel_spherical(3, 0.0), 0.0);
94        assert!((bessel_spherical(1, -2.0) + bessel_spherical(1, 2.0)).abs() < 1e-15);
95        assert!((bessel_spherical(2, -2.0) - bessel_spherical(2, 2.0)).abs() < 1e-15);
96        // j_{-1} = cos x / x, y_{-1} = sin x / x
97        let x: f64 = 1.3;
98        assert!((bessel_spherical(-1, x) - x.cos() / x).abs() < 1e-14);
99        assert!((bessel_spherical_y(-1, x) - x.sin() / x).abs() < 1e-14);
100        assert!(bessel_spherical_y(0, -1.0).is_nan());
101    }
102
103    #[test]
104    fn small_argument_limit() {
105        // j_n(x) ~ x^n / (2n+1)!!
106        let x = 1e-3;
107        assert!((bessel_spherical(3, x) / (x.powi(3) / 105.0) - 1.0).abs() < 1e-5);
108    }
109}