pub fn log_sum_exp(values: &[f64]) -> f64Expand description
Log-sum-exp: numerically stable computation of ln(exp(a_1) + ... + exp(a_n)).
This is the fundamental primitive for working in log-probability space.
The naive values.iter().map(|v| v.exp()).sum::<f64>().ln() overflows
for large values and underflows for small ones; the max-shift trick
avoids both.
Returns NEG_INFINITY for an empty slice.
§Examples
// ln(e^0 + e^0) = ln(2)
let lse = log_sum_exp(&[0.0, 0.0]);
assert!((lse - 2.0_f64.ln()).abs() < 1e-12);
// Dominated term: ln(e^1000 + e^0) ≈ 1000
let lse = log_sum_exp(&[1000.0, 0.0]);
assert!((lse - 1000.0).abs() < 1e-10);
// Single element: identity.
assert_eq!(log_sum_exp(&[42.0]), 42.0);
// Empty: -inf.
assert_eq!(log_sum_exp(&[]), f64::NEG_INFINITY);