cccc996Use Nawaf Bou-Rabee's reference WALNUTS implementationJeremy Magland 1function s = logsumexp(x)
2%LOGSUMEXP Numerically stable log(sum(exp(x))) over all elements of x.
3m = max(x(:));
4if ~isfinite(m)
5 s = m; % all -Inf -> -Inf (and +Inf -> +Inf); avoids Inf-Inf = NaN
6 return
7end
8s = m + log(sum(exp(x(:) - m)));
9end