Skip to main content

lib_jsl_special_functions/
airy_functions.rs

1//! Airy functions.
2//!
3//! A Maclaurin series is used for small |x|; otherwise the functions are expressed through
4//! Bessel functions of order 1/3.
5//!
6//! See the book entry [Bessel and Airy functions](https://jwkunz.github.io/lib_jsl/book/special_functions/bessel.html#airy-functions).
7
8use crate::bessel_functions_fractional::{bessel_ik, bessel_jy};
9use std::f64::consts::PI;
10
11/// Ai(0)
12const AI0: f64 = 0.355_028_053_887_817_2;
13/// -Ai'(0)
14const AIP0: f64 = 0.258_819_403_792_806_8;
15const SERIES_LIMIT: f64 = 2.0;
16
17/// Returns the Maclaurin series pair `(f(x), g(x))` such that Ai = AI0 f - AIP0 g and
18/// Bi = √3 (AI0 f + AIP0 g).
19fn series_fg(x: f64) -> (f64, f64) {
20    let x3 = x * x * x;
21    let mut f = 1.0;
22    let mut g = x;
23    let mut tf = 1.0;
24    let mut tg = x;
25    for k in 1..200 {
26        let k3 = 3.0 * k as f64;
27        tf *= x3 / ((k3 - 1.0) * k3);
28        tg *= x3 / (k3 * (k3 + 1.0));
29        f += tf;
30        g += tg;
31        if tf.abs() < 1e-17 * f.abs() && tg.abs() < 1e-17 * g.abs().max(1e-300) {
32            break;
33        }
34    }
35    (f, g)
36}
37
38// ANCHOR: airy_ai
39/// The Airy function Ai(x).
40pub fn airy_ai(x: f64) -> f64 {
41    if x.is_nan() {
42        return f64::NAN;
43    }
44    if x.abs() <= SERIES_LIMIT {
45        let (f, g) = series_fg(x);
46        return AI0 * f - AIP0 * g;
47    }
48    if x > 0.0 {
49        let z = 2.0 / 3.0 * x * x.sqrt();
50        if z > 745.0 {
51            return 0.0;
52        }
53        let k = bessel_ik(1.0 / 3.0, z).1;
54        (x / 3.0).sqrt() / PI * k
55    } else {
56        let ax = -x;
57        let z = 2.0 / 3.0 * ax * ax.sqrt();
58        let (j, y) = bessel_jy(1.0 / 3.0, z);
59        0.5 * ax.sqrt() * (j - y / 3f64.sqrt())
60    }
61}
62// ANCHOR_END: airy_ai
63
64/// The Airy function Bi(x).
65pub fn airy_bi(x: f64) -> f64 {
66    if x.is_nan() {
67        return f64::NAN;
68    }
69    if x.abs() <= SERIES_LIMIT {
70        let (f, g) = series_fg(x);
71        return 3f64.sqrt() * (AI0 * f + AIP0 * g);
72    }
73    if x > 0.0 {
74        let z = 2.0 / 3.0 * x * x.sqrt();
75        if z > 700.0 {
76            return f64::INFINITY;
77        }
78        let (i, k) = bessel_ik(1.0 / 3.0, z);
79        // I_{-1/3} = I_{1/3} + (2/π) sin(π/3) K_{1/3}
80        let i_neg = i + 2.0 / PI * (PI / 3.0).sin() * k;
81        (x / 3.0).sqrt() * (i_neg + i)
82    } else {
83        let ax = -x;
84        let z = 2.0 / 3.0 * ax * ax.sqrt();
85        let (j, y) = bessel_jy(1.0 / 3.0, z);
86        -0.5 * ax.sqrt() * (y + j / 3f64.sqrt())
87    }
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    fn close(a: f64, b: f64, rel: f64) -> bool {
95        (a - b).abs() <= rel * b.abs()
96    }
97
98    #[test]
99    fn ai_values() {
100        assert!(close(airy_ai(0.0), 0.3550280538878172, 1e-15));
101        assert!(close(airy_ai(1.0), 0.13529241631288141, 1e-14));
102        assert!(close(airy_ai(-1.0), 0.5355608832923521, 1e-14));
103        assert!(close(airy_ai(3.0), 0.006591139357460719, 1e-13));
104        assert!(close(airy_ai(-5.0), 0.3507610090241143, 1e-13));
105        assert!(close(airy_ai(2.5), 0.01572592338047049, 1e-13));
106        assert!(close(airy_ai(-2.5), -0.11232506769296609, 1e-13));
107        assert!(close(airy_ai(10.0), 1.1047532552898686e-10, 1e-12));
108        assert!(close(airy_ai(-10.0), 0.04024123848644319, 1e-12));
109        assert_eq!(airy_ai(200.0), 0.0);
110    }
111
112    #[test]
113    fn bi_values() {
114        assert!(close(airy_bi(0.0), 0.6149266274460007, 1e-14));
115        assert!(close(airy_bi(1.0), 1.2074235949528713, 1e-14));
116        assert!(close(airy_bi(-1.0), 0.103_997_389_496_944_6, 1e-13));
117        assert!(close(airy_bi(3.0), 14.037328963730232, 1e-13));
118        assert!(close(airy_bi(-5.0), -0.13836913490160058, 1e-12));
119        assert!(close(airy_bi(10.0), 455641153.5482251, 1e-12));
120        assert!(close(airy_bi(-10.0), -0.314_679_829_643_838_6, 1e-12));
121    }
122
123    #[test]
124    fn continuity_across_switch() {
125        for &b in &[SERIES_LIMIT, -SERIES_LIMIT] {
126            let e = 1e-9;
127            assert!((airy_ai(b + e) - airy_ai(b - e)).abs() < 1e-8);
128            assert!((airy_bi(b + e) - airy_bi(b - e)).abs() < 1e-8);
129        }
130    }
131}