def log_add(log_x, log_y):
"""Given log x and log y, returns log(x + y)."""
# Swap variables so log_y is larger.
if log_x > log_y:
log_x, log_y = log_y, log_x
# Use the log(1 + e^p) trick to compute this efficiently
# If the difference is large enough, this is effectively log y.
delta = log_y - log_x
return math.log1p(math.exp(delta)) + log_x if delta <= 50.0 else log_y
评论列表
文章目录