Skip to main content

lib_jsl_special_functions/
struve.rs

1//! The Struve functions `H_ν` and modified Struve functions `L_ν` (SciPy's `struve` and
2//! `modstruve`).
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#struve-functions).
5
6use crate::bessel_functions_fractional::{bessel_ik, bessel_jy};
7use crate::gamma::ln_gamma;
8use std::f64::consts::PI;
9
10const EPS: f64 = f64::EPSILON;
11
12/// `1/Γ(a)` as `(ln|1/Γ(a)|, sign)`, with sign 0 at the poles.
13fn ln_rgamma(a: f64) -> (f64, f64) {
14    if a <= 0.0 && a == a.floor() {
15        return (f64::NEG_INFINITY, 0.0);
16    }
17    let sign = if a > 0.0 || (a.floor() as i64).rem_euclid(2) == 0 { 1.0 } else { -1.0 };
18    (-ln_gamma(a), sign)
19}
20
21/// An estimate of a value together with a bound on its absolute error.
22type Estimate = (f64, f64);
23
24/// The defining power series `Σ (∓1)^k (x/2)^{2k+ν+1} / (Γ(k + 3/2) Γ(k + ν + 3/2))`, accurate
25/// while its terms do not cancel, i.e. for `x` up to about `ν + 20`.
26fn power_series(v: f64, x: f64, modified: bool) -> Estimate {
27    let (mut sum, mut largest) = (0.0f64, 0.0f64);
28    let lx = (x / 2.0).ln();
29    for k in 0..400 {
30        let kf = k as f64;
31        let (lr, sign) = ln_rgamma(kf + v + 1.5);
32        let alt = if modified || k % 2 == 0 { 1.0 } else { -1.0 };
33        let term = alt * sign * ((2.0 * kf + v + 1.0) * lx - ln_gamma(kf + 1.5) + lr).exp();
34        sum += term;
35        largest = largest.max(term.abs());
36        if k > 5 && term.abs() <= 1e-17 * sum.abs() {
37            break;
38        }
39    }
40    (sum, (largest + sum.abs()) * EPS * 2.0)
41}
42
43/// The Bessel-function series `√(x/(2π)) Σ (x/2)^k/(k! (k + ½)) J_{k+ν+½}(x)`, with
44/// `(−1)^k I_{k+ν+½}` for `L_ν`; for `H_ν` it converges at all `x` without cancellation.
45fn bessel_series(v: f64, x: f64, modified: bool) -> Estimate {
46    let (mut sum, mut largest) = (0.0f64, 0.0f64);
47    let lx = (x / 2.0).ln();
48    for k in 0..400 {
49        let kf = k as f64;
50        let c = (kf * lx - ln_gamma(kf + 1.0)).exp() / (kf + 0.5);
51        let order = kf + v + 0.5;
52        let b = if modified {
53            let i = bessel_ik(order, x).0;
54            if k % 2 == 0 { i } else { -i }
55        } else {
56            bessel_jy(order, x).0
57        };
58        let term = c * b;
59        if !term.is_finite() {
60            return (f64::NAN, f64::INFINITY);
61        }
62        sum += term;
63        largest = largest.max(term.abs());
64        if k > 5 && term.abs() <= 1e-17 * sum.abs() {
65            break;
66        }
67    }
68    let pre = (x / (2.0 * PI)).sqrt();
69    (pre * sum, pre * (largest * 10.0 + sum.abs()) * EPS)
70}
71
72/// The large-`x` expansion `H_ν = Y_ν + (1/π) Σ Γ(k + ½) (x/2)^{ν−2k−1} / Γ(ν + ½ − k)` (and
73/// `L_ν = I_{−ν} − (1/π) Σ (−1)^k …`), summed until its terms stop decreasing; its error is
74/// about the last term.
75fn asymptotic(v: f64, x: f64, modified: bool) -> Estimate {
76    let lx = (x / 2.0).ln();
77    let (mut sum, mut last) = (0.0f64, f64::INFINITY);
78    for k in 0..80 {
79        let kf = k as f64;
80        let (lr, sign) = ln_rgamma(v + 0.5 - kf);
81        let mut a = sign * (ln_gamma(kf + 0.5) + (v - 2.0 * kf - 1.0) * lx + lr).exp();
82        if modified && k % 2 == 0 {
83            a = -a;
84        }
85        if a.abs() > last {
86            break;
87        }
88        sum += a;
89        last = a.abs();
90        if last == 0.0 {
91            break;
92        }
93    }
94    let base = if modified { bessel_ik(-v, x).0 } else { bessel_jy(v, x).1 };
95    let value = base + sum / PI;
96    (value, last / PI + (base.abs() + value.abs()) * EPS)
97}
98
99/// For `ν > −½`, `H_ν = Y_ν + (2(x/2)^ν/(√π Γ(ν + ½))) ∫₀^∞ e^{−xt}(1 + t²)^{ν−½} dt`: the integral
100/// is smooth and positive, and with `xt = exp(s − e^{−s})` (a double-exponential substitution)
101/// the trapezoid rule converges to full accuracy in 160 points. Accurate unless `H_ν` is much
102/// smaller than `Y_ν` (large orders at small `x`).
103fn integral_about_y(v: f64, x: f64) -> Estimate {
104    let h = 1.0 / 16.0;
105    let mut sum = 0.0;
106    for k in 0..160 {
107        let s = -5.0 + k as f64 * h;
108        let es = (-s).exp();
109        let u = (s - es).exp();
110        sum += (-u).exp() * (1.0 + (u / x).powi(2)).powf(v - 0.5) * (1.0 + es) * u;
111    }
112    let part = (v * (x / 2.0).ln() - ln_gamma(v + 0.5)).exp() * 2.0 / PI.sqrt() * sum * h / x;
113    let y = bessel_jy(v, x).1;
114    (y + part, (y.abs() + part.abs()) * 4.0 * EPS)
115}
116
117/// The most accurate of the methods, judging by their error estimates; the costly
118/// Bessel series only when neither of the others reaches full accuracy.
119fn struve_positive(v: f64, x: f64, modified: bool) -> f64 {
120    let consider = |best: &mut Estimate, e: Estimate| {
121        if e.0.is_finite() && e.1 < best.1 {
122            *best = e;
123        }
124    };
125    let mut best: Estimate = (f64::NAN, f64::INFINITY);
126    if x > 2.0 {
127        consider(&mut best, asymptotic(v, x, modified));
128        if !modified && v > -0.5 {
129            consider(&mut best, integral_about_y(v, x));
130        }
131    }
132    if x < 30.0 + v.abs() {
133        consider(&mut best, power_series(v, x, modified));
134    }
135    if !(best.1 <= 4.0 * EPS * best.0.abs()) {
136        consider(&mut best, bessel_series(v, x, modified));
137    }
138    best.0
139}
140
141/// `H_ν(0)` and `L_ν(0)`: 0 for `ν > −1`, `2/π` at `ν = −1`, infinite below.
142fn at_zero(v: f64) -> f64 {
143    if v > -1.0 {
144        0.0
145    } else if v == -1.0 {
146        2.0 / PI
147    } else {
148        f64::INFINITY
149    }
150}
151
152/// Negative `x`: `F_n(−x) = (−1)^{n+1} F_n(x)` for integer orders, `NaN` otherwise.
153fn reflect(v: f64, x: f64, modified: bool) -> f64 {
154    if v != v.round() {
155        return f64::NAN;
156    }
157    let r = struve_positive(v, -x, modified);
158    if (v.round() as i64).rem_euclid(2) == 0 { -r } else { r }
159}
160
161// ANCHOR: struve
162/// The Struve function `H_ν(x)` (SciPy's `struve`), a particular solution of the inhomogeneous
163/// Bessel equation `x²y'' + xy' + (x² − ν²)y = 4(x/2)^{ν+1}/(√π Γ(ν + ½))`. Of the defining
164/// power series, the Bessel-function series, the large-`x` expansion about `Y_ν` and (for
165/// `ν > −½`) the integral of `H_ν − Y_ν`, the one with the smallest error estimate is used.
166/// Negative `x` only for integer orders.
167pub fn struve_h(v: f64, x: f64) -> f64 {
168    if v.is_nan() || x.is_nan() {
169        return f64::NAN;
170    }
171    if x == 0.0 {
172        return at_zero(v);
173    }
174    if x < 0.0 {
175        return reflect(v, x, false);
176    }
177    struve_positive(v, x, false)
178}
179// ANCHOR_END: struve
180
181// ANCHOR: modstruve
182/// The modified Struve function `L_ν(x) = −i e^{−iνπ/2} H_ν(ix)` (SciPy's `modstruve`), by the
183/// same three methods with `I` in place of `J` and `Y`.
184pub fn struve_l(v: f64, x: f64) -> f64 {
185    if v.is_nan() || x.is_nan() {
186        return f64::NAN;
187    }
188    if x == 0.0 {
189        return at_zero(v);
190    }
191    if x < 0.0 {
192        return reflect(v, x, true);
193    }
194    struve_positive(v, x, true)
195}
196// ANCHOR_END: modstruve
197
198#[cfg(test)]
199mod tests {
200    use super::*;
201
202    #[test]
203    fn closed_forms_of_half_integer_order() {
204        // H_{1/2}(x) = √(2/(πx)) (1 − cos x), L_{−1/2}(x) = √(2/(πx)) sinh x.
205        for &x in &[0.3, 1.0, 4.0, 12.0, 25.0, 60.0] {
206            let c = (2.0 / (PI * x)).sqrt();
207            let h = c * (1.0 - x.cos());
208            assert!((struve_h(0.5, x) - h).abs() < 1e-13 * h.abs().max(c), "H {x}");
209            let l = c * x.sinh();
210            assert!((struve_l(-0.5, x) - l).abs() < 1e-13 * l, "L {x}");
211        }
212        // Integer reflection and the value at zero.
213        assert!((struve_h(1.0, -2.0) - struve_h(1.0, 2.0)).abs() < 1e-15);
214        assert!((struve_h(0.0, -2.0) + struve_h(0.0, 2.0)).abs() < 1e-15);
215        assert_eq!(struve_h(0.0, 0.0), 0.0);
216        assert!(struve_h(0.5, -1.0).is_nan());
217    }
218}