Skip to main content

lib_jsl_special_functions/
polylogarithm.rs

1//! The polylogarithm `Li_s(z) = Σ_{k≥1} z^k / k^s` for real order and real argument.
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-polylogarithm).
4
5use crate::fermi_dirac_integrals::fermi_dirac_integral;
6use crate::gamma::gamma;
7use crate::zeta::zeta;
8
9/// `Li_s(−1) = −η(s) = −(1 − 2^{1−s}) ζ(s)`, valid for every real `s` (with `Li_1(−1) = −ln 2`).
10fn at_minus_one(s: f64) -> f64 {
11    if s == 1.0 {
12        return -std::f64::consts::LN_2;
13    }
14    ((1.0 - s) * std::f64::consts::LN_2).exp_m1() * zeta(s)
15}
16
17fn direct_series(s: f64, z: f64) -> f64 {
18    let mut sum = 0.0;
19    let mut power = 1.0;
20    for k in 1..2000 {
21        power *= z;
22        let term = power / (k as f64).powf(s);
23        sum += term;
24        if term.abs() <= 1e-17 * sum.abs() {
25            break;
26        }
27    }
28    sum
29}
30
31/// The expansion in `μ = ln z` about `z = 1`, valid for `|μ| < 2π` (used for `½ < z < 1`).
32fn log_series(s: f64, z: f64) -> f64 {
33    let mu = z.ln();
34    let integer = s.fract() == 0.0 && s >= 1.0;
35    let n = s as i64;
36    let mut sum = if integer {
37        // μ^{n−1}/(n−1)! (H_{n−1} − ln(−μ)) replaces the singular k = n−1 term.
38        let harmonic: f64 = (1..n).map(|k| 1.0 / k as f64).sum();
39        let factorial: f64 = (1..n).map(|k| k as f64).product();
40        mu.powi((n - 1) as i32) / factorial * (harmonic - (-mu).ln())
41    } else {
42        gamma(1.0 - s) * (-mu).powf(s - 1.0)
43    };
44    let mut power = 1.0; // μ^k / k!
45    for k in 0..60 {
46        if k > 0 {
47            power *= mu / k as f64;
48        }
49        if integer && k == n - 1 {
50            continue;
51        }
52        let term = zeta(s - k as f64) * power;
53        sum += term;
54        // ζ vanishes at the negative even integers, so only stop on a non-zero small term.
55        if k > 2 && term != 0.0 && term.abs() <= 1e-17 * sum.abs() {
56            break;
57        }
58    }
59    sum
60}
61
62/// `Li_{−n}(z) = Σ_{k=0}^{n} k! S(n+1, k+1) w^{k+1}` with `w = z/(1−z)` and Stirling numbers of the
63/// second kind.
64fn negative_integer_order(n: usize, z: f64) -> f64 {
65    let w = z / (1.0 - z);
66    // Row n+1 of the Stirling numbers of the second kind, scaled by k!: a(m, k) = k! S(m, k)
67    // satisfies a(m+1, k) = k (a(m, k) + a(m, k−1)).
68    let mut row = vec![0.0; n + 2];
69    row[1] = 1.0; // m = 1
70    for _ in 1..=n {
71        for k in (1..row.len()).rev() {
72            row[k] = k as f64 * (row[k] + row[k - 1]);
73        }
74    }
75    // row[k] = k! S(n+1, k); the sum needs (k−1)! S(n+1, k) = row[k] / k.
76    (1..=n + 1).map(|k| row[k] / k as f64 * w.powi(k as i32)).sum()
77}
78
79/// The polylogarithm `Li_s(z)` for real `s` and real `z ≤ 1`.
80///
81/// * `|z| ≤ ½`: the defining series.
82/// * `½ < z < 1`: the expansion in `ln z` with zeta coefficients.
83/// * `−1 ≤ z < −½`: the duplication formula `Li_s(z) = 2^{1−s} Li_s(z²) − Li_s(−z)`.
84/// * `z < −1`: the inversion formula for integer `s ≥ 1`, and `Li_s(−e^μ) = −F_{s−1}(μ)` (the
85///   complete Fermi-Dirac integral) for other `s > 0`.
86/// * Integer `s ≤ 1`: the closed forms `−ln(1−z)`, `z/(1−z)` and rational functions.
87///
88/// `Li_s(1) = ζ(s)` for `s > 1` and `+∞` otherwise. Returns NaN for `z > 1` (the value is complex)
89/// and for `z < −1` with non-integer `s ≤ 0`. Orders within about 1e-6 of an integer lose
90/// accuracy in the `ln z` expansion for `½ < z < 1`.
91pub fn polylog(s: f64, z: f64) -> f64 {
92    if s.is_nan() || z.is_nan() || z > 1.0 {
93        return f64::NAN;
94    }
95    if z == 0.0 {
96        return 0.0;
97    }
98    if z == 1.0 {
99        return if s > 1.0 { zeta(s) } else { f64::INFINITY };
100    }
101    let integer = s.fract() == 0.0;
102    if integer && s <= 1.0 {
103        return match s as i64 {
104            1 => -(-z).ln_1p(),
105            0 => z / (1.0 - z),
106            n => negative_integer_order((-n) as usize, z),
107        };
108    }
109    if z == -1.0 {
110        return at_minus_one(s);
111    }
112    if z.abs() <= 0.5 {
113        return direct_series(s, z);
114    }
115    if z > 0.5 {
116        return log_series(s, z);
117    }
118    if z >= -1.0 {
119        return 2f64.powf(1.0 - s) * polylog(s, z * z) - polylog(s, -z);
120    }
121    // z < −1.
122    let l = (-z).ln();
123    if integer {
124        let n = s as i64;
125        let factorial = |m: i64| (1..=m).map(|k| k as f64).product::<f64>();
126        let sign = if n % 2 == 0 { 1.0 } else { -1.0 };
127        let mut value = -sign * polylog(s, 1.0 / z) - l.powi(n as i32) / factorial(n);
128        for k in 1..=n / 2 {
129            value += 2.0 * l.powi((n - 2 * k) as i32) / factorial(n - 2 * k) * at_minus_one((2 * k) as f64);
130        }
131        return value;
132    }
133    if s > 0.0 {
134        return -fermi_dirac_integral(s - 1.0, l);
135    }
136    f64::NAN
137}
138
139#[cfg(test)]
140mod tests {
141    use super::*;
142    use std::f64::consts::{LN_2, PI};
143
144    fn close(a: f64, b: f64, tol: f64) -> bool {
145        (a - b).abs() <= tol * b.abs().max(1e-300)
146    }
147
148    #[test]
149    fn closed_forms_of_integer_orders() {
150        let zeta3 = 1.202_056_903_159_594_3;
151        assert!(close(polylog(2.0, 0.5), PI * PI / 12.0 - LN_2 * LN_2 / 2.0, 1e-15));
152        assert!(close(polylog(2.0, -1.0), -PI * PI / 12.0, 1e-15));
153        assert!(close(polylog(3.0, 0.5), 7.0 / 8.0 * zeta3 - PI * PI / 12.0 * LN_2 + LN_2.powi(3) / 6.0, 1e-14));
154        assert!(close(polylog(2.0, 1.0), PI * PI / 6.0, 1e-15) && polylog(1.0, 1.0).is_infinite());
155        for &z in &[-7.0, -0.9, -0.3, 0.2, 0.75, 0.99] {
156            assert!(close(polylog(1.0, z), -(1.0 - z).ln(), 1e-14));
157            assert!(close(polylog(0.0, z), z / (1.0 - z), 1e-14));
158            assert!(close(polylog(-2.0, z), z * (1.0 + z) / (1.0 - z).powi(3), 1e-13));
159            assert!(close(polylog(-3.0, z), z * (1.0 + 4.0 * z + z * z) / (1.0 - z).powi(4), 1e-13));
160        }
161        // Euler's reflection Li_2(z) + Li_2(1−z) = π²/6 − ln z ln(1−z) and Landen's identity
162        // Li_2(z) + Li_2(z/(z−1)) = −½ ln²(1−z), which reaches z < −1.
163        for &z in &[0.6, 0.9, 0.999] {
164            assert!(close(polylog(2.0, z) + polylog(2.0, 1.0 - z), PI * PI / 6.0 - f64::ln(z) * f64::ln(1.0 - z), 1e-14), "z={z}");
165        }
166        for &z in &[-3.0, -20.0, -0.7] {
167            let w: f64 = z / (z - 1.0);
168            assert!(close(polylog(2.0, z), -0.5 * f64::ln(1.0 - z).powi(2) - polylog(2.0, w), 1e-13), "z={z}");
169        }
170        // The Li_4 inversion formula written out with its constants, Li_2(−1) = −π²/12 and
171        // Li_4(−1) = −7π⁴/720, against the defining series at 1/z.
172        let z = -4.0;
173        let l = f64::ln(4.0);
174        let want = -direct_series(4.0, 1.0 / z) - l.powi(4) / 24.0 - l * l * PI * PI / 12.0 - 7.0 * PI.powi(4) / 360.0;
175        assert!(close(polylog(4.0, z), want, 1e-13));
176    }
177
178    #[test]
179    fn non_integer_orders_match_the_series_and_integral() {
180        // Direct summation is slow but converges for |z| < 1.
181        let brute = |s: f64, z: f64| (1..20_000).map(|k| z.powi(k) / (k as f64).powf(s)).sum::<f64>();
182        for &(s, z) in &[(1.5, 0.7), (0.5, 0.95), (2.5, -0.8), (-0.5, 0.6), (-1.5, -0.65), (3.3, 0.55)] {
183            assert!(close(polylog(s, z), brute(s, z), 1e-12), "s={s} z={z}: {} {}", polylog(s, z), brute(s, z));
184        }
185        // Continuity across the switch between the series and the ln z expansion.
186        assert!(close(polylog(1.5, 0.5 - 1e-12), polylog(1.5, 0.5 + 1e-12), 1e-10));
187        // z < −1 from the integral representation Li_s(z) = z/Γ(s) ∫ t^{s−1}/(e^t − z) dt.
188        let (s, z) = (2.5, -5.0);
189        let m = 200_000;
190        let h = 60.0 / m as f64;
191        let integral: f64 = (0..=m)
192            .map(|i| {
193                let t = i as f64 * h;
194                let w = if i == 0 || i == m { 1.0 } else if i % 2 == 1 { 4.0 } else { 2.0 };
195                w * t.powf(s - 1.0) / (t.exp() - z)
196            })
197            .sum::<f64>()
198            * h
199            / 3.0;
200        assert!(close(polylog(s, z), z / gamma(s) * integral, 1e-9));
201        assert!(polylog(2.0, 1.5).is_nan() && polylog(-0.5, -3.0).is_nan());
202        assert!(close(polylog(0.5, -1.0), -(1.0 - 2f64.sqrt()) * zeta(0.5), 1e-14));
203    }
204}