Python – scipy.special.logsumexp


The scipy.special package contains a set of different functionalities that are used for mathematical physics. One of them is the logsumexp() function. This function is used to compute the log of the sum of exponentials of input elements. Let us take a couple of examples and see how to use this function.

Syntax

scipy.special.logsumexp(x)

where, x is the input value.

Example 1

Let us consider the following example −

# Import logsumexp from scipy.special
from scipy.special import logsumexp
import numpy as np

# Input array
a = np.arange(10)
print("Input Array:
", a) # logsum() function res = logsumexp(a) print("logsumexp of a:", res)

Output

It will produce the following output −

Input Array:
[0 1 2 3 4 5 6 7 8 9]
logsumexp of a: 9.45862974442671

Example 2

Let us take another example −

# Import logsumexp from scipy.special
from scipy.special import logsumexp

# logsumexp function
res = logsumexp(a=[5,6], b=[4,-2], return_sign=True)

# Display the logsumexp values
print("Logsumexp values are :", res)

Here, return_sign is an optional parameter. If it is True, then the result will be a pair containing sign information. If return_sign=False, then the results that are negative will be returned as NaN. Default is False.

Output

The above program will generate the following output −

Logsumexp values are : (5.36225391235589, -1.0)

Updated on: 22-Dec-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements