pxfm/gamma/
gamma_qf.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::common::f_fmla;
30use crate::exponents::core_expdf;
31use crate::gamma::gamma_pf::core_gamma_pf;
32use crate::gamma::lgamma_rf::lgamma_coref;
33use crate::logs::fast_logf;
34
35/// Regularized upper incomplete gamma
36pub fn f_gamma_qf(a: f32, x: f32) -> f32 {
37    let aa = a.to_bits();
38    let ax = x.to_bits();
39
40    if aa >= 0xffu32 << 23 || aa == 0 || ax >= 0xffu32 << 23 || ax == 0 {
41        if (aa >> 31) != 0 || (ax >> 31) != 0 {
42            // |a| < 0 or |b| < 0
43            return f32::NAN;
44        }
45        if aa.wrapping_shl(1) == 0 {
46            // |a| == 0
47            return 1.0;
48        }
49        if ax.wrapping_shl(1) == 0 {
50            // |x| == 0
51            return 0.;
52        }
53        if a.is_infinite() {
54            // |a| == infinity
55            return f32::INFINITY;
56        }
57        if x.is_infinite() {
58            // |x| == infinity
59            return f32::INFINITY;
60        }
61        return a + f32::NAN;
62    }
63
64    const EPS: f64 = 1e-9;
65
66    const BIG: f64 = 4503599627370496.0;
67    const BIG_INV: f64 = 2.22044604925031308085e-16;
68
69    if x < 1.0 || x <= a {
70        return (1.0 - core_gamma_pf(a, x)) as f32;
71    }
72
73    let da = a as f64;
74    let dx = x as f64;
75
76    let ax = f_fmla(da, fast_logf(x), -dx - lgamma_coref(a).0);
77    if ax <= -104. {
78        if a < x {
79            return 0.0;
80        }
81        return 1.0;
82    }
83    if ax >= 89. {
84        return f32::INFINITY;
85    }
86
87    let mut y = 1.0 - da;
88    let mut z = dx + y + 1.0;
89    let mut c = 0.0;
90    let mut pkm2 = 1.0;
91    let mut qkm2 = dx;
92    let mut pkm1 = dx + 1.0;
93    let mut qkm1 = z * dx;
94    let mut ans = pkm1 / qkm1;
95    for _ in 0..200 {
96        y += 1.0;
97        z += 2.0;
98        c += 1.0;
99        let yc = y * c;
100        let pk = pkm1 * z - pkm2 * yc;
101        let qk = qkm1 * z - qkm2 * yc;
102
103        pkm2 = pkm1;
104        pkm1 = pk;
105        qkm2 = qkm1;
106        qkm1 = qk;
107
108        if pk.abs() > BIG {
109            pkm2 *= BIG_INV;
110            pkm1 *= BIG_INV;
111            qkm2 *= BIG_INV;
112            qkm1 *= BIG_INV;
113        }
114
115        if qk != 0.0 {
116            let r = pk / qk;
117            let t = ((ans - r) / r).abs();
118            ans = r;
119
120            if t <= EPS {
121                break;
122            }
123        }
124    }
125    (ans * core_expdf(ax)) as f32
126}
127
128#[cfg(test)]
129mod tests {
130    use super::*;
131    #[test]
132    fn test_f_beta_qf() {
133        assert_eq!(f_gamma_qf(23.421, 41.), 0.001130525);
134        assert_eq!(f_gamma_qf(0.764, 0.432123), 0.52247006);
135        assert_eq!(f_gamma_qf(0.421, 1.), 0.12721314);
136        assert!(f_gamma_qf(-1., 12.).is_nan());
137        assert!(f_gamma_qf(1., -12.).is_nan());
138        assert!(f_gamma_qf(f32::NAN, 12.).is_nan());
139        assert!(f_gamma_qf(1., f32::NAN).is_nan());
140        assert_eq!(f_gamma_qf(1., f32::INFINITY), f32::INFINITY);
141        assert_eq!(f_gamma_qf(f32::INFINITY, f32::INFINITY), f32::INFINITY);
142        assert_eq!(f_gamma_qf(f32::INFINITY, 5.32), f32::INFINITY);
143    }
144}