Skip to main content

lib_jsl_special_functions/
dawsons_integral.rs

1//! Dawson's integral.
2//!
3//! See the book entry [Incomplete gamma and beta functions and the error function](https://jwkunz.github.io/lib_jsl/book/special_functions/incomplete.html#dawsons-integral).
4
5use std::f64::consts::PI;
6use std::sync::OnceLock;
7
8const H: f64 = 0.2;
9const NMAX: usize = 20;
10
11fn coefficients() -> &'static [f64; NMAX] {
12    static C: OnceLock<[f64; NMAX]> = OnceLock::new();
13    C.get_or_init(|| {
14        let mut c = [0.0; NMAX];
15        for (i, v) in c.iter_mut().enumerate() {
16            let t = (2 * i + 1) as f64 * H;
17            *v = (-t * t).exp();
18        }
19        c
20    })
21}
22
23/// Dawson's integral F(x) = e^(-x²) ∫₀ˣ e^(t²) dt.
24///
25/// Evaluated with Rybicki's method (relative accuracy about 1e-15). Odd in `x`.
26pub fn dawson_integral(x: f64) -> f64 {
27    if x.is_nan() {
28        return f64::NAN;
29    }
30    let xx = x.abs();
31    if xx.is_infinite() {
32        return 0.0;
33    }
34    if xx < 0.2 {
35        // Taylor series: F(x) = x - 2x³/3 + 4x⁵/15 - 8x⁷/105 + ...
36        let x2 = x * x;
37        let mut term = x;
38        let mut sum = x;
39        for k in 1..30 {
40            term *= -2.0 * x2 / (2 * k + 1) as f64;
41            sum += term;
42            if term.abs() < 1e-18 * sum.abs() {
43                break;
44            }
45        }
46        return sum;
47    }
48    if xx > 1e8 {
49        // F(x) ≈ 1/(2x) (1 + 1/(2x²))
50        return (0.5 / x) * (1.0 + 0.5 / (x * x));
51    }
52    let c = coefficients();
53    let n0 = 2.0 * (0.5 * xx / H + 0.5).floor();
54    let xp = xx - n0 * H;
55    let mut e1 = (2.0 * xp * H).exp();
56    let e2 = e1 * e1;
57    let mut d1 = n0 + 1.0;
58    let mut d2 = d1 - 2.0;
59    let mut sum = 0.0;
60    for &ci in c.iter() {
61        sum += ci * (e1 / d1 + 1.0 / (d2 * e1));
62        d1 += 2.0;
63        d2 -= 2.0;
64        e1 *= e2;
65    }
66    let ans = (1.0 / PI.sqrt()) * (-xp * xp).exp() * sum;
67    if x < 0.0 { -ans } else { ans }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn values() {
76        let cases = [
77            (0.1, 0.09933599239785486),
78            (0.5, 0.424_436_383_502_022_3),
79            (1.0, 0.538_079_506_912_768_4),
80            (2.0, 0.30134038892379197),
81            (5.0, 0.10213407442427684),
82            (10.0, 0.050_253_847_187_598_53),
83            (-0.5, -0.424_436_383_502_022_3),
84        ];
85        for &(x, want) in &cases {
86            assert!((dawson_integral(x) - want).abs() < 1e-14 * want.abs().max(1.0), "F({x})");
87        }
88    }
89
90    #[test]
91    fn asymptotics_and_edges() {
92        assert_eq!(dawson_integral(0.0), 0.0);
93        let x = 1e3;
94        assert!((dawson_integral(x) / (0.5 / x * (1.0 + 0.5 / (x * x))) - 1.0).abs() < 1e-5);
95        assert_eq!(dawson_integral(f64::INFINITY), 0.0);
96    }
97
98    #[test]
99    fn branch_continuity() {
100        let e = 1e-9;
101        assert!((dawson_integral(0.2 - e) - dawson_integral(0.2 + e)).abs() < 1e-8);
102    }
103}