Skip to main content

xsf/numpy/
npymath.rs

1//! <https://github.com/numpy/numpy/blob/4992eaf/numpy/_core/src/npymath/npy_math_internal.h.src>
2
3use core::f64::consts::{LN_2, LOG2_E};
4
5pub trait LogAddExpArg: crate::sealed::Sealed {
6    const ZERO: Self;
7    fn npy_logaddexp(self, other: Self) -> Self;
8    fn npy_logaddexp2(self, other: Self) -> Self;
9}
10
11impl LogAddExpArg for f32 {
12    const ZERO: Self = 0.0;
13
14    #[inline]
15    fn npy_logaddexp(self, other: Self) -> Self {
16        f64::from(self).npy_logaddexp(f64::from(other)) as f32
17    }
18
19    #[inline]
20    fn npy_logaddexp2(self, other: Self) -> Self {
21        f64::from(self).npy_logaddexp2(f64::from(other)) as f32
22    }
23}
24
25impl LogAddExpArg for f64 {
26    const ZERO: Self = 0.0;
27
28    #[inline]
29    fn npy_logaddexp(self, other: Self) -> Self {
30        if self == other {
31            // Handles infinities of the same sign without warnings
32            self + LN_2
33        } else {
34            let tmp = self - other;
35            if tmp > 0.0 {
36                self + (-tmp).exp().ln_1p()
37            } else if tmp <= 0.0 {
38                other + tmp.exp().ln_1p()
39            } else {
40                // NaNs
41                tmp
42            }
43        }
44    }
45
46    #[inline]
47    fn npy_logaddexp2(self, other: Self) -> Self {
48        if self == other {
49            // Handles infinities of the same sign without warnings
50            self + 1.0
51        } else {
52            let tmp = self - other;
53            if tmp > 0.0 {
54                self + (-tmp).exp2().ln_1p() * LOG2_E
55            } else if tmp <= 0.0 {
56                other + tmp.exp2().ln_1p() * LOG2_E
57            } else {
58                // NaNs
59                tmp
60            }
61        }
62    }
63}
64
65/// $\ln(e^x + e^y)$
66///
67/// This function is useful in statistics where the calculated probabilities of events may be so
68/// small as to exceed the range of normal floating point numbers. In such cases the logarithm of
69/// the calculated probability is stored. This function allows adding probabilities stored in such
70/// a fashion.
71///
72/// # See also
73/// - [`logaddexp2`]
74/// - [NumPy documentation](https://numpy.org/doc/stable/reference/generated/numpy.logaddexp.html)
75///
76#[inline]
77pub fn logaddexp<T: LogAddExpArg>(x: T, y: T) -> T {
78    x.npy_logaddexp(y)
79}
80
81/// $\log_2(2^x + 2^y)$
82///
83/// This function is useful in machine learning when the calculated probabilities of events may be
84/// so small as to exceed the range of normal floating point numbers. In such cases the base-2
85/// logarithm of the calculated probability can be used instead. This function allows adding
86/// probabilities stored in such a fashion.
87///
88/// # See also
89/// - [`logaddexp`]
90/// - [NumPy documentation](https://numpy.org/doc/stable/reference/generated/numpy.logaddexp2.html)
91///
92#[inline]
93pub fn logaddexp2<T: LogAddExpArg>(x: T, y: T) -> T {
94    x.npy_logaddexp2(y)
95}