pxfm/gamma/
gamma_p.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 9/2025. All rights reserved.
3 * //
4 * // Redistribution and use in source and binary forms, with or without modification,
5 * // are permitted provided that the following conditions are met:
6 * //
7 * // 1.  Redistributions of source code must retain the above copyright notice, this
8 * // list of conditions and the following disclaimer.
9 * //
10 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
11 * // this list of conditions and the following disclaimer in the documentation
12 * // and/or other materials provided with the distribution.
13 * //
14 * // 3.  Neither the name of the copyright holder nor the names of its
15 * // contributors may be used to endorse or promote products derived from
16 * // this software without specific prior written permission.
17 * //
18 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29use crate::bessel::i0_exp;
30use crate::double_double::DoubleDouble;
31use crate::gamma::lgamma::lgamma_core;
32use crate::logs::fast_log_d_to_dd;
33
34/// Regularized lower incomplete gamma
35pub fn f_gamma_p(a: f64, x: f64) -> f64 {
36    let aa = a.to_bits();
37    let ax = x.to_bits();
38
39    if aa >= 0x7ffu64 << 52 || aa == 0 || ax >= 0x7ffu64 << 52 || ax == 0 {
40        if (aa >> 63) != 0 || (ax >> 63) != 0 {
41            // |a| < 0 or |b| < 0
42            return f64::NAN;
43        }
44        if aa.wrapping_shl(1) == 0 {
45            // |a| == 0
46            return 1.0;
47        }
48        if ax.wrapping_shl(1) == 0 {
49            // |x| == 0
50            return 0.;
51        }
52        if a.is_infinite() {
53            // |a| == infinity
54            return f64::INFINITY;
55        }
56        if x.is_infinite() {
57            // |x| == infinity
58            return f64::INFINITY;
59        }
60        return a + f64::NAN;
61    }
62
63    let big = 4503599627370496.0;
64    let big_inv = 2.22044604925031308085e-16;
65
66    const EPS: f64 = f64::EPSILON;
67
68    let da = a;
69    let dx = x;
70
71    let r = DoubleDouble::full_add_f64(-lgamma_core(a).0, -dx);
72    let ax = DoubleDouble::mul_f64_add(fast_log_d_to_dd(x), da, r).to_f64();
73
74    if ax <= -709.78271289338399 {
75        if a < x {
76            return 1.0;
77        }
78        return 0.0;
79    }
80    if ax >= 709.783 {
81        return f64::INFINITY;
82    }
83
84    if x <= 1.0 || x <= a {
85        let mut r2 = DoubleDouble::new(0., da);
86        let mut c2 = DoubleDouble::new(0., 1.0);
87        let mut ans2 = DoubleDouble::new(0., 1.0);
88        let v_e = i0_exp(ax);
89        for _ in 0..200 {
90            r2 = DoubleDouble::full_add_f64(r2, 1.0);
91            c2 = DoubleDouble::quick_mult(DoubleDouble::from_f64_div_dd(dx, r2), c2);
92            c2 = DoubleDouble::from_exact_add(c2.hi, c2.lo);
93            ans2 = DoubleDouble::add(ans2, c2);
94
95            if c2.hi / ans2.hi <= EPS {
96                break;
97            }
98        }
99        let v0 = DoubleDouble::quick_mult(v_e, ans2);
100        return DoubleDouble::div_dd_f64(v0, da).to_f64();
101    }
102
103    let v_e = i0_exp(ax);
104
105    let mut y = 1.0 - da;
106    let mut z = dx + y + 1.0;
107    let mut c = 0i32;
108
109    let mut p3 = 1.0;
110    let mut q3 = dx;
111    let mut p2 = dx + 1.0;
112    let mut q2 = z * dx;
113    let mut ans = p2 / q2;
114
115    for _ in 0..200 {
116        y += 1.0;
117        z += 2.0;
118        c += 1;
119        let yc = y * c as f64;
120
121        let p = p2 * z - p3 * yc;
122        let q = q2 * z - q3 * yc;
123
124        p3 = p2;
125        p2 = p;
126        q3 = q2;
127        q2 = q;
128
129        if p.abs() > big {
130            p3 *= big_inv;
131            p2 *= big_inv;
132            q3 *= big_inv;
133            q2 *= big_inv;
134        }
135
136        if q != 0.0 {
137            let nextans = p / q;
138            let error = ((ans - nextans) / nextans).abs();
139            ans = nextans;
140
141            if error <= EPS {
142                break;
143            }
144        }
145    }
146
147    DoubleDouble::mul_f64_add_f64(-v_e, ans, 1.0).to_f64()
148}
149
150#[cfg(test)]
151mod tests {
152    use super::*;
153    #[test]
154    fn test_f_beta_pf() {
155        assert_eq!(f_gamma_p(1., f64::INFINITY), f64::INFINITY);
156        assert_eq!(f_gamma_p(23.421, 41.), 0.9988694746117834);
157        assert_eq!(f_gamma_p(0.764, 0.432123), 0.47752996395412817);
158        assert_eq!(f_gamma_p(0.421, 1.), 0.8727868618082306);
159        assert!(f_gamma_p(-1., 12.).is_nan());
160        assert!(f_gamma_p(1., -12.).is_nan());
161        assert!(f_gamma_p(f64::NAN, 12.).is_nan());
162        assert!(f_gamma_p(1., f64::NAN).is_nan());
163        assert_eq!(f_gamma_p(f64::INFINITY, f64::INFINITY), f64::INFINITY);
164        assert_eq!(f_gamma_p(f64::INFINITY, 5.32), f64::INFINITY);
165    }
166}