Skip to main content

lib_jsl_special_functions/
hankel.rs

1//! The Hankel functions `H⁽¹⁾_ν = J_ν + iY_ν` and `H⁽²⁾_ν = J_ν − iY_ν` (SciPy's `hankel1`,
2//! `hankel2`, `hankel1e` and `hankel2e`).
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;
7use num_complex::Complex;
8
9type C = Complex<f64>;
10
11// ANCHOR: hankel
12/// The Hankel function of the first kind `H⁽¹⁾_ν(x) = J_ν(x) + i Y_ν(x)` for real order and
13/// `x > 0` (`NaN` otherwise); an outgoing cylindrical wave, `≈ √(2/(πx)) e^{i(x − νπ/2 − π/4)}`.
14pub fn hankel1(v: f64, x: f64) -> C {
15    let (j, y) = bessel_jy(v, x);
16    C::new(j, y)
17}
18
19/// The Hankel function of the second kind `H⁽²⁾_ν(x) = J_ν(x) − i Y_ν(x)`.
20pub fn hankel2(v: f64, x: f64) -> C {
21    let (j, y) = bessel_jy(v, x);
22    C::new(j, -y)
23}
24// ANCHOR_END: hankel
25
26/// The scaled Hankel function `H⁽¹⁾_ν(x) e^{−ix}`, which removes the oscillation of the phase.
27pub fn hankel1e(v: f64, x: f64) -> C {
28    hankel1(v, x) * C::from_polar(1.0, -x)
29}
30
31/// The scaled Hankel function `H⁽²⁾_ν(x) e^{ix}`.
32pub fn hankel2e(v: f64, x: f64) -> C {
33    hankel2(v, x) * C::from_polar(1.0, x)
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use std::f64::consts::PI;
40
41    #[test]
42    fn half_order_closed_form_and_conjugacy() {
43        // H⁽¹⁾_{1/2}(x) = −i √(2/(πx)) e^{ix}.
44        for &x in &[0.5, 2.0, 9.0, 40.0] {
45            let h = hankel1(0.5, x);
46            let want = C::new(0.0, -(2.0 / (PI * x)).sqrt()) * C::from_polar(1.0, x);
47            assert!((h - want).norm() < 1e-13 * want.norm(), "{x}");
48            assert_eq!(hankel2(0.5, x), h.conj());
49            assert!((hankel1e(0.5, x).norm() - want.norm()).abs() < 1e-14);
50        }
51        assert!(hankel1(0.0, -1.0).re.is_nan());
52    }
53}