NumPy logaddexp()

The numpy.logaddexp() function computes the logarithm of the sum of exponentials of the inputs. It is useful in statistics and machine learning where probabilities are stored in log space to avoid underflow.

Syntax

</>
Copy
numpy.logaddexp(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
x1, x2array_likeInput values. If the shapes are different, they must be broadcastable to a common shape.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the function.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array with the result of log(exp(x1) + exp(x2)). If both inputs are scalars, the function returns a scalar.


Examples

1. Basic Usage of logaddexp()

Computing the logarithm of the sum of exponentials for two scalar values.

</>
Copy
import numpy as np

# Define two scalar values
x1 = 2.0
x2 = 3.0

# Compute log(exp(x1) + exp(x2))
result = np.logaddexp(x1, x2)

# Print the result
print("logaddexp(2.0, 3.0):", result)

Output:

logaddexp(2.0, 3.0): 3.313261687518223

2. Using logaddexp() with Arrays

Computing the logarithm of the sum of exponentials element-wise for two arrays.

</>
Copy
import numpy as np

# Define two arrays
x1 = np.array([1, 2, 3])
x2 = np.array([2, 3, 4])

# Compute logaddexp element-wise
result = np.logaddexp(x1, x2)

# Print the results
print("x1:", x1)
print("x2:", x2)
print("logaddexp(x1, x2):", result)

Output:

x1: [1 2 3]
x2: [2 3 4]
logaddexp(x1, x2): [2.31326169 3.31326169 4.31326169]

3. Using the out Parameter

Storing the result in an existing array instead of creating a new one.

</>
Copy
import numpy as np

# Define two arrays
x1 = np.array([0.1, 0.2, 0.3])
x2 = np.array([0.5, 0.6, 0.7])

# Create an output array
output_array = np.empty_like(x1)

# Compute logaddexp and store the result in output_array
np.logaddexp(x1, x2, out=output_array)

# Print the results
print("Computed logaddexp values:", output_array)

Output:

Computed logaddexp values: [1.01301525 1.11301525 1.21301525]

4. Using the where Parameter

Computing logaddexp only for selected elements based on a condition.

</>
Copy
import numpy as np

# Define two arrays
x1 = np.array([1, 2, 3, 4])
x2 = np.array([4, 3, 2, 1])

# Define a mask (compute logaddexp only where mask is True)
mask = np.array([True, False, True, False])

# Compute logaddexp where the mask is True
result = np.logaddexp(x1, x2, where=mask)

# Print the results
print("Computed logaddexp values with mask:", result)

Output:

Computed logaddexp values with mask: [4.04858735 0.         3.31326169 0.        ]

The function computes logaddexp only for elements where mask=True. The remaining values retain their original value.