Skip to main content

lib_jsl_special_functions/
fermi_dirac_integrals.rs

1//! Fermi-Dirac integrals.
2//!
3//! See the book entry [Zeta functions, polylogarithms and Fermi-Dirac integrals](https://jwkunz.github.io/lib_jsl/book/special_functions/zeta.html#fermi-dirac-integrals).
4
5use std::sync::OnceLock;
6
7use crate::gamma::gamma;
8
9const GL_ORDER: usize = 20;
10
11#[allow(clippy::needless_range_loop)]
12/// Gauss-Legendre nodes and weights on [-1, 1], computed by Newton iteration.
13fn gauss_legendre() -> &'static [(f64, f64); GL_ORDER] {
14    static NODES: OnceLock<[(f64, f64); GL_ORDER]> = OnceLock::new();
15    NODES.get_or_init(|| {
16        let n = GL_ORDER;
17        let mut out = [(0.0, 0.0); GL_ORDER];
18        for i in 0..n {
19            let mut x = (std::f64::consts::PI * (i as f64 + 0.75) / (n as f64 + 0.5)).cos();
20            let mut dp = 1.0;
21            for _ in 0..100 {
22                let (mut p0, mut p1) = (1.0, x);
23                for k in 2..=n {
24                    let kf = k as f64;
25                    let p2 = ((2.0 * kf - 1.0) * x * p1 - (kf - 1.0) * p0) / kf;
26                    p0 = p1;
27                    p1 = p2;
28                }
29                dp = n as f64 * (x * p1 - p0) / (x * x - 1.0);
30                let dx = p1 / dp;
31                x -= dx;
32                if dx.abs() < 1e-16 {
33                    break;
34                }
35            }
36            out[i] = (x, 2.0 / ((1.0 - x * x) * dp * dp));
37        }
38        out
39    })
40}
41
42/// Chooses the exponent `k` of the substitution `t = u^k` that removes the endpoint singularity
43/// of `t^j` (smooth integrand when `k (j + 1)` is an integer >= 1).
44fn substitution_power(j: f64) -> u32 {
45    for k in 1..=12u32 {
46        let e = k as f64 * (j + 1.0);
47        if (e - e.round()).abs() < 1e-9 && e.round() >= 1.0 {
48            return k;
49        }
50    }
51    ((3.0 / (j + 1.0)).ceil() as u32).clamp(1, 400)
52}
53
54/// The complete Fermi-Dirac integral of real order `j > -1`,
55/// `F_j(x) = 1/Γ(j+1) ∫₀^∞ t^j / (1 + e^(t-x)) dt`.
56///
57/// Evaluated by composite 20-point Gauss-Legendre quadrature after a substitution that removes the
58/// endpoint singularity. Accuracy is about 1e-13 for orders where `k(j+1)` is an integer for a small
59/// `k` (all integer and half-integer orders) and degrades gradually otherwise (and as `j → -1`).
60/// Returns `NaN` for `j <= -1`.
61pub fn fermi_dirac_integral(j: f64, x: f64) -> f64 {
62    if j.is_nan() || x.is_nan() || j <= -1.0 {
63        return f64::NAN;
64    }
65    if x == f64::NEG_INFINITY {
66        return 0.0;
67    }
68    if x == f64::INFINITY {
69        return f64::INFINITY;
70    }
71    if j == 0.0 {
72        // F_0(x) = ln(1 + e^x), exact.
73        return if x > 0.0 { x + (-x).exp().ln_1p() } else { x.exp().ln_1p() };
74    }
75    let k = substitution_power(j);
76    let kf = k as f64;
77    let top = x.max(0.0) + 45.0;
78    let umax = top.powf(1.0 / kf);
79    let panels = ((top / 1.5).ceil() as usize * k.min(20) as usize).clamp(32, 20_000);
80    let h = umax / panels as f64;
81    let nodes = gauss_legendre();
82    let expo = kf * (j + 1.0) - 1.0;
83    let mut sum = 0.0;
84    for p in 0..panels {
85        let a = p as f64 * h;
86        let mid = a + 0.5 * h;
87        for &(xn, w) in nodes.iter() {
88            let u = mid + 0.5 * h * xn;
89            let t = u.powf(kf);
90            let z = t - x;
91            let f = if z > 700.0 { 0.0 } else { 1.0 / (1.0 + z.exp()) };
92            sum += w * kf * u.powf(expo) * f;
93        }
94    }
95    sum * 0.5 * h / gamma(j + 1.0)
96}
97
98#[cfg(test)]
99mod tests {
100    use super::*;
101
102    fn close(a: f64, b: f64, rel: f64) -> bool {
103        (a - b).abs() <= rel * b.abs()
104    }
105
106    #[test]
107    fn order_zero_exact() {
108        assert!(close(fermi_dirac_integral(0.0, 3.0), 3.048587351573742, 1e-15));
109        assert!(close(fermi_dirac_integral(0.0, -40.0), (-40.0f64).exp(), 1e-15));
110    }
111
112    #[test]
113    fn integer_orders() {
114        assert!(close(fermi_dirac_integral(1.0, 0.0), std::f64::consts::PI.powi(2) / 12.0, 1e-13));
115        assert!(close(fermi_dirac_integral(2.0, 0.0), 0.9015426773696957, 1e-13));
116        assert!(close(fermi_dirac_integral(1.0, 30.0), 451.644_934_066_848_1, 1e-13));
117        assert!(close(fermi_dirac_integral(3.0, -0.5), 0.585866475325089, 1e-13));
118    }
119
120    #[test]
121    fn half_integer_orders() {
122        assert!(close(fermi_dirac_integral(0.5, 1.0), 1.5756407761513002, 1e-12));
123        assert!(close(fermi_dirac_integral(0.5, -2.0), 0.12929851332007559, 1e-12));
124        assert!(close(fermi_dirac_integral(-0.5, 0.3), 0.7238460148294637, 1e-12));
125        assert!(close(fermi_dirac_integral(1.5, 5.0), 20.914_467_402_762_63, 1e-12));
126        assert!(close(fermi_dirac_integral(-0.5, -1.0), 0.29402761761145122, 1e-12));
127        assert!(close(fermi_dirac_integral(0.5, 100.0), 752.3455915521961, 1e-12));
128    }
129
130    #[test]
131    fn limits_and_invalid() {
132        assert_eq!(fermi_dirac_integral(1.0, f64::NEG_INFINITY), 0.0);
133        assert!(fermi_dirac_integral(-1.0, 0.0).is_nan());
134        // Non-degenerate limit F_j(x) ~ e^x for x -> -inf
135        assert!(close(fermi_dirac_integral(1.3, -20.0), (-20.0f64).exp(), 1e-6));
136    }
137}