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