def lscsum(lx, epsilon=None):
lx = numpy.asarray(lx)<EOL>base = lx.max()<EOL>if numpy.isinf(base):<EOL><INDENT>return base<EOL><DEDENT>if (epsilon is not None) and (base < epsilon):<EOL><INDENT>return epsilon<EOL><DEDENT>x = numpy.exp(lx - base)<EOL>ssum = x.sum()<EOL>result = numpy.log(ssum) + base<EOL>return result<EOL>
Accepts log-values as input, exponentiates them, computes the sum,
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.