Skip to main content

lib_jsl_special_functions/
exponential_integrals.rs

1//! Exponential integrals.
2//!
3//! See the book entry [Exponential, sine, cosine and Fresnel integrals](https://jwkunz.github.io/lib_jsl/book/special_functions/exponential-integrals.html#exponential-integrals).
4
5const EULER: f64 = 0.577_215_664_901_532_9;
6const EPS: f64 = 1e-16;
7const FPMIN: f64 = f64::MIN_POSITIVE / EPS;
8
9/// The exponential integral E₁(x) = ∫ₓ^∞ e^(-t)/t dt for `x > 0`.
10///
11/// Returns `+inf` at 0 and `NaN` for negative `x` (where the function is complex).
12pub fn exponential_integral(x: f64) -> f64 {
13    exponential_integral_en(1, x)
14}
15
16// ANCHOR: en
17/// The generalized exponential integral Eₙ(x) = ∫₁^∞ e^(-xt)/tⁿ dt, for `x >= 0`.
18pub fn exponential_integral_en(n: u32, x: f64) -> f64 {
19    if x.is_nan() || x < 0.0 {
20        return f64::NAN;
21    }
22    if n == 0 {
23        return (-x).exp() / x;
24    }
25    let nm1 = (n - 1) as i64;
26    if x == 0.0 {
27        return if nm1 > 0 { 1.0 / nm1 as f64 } else { f64::INFINITY };
28    }
29    if x > 1.0 {
30        // Continued fraction (modified Lentz).
31        let mut b = x + n as f64;
32        let mut c = 1.0 / FPMIN;
33        let mut d = 1.0 / b;
34        let mut h = d;
35        for i in 1..=10_000 {
36            let a = -(i as f64) * (nm1 as f64 + i as f64);
37            b += 2.0;
38            d = 1.0 / (a * d + b);
39            c = b + a / c;
40            let del = c * d;
41            h *= del;
42            if (del - 1.0).abs() < EPS {
43                break;
44            }
45        }
46        h * (-x).exp()
47    } else {
48        // Power series.
49        let mut ans = if nm1 != 0 { 1.0 / nm1 as f64 } else { -x.ln() - EULER };
50        let mut fact = 1.0;
51        for i in 1..=1000i64 {
52            fact *= -x / i as f64;
53            let del = if i != nm1 {
54                -fact / (i - nm1) as f64
55            } else {
56                let mut psi = -EULER;
57                for ii in 1..=nm1 {
58                    psi += 1.0 / ii as f64;
59                }
60                fact * (-x.ln() + psi)
61            };
62            ans += del;
63            if del.abs() < ans.abs() * EPS {
64                break;
65            }
66        }
67        ans
68    }
69}
70// ANCHOR_END: en
71
72/// The exponential integral Ei(x) = -PV ∫₋ₓ^∞ e^(-t)/t dt, defined for all real `x`
73/// (principal value at 0, where it tends to -infinity).
74pub fn exponential_integral_ei(x: f64) -> f64 {
75    if x.is_nan() {
76        return f64::NAN;
77    }
78    if x == 0.0 {
79        return f64::NEG_INFINITY;
80    }
81    if x < 0.0 {
82        return -exponential_integral_en(1, -x);
83    }
84    if x < FPMIN {
85        return x.ln() + EULER;
86    }
87    if x <= -EPS.ln() {
88        let mut sum = 0.0;
89        let mut fact = 1.0;
90        for k in 1..=1000 {
91            fact *= x / k as f64;
92            let term = fact / k as f64;
93            sum += term;
94            if term < EPS * sum {
95                break;
96            }
97        }
98        sum + x.ln() + EULER
99    } else {
100        // Asymptotic series.
101        let mut sum = 0.0;
102        let mut term = 1.0;
103        for k in 1..=1000 {
104            let prev = term;
105            term *= k as f64 / x;
106            if term < EPS {
107                break;
108            }
109            if term < prev {
110                sum += term;
111            } else {
112                sum -= prev;
113                break;
114            }
115        }
116        x.exp() * (1.0 + sum) / x
117    }
118}
119
120#[cfg(test)]
121mod tests {
122    use super::*;
123
124    fn close(a: f64, b: f64, rel: f64) -> bool {
125        (a - b).abs() <= rel * b.abs()
126    }
127
128    #[test]
129    fn e1_values() {
130        assert!(close(exponential_integral(1.0), 0.21938393439552029, 1e-14));
131        assert!(close(exponential_integral(0.1), 1.8229239584193906, 1e-14));
132        assert!(close(exponential_integral(5.0), 0.001148295591275326, 1e-13));
133        assert!(exponential_integral(-1.0).is_nan());
134        assert!(exponential_integral(0.0).is_infinite());
135    }
136
137    #[test]
138    fn en_recurrence() {
139        // n E_{n+1}(x) = e^-x - x E_n(x)
140        for &x in &[0.3, 1.0, 2.5, 8.0] {
141            for n in 1..5u32 {
142                let lhs = n as f64 * exponential_integral_en(n + 1, x);
143                let rhs = (-x).exp() - x * exponential_integral_en(n, x);
144                assert!((lhs - rhs).abs() < 1e-14, "n={n} x={x}");
145            }
146        }
147        assert!(close(exponential_integral_en(0, 2.0), (-2.0f64).exp() / 2.0, 1e-15));
148    }
149
150    #[test]
151    fn ei_values() {
152        assert!(close(exponential_integral_ei(1.0), 1.8951178163559368, 1e-14));
153        assert!(close(exponential_integral_ei(10.0), 2492.228976241877, 1e-13));
154        assert!(close(exponential_integral_ei(50.0), 1.058_563_689_713_169e20, 1e-12));
155        assert!(close(exponential_integral_ei(-1.0), -0.21938393439552029, 1e-14));
156        assert_eq!(exponential_integral_ei(0.0), f64::NEG_INFINITY);
157    }
158}