def lnsum0(lx):
lx = numpy.asarray(lx)<EOL>base = lx.max()<EOL>x = numpy.exp(lx - base)<EOL>ssum = x.sum(<NUM_LIT:0>)<EOL>normalized = nsum0(lx)<EOL>result = numpy.log(normalized)<EOL>conventional = numpy.log(numpy.exp(lx).sum(<NUM_LIT:0>) / numpy.exp(lx).sum())<EOL>assert similar(result, conventional)<EOL>return result<EOL>
Accepts log-values as input, exponentiates them, sums down the rows
(first dimension), normalizes, then converts the sum back to
log-space and returns the result.
Handles underflow by rescaling so that the largest values is exactly 1.0.