SciPy

scipy.misc.logsumexp

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

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

Parameters:

a : array_like

Input array.

axis : None or int or tuple of ints, optional

Axis or axes over which the sum is taken. By default axis is None, and all elements are summed. Tuple of ints is not accepted if NumPy version is lower than 1.7.0.

New in version 0.11.0.

keepdims: bool, optional

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original array.

New in version 0.15.0.

b : array-like, optional

Scaling factor for exp(a) must be of the same shape as a or broadcastable to a.

New in version 0.12.0.

Returns:

res : ndarray

The result, np.log(np.sum(np.exp(a))) calculated in a numerically more stable way. If b is given then np.log(np.sum(b*np.exp(a))) is returned.

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

With weights

>>> a = np.arange(10)
>>> b = np.arange(10, 0, -1)
>>> logsumexp(a, b=b)
9.9170178533034665
>>> np.log(np.sum(b*np.exp(a)))
9.9170178533034647

Previous topic

scipy.misc.lena

Next topic

scipy.misc.pade