Skip to main content

lib_jsl_special_functions/
zeta.rs

1//! The Riemann and Hurwitz zeta functions for real arguments.
2//!
3//! See the book entry [Zeta functions, polylogarithms and Fermi-Dirac integrals](https://jwkunz.github.io/lib_jsl/book/special_functions/zeta.html#the-riemann-zeta-function).
4
5use crate::gamma::gamma;
6use std::f64::consts::{LN_2, PI};
7
8/// Borwein's coefficients for `n` terms of the accelerated alternating series (error about
9/// `3 / (3 + √8)ⁿ`, below 1e-17 for `n = 24`).
10const BORWEIN_TERMS: usize = 24;
11
12fn borwein_d() -> [f64; BORWEIN_TERMS + 1] {
13    let n = BORWEIN_TERMS as f64;
14    let mut d = [0.0; BORWEIN_TERMS + 1];
15    let mut term = 1.0; // n (n+i−1)! 4^i / ((n−i)! (2i)!) at i = 0
16    let mut sum = term;
17    d[0] = sum;
18    for (i, slot) in d.iter_mut().enumerate().skip(1) {
19        let fi = i as f64;
20        term *= 4.0 * (n + fi - 1.0) * (n - fi + 1.0) / ((2.0 * fi) * (2.0 * fi - 1.0));
21        sum += term;
22        *slot = sum;
23    }
24    d
25}
26
27// ANCHOR: eta
28/// The Dirichlet eta function `η(s) = Σ (−1)^{k−1} / k^s` for `s ≥ 0` by Borwein's algorithm.
29fn eta(s: f64) -> f64 {
30    let d = borwein_d();
31    let dn = d[BORWEIN_TERMS];
32    let sum: f64 = (0..BORWEIN_TERMS)
33        .map(|k| {
34            let sign = if k % 2 == 0 { 1.0 } else { -1.0 };
35            sign * (d[k] - dn) / ((k + 1) as f64).powf(s)
36        })
37        .sum();
38    -sum / dn
39}
40// ANCHOR_END: eta
41
42/// The Riemann zeta function `ζ(s)` for real `s ≠ 1` (`+∞` at the pole).
43///
44/// For `s ≥ 0` it is `η(s) / (1 − 2^{1−s})` with Borwein's alternating-series acceleration
45/// (relative error near 1e-15); for `s < 0` the functional equation
46/// `ζ(s) = 2^s π^{s−1} sin(πs/2) Γ(1−s) ζ(1−s)` is used, with exact zeros at the negative even
47/// integers. Overflows to ±∞ below about `s = −170`.
48pub fn zeta(s: f64) -> f64 {
49    if s.is_nan() {
50        return f64::NAN;
51    }
52    if s == 1.0 {
53        return f64::INFINITY;
54    }
55    if s == f64::INFINITY {
56        return 1.0;
57    }
58    if s < 0.0 {
59        if s == f64::NEG_INFINITY {
60            return f64::NAN;
61        }
62        if s.fract() == 0.0 && (s / 2.0).fract() == 0.0 {
63            return 0.0;
64        }
65        return 2f64.powf(s) * PI.powf(s - 1.0) * (PI * s / 2.0).sin() * gamma(1.0 - s) * zeta(1.0 - s);
66    }
67    if s >= 60.0 {
68        return 1.0 + 2f64.powf(-s) + 3f64.powf(-s);
69    }
70    // 1 − 2^{1−s} without cancellation near s = 1.
71    let denominator = -((1.0 - s) * LN_2).exp_m1();
72    eta(s) / denominator
73}
74
75/// The Hurwitz zeta function `ζ(s, a) = Σ_{k≥0} (k + a)^{−s}` for `s > 1` and `a > 0`, by
76/// Euler-Maclaurin summation (relative error near 1e-15). `ζ(s, 1) = ζ(s)`.
77pub fn hurwitz_zeta(s: f64, a: f64) -> f64 {
78    if !(s > 1.0) || !(a > 0.0) {
79        return if s == 1.0 && a > 0.0 { f64::INFINITY } else { f64::NAN };
80    }
81    // B_{2j} / (2j)! for j = 1..8.
82    const B_OVER_FACTORIAL: [f64; 8] = [
83        1.0 / 12.0,
84        -1.0 / 720.0,
85        1.0 / 30240.0,
86        -1.0 / 1_209_600.0,
87        1.0 / 47_900_160.0,
88        -691.0 / 1_307_674_368_000.0,
89        1.0 / 74_724_249_600.0,
90        -3617.0 / 10_670_622_842_880_000.0,
91    ];
92    const N: usize = 16;
93    let head: f64 = (0..N).map(|k| (k as f64 + a).powf(-s)).sum();
94    let x = N as f64 + a;
95    let mut tail = x.powf(1.0 - s) / (s - 1.0) + 0.5 * x.powf(-s);
96    // Rising factorial s(s+1)…(s+2j−2) times x^{−s−2j+1}.
97    let mut factor = s * x.powf(-s - 1.0);
98    for (j, b) in B_OVER_FACTORIAL.iter().enumerate() {
99        let term = b * factor;
100        tail += term;
101        if term.abs() < 1e-17 * (head + tail).abs() {
102            break;
103        }
104        let jf = (j + 1) as f64;
105        factor *= (s + 2.0 * jf - 1.0) * (s + 2.0 * jf) / (x * x);
106    }
107    head + tail
108}
109
110#[cfg(test)]
111mod tests {
112    use super::*;
113
114    fn close(a: f64, b: f64, tol: f64) -> bool {
115        (a - b).abs() <= tol * b.abs().max(1e-300)
116    }
117
118    #[test]
119    fn known_values_of_the_riemann_zeta_function() {
120        assert!(close(zeta(2.0), PI * PI / 6.0, 1e-15));
121        assert!(close(zeta(4.0), PI.powi(4) / 90.0, 1e-15));
122        assert!(close(zeta(3.0), 1.202_056_903_159_594_3, 1e-15));
123        assert!(close(zeta(0.5), -1.460_354_508_809_586_8, 1e-14));
124        assert!(close(zeta(1.5), 2.612_375_348_685_488, 1e-14));
125        let eps = 2f64.powi(-30); // exactly representable offset from the pole
126        assert!(close(zeta(1.0 + eps), 1.0 / eps + 0.577_215_664_901_532_9, 1e-13)); // Laurent expansion
127        assert!(close(zeta(0.0), -0.5, 1e-15));
128        assert!(close(zeta(-1.0), -1.0 / 12.0, 1e-14));
129        assert!(close(zeta(-3.0), 1.0 / 120.0, 1e-13));
130        assert!(close(zeta(-0.5), -0.207_886_224_977_354_57, 1e-13));
131        assert_eq!((zeta(-2.0), zeta(-10.0)), (0.0, 0.0));
132        assert!(close(zeta(80.0), 1.0, 1e-16) && zeta(1.0).is_infinite() && zeta(f64::NAN).is_nan());
133    }
134
135    #[test]
136    fn functional_equation_and_hurwitz_relations() {
137        // The functional equation holds across s = ½ for the non-reflected branch too.
138        for &s in &[0.1, 0.3, 0.7, 0.9] {
139            let reflected = 2f64.powf(s) * PI.powf(s - 1.0) * (PI * s / 2.0).sin() * gamma(1.0 - s) * zeta(1.0 - s);
140            assert!(close(zeta(s), reflected, 1e-13), "s={s}");
141        }
142        for &s in &[1.1, 2.0, 3.5, 7.0, 20.0] {
143            assert!(close(hurwitz_zeta(s, 1.0), zeta(s), 1e-14), "s={s}");
144            // ζ(s, ½) = (2^s − 1) ζ(s), and ζ(s, a) = ζ(s, a + 1) + a^{−s}.
145            assert!(close(hurwitz_zeta(s, 0.5), (2f64.powf(s) - 1.0) * zeta(s), 1e-14));
146            assert!(close(hurwitz_zeta(s, 0.3), hurwitz_zeta(s, 1.3) + 0.3f64.powf(-s), 1e-14));
147        }
148        // Trigamma: ψ₁(1/4) = π² + 8G (Catalan's constant), and ζ(2, a) = ψ₁(a).
149        assert!(close(hurwitz_zeta(2.0, 0.25), PI * PI + 8.0 * 0.915_965_594_177_219, 1e-14));
150        assert!(hurwitz_zeta(0.5, 1.0).is_nan() && hurwitz_zeta(2.0, -1.0).is_nan());
151    }
152}