Skip to content

Commit 094b2de

Browse files
new implementation of logaddexp (#41)
* new implementation of logaddexp * Add threshold for zero results in `log1pexp` * changed type promotion to pass the new tests * clarified logaddexp signature * relaxed signature type to Real * removed inaccurate threshold for logaddexp * removed internal function _logaddexp * Update src/basicfuns.jl Looks good to me as well. Co-authored-by: David Widmann <[email protected]> Co-authored-by: David Widmann <[email protected]> Co-authored-by: David Widmann <[email protected]>
1 parent 6709eda commit 094b2de

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/basicfuns.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,18 @@ Return `log(exp(x) + exp(y))`, avoiding intermediate overflow/undeflow, and hand
312312
non-finite values.
313313
"""
314314
function logaddexp(x::Real, y::Real)
315-
# ensure Δ = 0 if x = y = ± Inf
316-
Δ = x == y ? zero(x - y) : abs(x - y)
317-
max(x, y) + log1pexp(-Δ)
315+
# Compute max = Base.max(x, y) and diff = x == y ? zero(x - y) : -abs(x - y)
316+
# in a faster type-stable way
317+
a, b = promote(x, y)
318+
if a < b
319+
diff = a - b
320+
max = b
321+
else
322+
# ensure diff = 0 if a = b = ± Inf
323+
diff = a == b ? zero(a - b) : b - a
324+
max = !isnan(b) ? a : b
325+
end
326+
return max + log1pexp(diff)
318327
end
319328

320329
Base.@deprecate logsumexp(x::Real, y::Real) logaddexp(x, y)

0 commit comments

Comments
 (0)