{ } Raw JSON

bundles / scipy latest / scipy / special / _logsumexp / logsumexp

function

scipy.special._logsumexp:logsumexp

source: /scipy/special/_logsumexp.py :15

Signature

def   logsumexp ( a axis = None b = None keepdims = False return_sign = False )

Summary

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.

b : array-like, optional

Scaling factor for exp(a) must be of the same shape as a or broadcastable to a. These values may be negative in order to implement subtraction.

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.

return_sign : bool, optional

If this is set to True, the result will be a pair containing sign information; if False, results that are negative will be returned as NaN. Default is False (no sign information).

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. If return_sign is True, res contains the log of the absolute value of the argument.

sgn : ndarray

If return_sign is True, this will be an array of floating-point numbers matching res containing +1, 0, -1 (for real-valued inputs) or a complex phase (for complex inputs). This gives the sign of the argument of the logarithm in res. If return_sign is False, only one result 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.

The logarithm is a multivalued function: for each there is an infinite number of such that . The convention is to return the whose imaginary part lies in .

Array API Standard Support

logsumexp has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.

====================  ====================  ====================
Library               CPU                   GPU
====================  ====================  ====================
NumPy                 ✅                     n/a                 
CuPy                  n/a                   ✅                   
PyTorch               ✅                     ✅                   
JAX                   ✅                     ✅                   
Dask                  ✅                     n/a                 
====================  ====================  ====================

See dev-arrayapi for more information.

Examples

import numpy as np
from scipy.special import logsumexp
a = np.arange(10)
logsumexp(a)
np.log(np.sum(np.exp(a)))
With weights
a = np.arange(10)
b = np.arange(10, 0, -1)
logsumexp(a, b=b)
np.log(np.sum(b*np.exp(a)))
Returning a sign flag
logsumexp([1,2],b=[1,-1],return_sign=True)
Notice that `logsumexp` does not directly support masked arrays. To use it on a masked array, convert the mask into zero weights:
a = np.ma.array([np.log(2), 2, np.log(3)],
                 mask=[False, True, False])
b = (~a.mask).astype(int)
logsumexp(a.data, b=b), np.log(5)

See also

numpy.logaddexp
numpy.logaddexp2

Aliases

  • scipy.special.logsumexp

Referenced by

This package