scipy.misc.logsumexp

scipy.misc.logsumexp(a, axis=None)[source]

Compute the log of the sum of exponentials of input elements.

Parameters :

a : array_like

Input array.

axis : int, optional

Axis over which the sum is taken. By default axis is None, and all elements are summed.

Returns :

res : ndarray

The result, np.log(np.sum(np.exp(a))) calculated in a numerically more stable way.

Notes

Numpy has a logaddexp function which is very similar to logsumexp, but only handles two arguments. logaddexp.reduce is similar to this function, but may be less stable.

Examples

>>> from scipy.misc import logsumexp
>>> a = np.arange(10)
>>> np.log(np.sum(np.exp(a)))
9.4586297444267107
>>> logsumexp(a)
9.4586297444267107

Previous topic

scipy.misc.lena

Next topic

scipy.misc.pade