NumPy logaddexp2()
The numpy.logaddexp2() function computes the logarithm of the sum of exponentiations of the inputs in base-2. It is particularly useful in probability calculations where probabilities are represented in log-space to prevent underflow issues.
Syntax
</>
                        Copy
                        numpy.logaddexp2(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)Parameters
| Parameter | Type | Description | 
|---|---|---|
| x1, x2 | array_like | Input values. If their shapes do not match, they must be broadcastable to a common shape. | 
| out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. | 
| where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=Falseretain their original value. | 
| casting | str, optional | Defines the casting behavior when computing the function. | 
| order | str, optional | Memory layout order of the output array. | 
| dtype | data-type, optional | Defines the data type of the output array. | 
| subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. | 
Return Value
Returns an array containing the base-2 logarithm of 2**x1 + 2**x2. If both x1 and x2 are scalars, a scalar is returned.
Examples
1. Computing logaddexp2 for Two Scalar Values
Computing the base-2 logarithm of the sum of exponentiations for two scalar values.
</>
                        Copy
                        import numpy as np
# Define two scalar values
x1 = 3
x2 = 4
# Compute logaddexp2
result = np.logaddexp2(x1, x2)
# Print the result
print("logaddexp2(3, 4):", result)Output:
logaddexp2(3, 4): 4.584962500721156
2. Applying logaddexp2 to Arrays
Computing the element-wise logaddexp2 for two arrays.
</>
                        Copy
                        import numpy as np
# Define two arrays
x1 = np.array([1, 2, 3])
x2 = np.array([3, 2, 1])
# Compute logaddexp2 element-wise
result = np.logaddexp2(x1, x2)
# Print the results
print("x1:", x1)
print("x2:", x2)
print("logaddexp2(x1, x2):", result)Output:
x1: [1 2 3]
x2: [3 2 1]
logaddexp2(x1, x2): [3.32192809 3.         3.32192809]
3. Using the out Parameter
Storing the result of logaddexp2 in a pre-allocated output array.
</>
                        Copy
                        import numpy as np
# Define two arrays
x1 = np.array([0, 1, 2])
x2 = np.array([2, 1, 0])
# Create an output array with the same shape
output_array = np.ndarray(x1.shape)
# Compute logaddexp2 and store it in output_array
np.logaddexp2(x1, x2, out=output_array)
# Print the results
print("Stored result in output_array:", output_array)Output:
Stored result in output_array: [2.32192809 2.         2.32192809]
4. Using the where Parameter
Applying a condition to compute logaddexp2 only for specific elements.
</>
                        Copy
                        import numpy as np
# Define two arrays
x1 = np.array([0, 2, 4, 6])
x2 = np.array([4, 2, 0, -2])
# Define a condition mask (only compute where mask is True)
mask = np.array([True, False, True, True])
# Compute logaddexp2 where mask is True
result = np.logaddexp2(x1, x2, where=mask)
# Print the results
print("Computed logaddexp2 with mask:", result)Output:
Computed logaddexp2 with mask: [4.08746284 0.         4.08746284 6.00562455]
The function is computed only for elements where mask=True. Other elements retain their original values.
