Skip to main content

lib_jsl_special_functions/
cosine_integrals.rs

1//! Cosine 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#sine-and-cosine-integrals).
4
5use std::f64::consts::FRAC_PI_2;
6
7const EULER: f64 = 0.577_215_664_901_532_9;
8const EPS: f64 = 1e-16;
9const FPMIN: f64 = f64::MIN_POSITIVE / EPS;
10const TMIN: f64 = 2.0;
11
12/// Returns `(Si(|x|), Ci(|x|))` for finite `x`.
13pub(crate) fn si_ci_positive(t: f64) -> (f64, f64) {
14    if t == 0.0 {
15        return (0.0, f64::NEG_INFINITY);
16    }
17    if t > TMIN {
18        // Complex continued fraction (modified Lentz) for E1(it).
19        let mut b = (1.0, t);
20        let mut c = (1.0 / FPMIN, 0.0);
21        let inv = |z: (f64, f64)| {
22            let den = z.0 * z.0 + z.1 * z.1;
23            (z.0 / den, -z.1 / den)
24        };
25        let mul = |a: (f64, f64), b: (f64, f64)| (a.0 * b.0 - a.1 * b.1, a.0 * b.1 + a.1 * b.0);
26        let mut d = inv(b);
27        let mut h = d;
28        for i in 2..100_000 {
29            let a = -((i - 1) as f64) * ((i - 1) as f64);
30            b.0 += 2.0;
31            d = inv((a * d.0 + b.0, a * d.1 + b.1));
32            let q = inv(c);
33            c = (b.0 + a * q.0, b.1 + a * q.1);
34            let del = mul(c, d);
35            h = mul(h, del);
36            if (del.0 - 1.0).abs() + del.1.abs() < EPS {
37                break;
38            }
39        }
40        h = mul((t.cos(), -t.sin()), h);
41        (FRAC_PI_2 + h.1, -h.0)
42    } else if t < FPMIN.sqrt() {
43        (t, EULER + t.ln())
44    } else {
45        // Power series.
46        let mut sum = 0.0;
47        let mut sums = 0.0;
48        let mut sumc = 0.0;
49        let mut sign = 1.0;
50        let mut fact = 1.0;
51        let mut odd = true;
52        for k in 1..1000 {
53            fact *= t / k as f64;
54            let term = fact / k as f64;
55            sum += sign * term;
56            let err = term / sum.abs();
57            if odd {
58                sign = -sign;
59                sums = sum;
60                sum = sumc;
61            } else {
62                sumc = sum;
63                sum = sums;
64            }
65            if err < EPS {
66                break;
67            }
68            odd = !odd;
69        }
70        (sums, sumc + t.ln() + EULER)
71    }
72}
73
74/// The cosine integral Ci(x) = γ + ln x + ∫₀ˣ (cos t − 1)/t dt for `x > 0`.
75///
76/// Returns `-inf` at 0 and `NaN` for negative `x` (where Ci is complex).
77pub fn cosine_integral(x: f64) -> f64 {
78    if x.is_nan() || x < 0.0 {
79        return f64::NAN;
80    }
81    if x.is_infinite() {
82        return 0.0;
83    }
84    si_ci_positive(x).1
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    #[test]
92    fn values() {
93        let cases = [
94            (0.5, -0.177_784_078_806_612_9),
95            (1.0, 0.33740392290096813),
96            (2.0, 0.422_980_828_774_865),
97            (5.0, -0.19002974965664388),
98            (10.0, -0.045_456_433_004_455_37),
99            (100.0, -0.00514882514261049),
100        ];
101        for &(x, want) in &cases[..5] {
102            assert!((cosine_integral(x) - want).abs() < 1e-14, "Ci({x})");
103        }
104        assert!((cosine_integral(cases[5].0) - cases[5].1).abs() < 1e-14);
105    }
106
107    #[test]
108    fn edges() {
109        assert_eq!(cosine_integral(0.0), f64::NEG_INFINITY);
110        assert!(cosine_integral(-1.0).is_nan());
111        assert_eq!(cosine_integral(f64::INFINITY), 0.0);
112        // small-x behaviour Ci(x) ~ gamma + ln x
113        let x = 1e-6f64;
114        assert!((cosine_integral(x) - (0.5772156649015329 + x.ln())).abs() < 1e-12);
115    }
116}