pxfm/gamma/beta.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::double_double::DoubleDouble;
30use crate::f_exp;
31use crate::gamma::lgamma::lgamma_core;
32
33/// Computes beta function
34pub fn f_beta(a: f64, b: f64) -> f64 {
35 let ax = a.to_bits();
36 let bx = b.to_bits();
37
38 if ax >= 0x7ffu64 << 52 || ax == 0 || bx >= 0x7ffu64 << 52 || bx == 0 {
39 if (ax >> 63) != 0 || (bx >> 63) != 0 {
40 // |a| < 0 or |b| < 0
41 return f64::NAN;
42 }
43 if ax.wrapping_shl(1) == 0 || bx.wrapping_shl(1) == 0 {
44 // |a| == 0 || |b| == 0
45 if ax.wrapping_shl(1) != 0 || bx.wrapping_shl(1) != 0 {
46 return f64::INFINITY;
47 }
48 return f64::NAN;
49 }
50 if a.is_infinite() || b.is_infinite() {
51 // |a| == inf or |b| == inf
52 return 0.;
53 }
54 return a + f64::NAN; // nan
55 }
56
57 let mut sign = 1i32;
58 let (mut y, sgngamf) = lgamma_core(DoubleDouble::from_full_exact_add(a, b).to_f64());
59 sign *= sgngamf; /* keep track of the sign */
60 let (y1, sgngamf) = lgamma_core(b);
61 y = DoubleDouble::quick_dd_sub(y1, y);
62 sign *= sgngamf;
63 let (y1, sgngamf) = lgamma_core(a);
64 y = DoubleDouble::quick_dd_add(y1, y);
65 sign *= sgngamf;
66 f_exp(y.to_f64()) * (sign as f64)
67}
68
69#[cfg(test)]
70mod tests {
71 use crate::f_beta;
72
73 #[test]
74 fn test_beta() {
75 assert!(f_beta(-5., 15.).is_nan());
76 assert!(f_beta(5., -15.).is_nan());
77 assert!(f_beta(f64::NAN, 15.).is_nan());
78 assert!(f_beta(15., f64::NAN).is_nan());
79 assert_eq!(f_beta(f64::INFINITY, 0.), f64::INFINITY);
80 assert_eq!(f_beta(f64::INFINITY, 1.), 0.);
81 assert_eq!(f_beta(1., f64::INFINITY), 0.);
82 assert_eq!(f_beta(5., 3.), 0.009523809523809526);
83 assert_eq!(f_beta(3., 5.), 0.009523809523809526);
84 assert_eq!(f_beta(12., 23.), 1.5196994506201402e-10);
85 }
86}