pub fn signed_log_sum_exp(log_mags: &[f64], signs: &[f64]) -> (f64, f64)Expand description
Numerically stable signed log-sum-exp. Given pairs
(log|aⱼ|, sign(aⱼ)) (with signs[j] ∈ {−1, 0, +1}), returns
(log|S|, sign(S)) for S = Σⱼ signs[j]·exp(log_mags[j]). Positive
and negative magnitudes are reduced separately with the standard
log-sum-exp trick (subtract the max, sum, log, add back); the two
partial sums are then combined via log(|p − n|) = max(log p, log n) + log1mexp(|log p − log n|), preserving accuracy
even when p ≈ n (catastrophic cancellation regime). When all
signs are zero or all magnitudes are −∞, returns
(NEG_INFINITY, 0.0).
A +∞ log-magnitude denotes an infinite-magnitude term (exp(+∞) = +∞)
and dominates the sum: if it appears only with positive sign the result
is (+∞, +1); only with negative sign, (+∞, −1) (a log-magnitude of
+∞ with sign −1 encodes the value −∞); with both signs the sum is
the indeterminate +∞ − ∞, returned as (NaN, 0.0). A −∞
log-magnitude is exp(−∞) = 0 and is correctly dropped.