pub fn logaddexp<F: Float>(
a: impl IntoArray<F>,
b: impl IntoArray<F>,
) -> Array<F>Expand description
Equivalent of np.logaddexp(a, b): log(exp(a) + exp(b)) without overflow.
use rustnum as np;
let a = np::array![0.0, 1.0, f64::NEG_INFINITY];
let r = np::logaddexp(&a, &a).to_vec();
assert_eq!(r, [std::f64::consts::LN_2, 1.0 + std::f64::consts::LN_2, f64::NEG_INFINITY]);
// adding probabilities that would underflow as plain numbers
let s = np::logaddexp(1e-50f64.ln(), 2.5e-50f64.ln()).item();
assert!((s - 3.5e-50f64.ln()).abs() < 1e-12);