NumPy expm1()

The numpy.expm1() function computes exp(x) - 1 for each element in an input array. This function is useful for numerical accuracy when computing small values of x.

Syntax

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

Parameters

ParameterTypeDescription
xarray_likeInput values for which exp(x) - 1 is computed.
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 element-wise computed value of exp(x) - 1. If the input is a scalar, a scalar is returned.


Examples

1. Computing expm1 for a Single Value

Here, we compute exp(x) - 1 for a single scalar value.

</>
Copy
import numpy as np

# Define an input value
x = 0.001  

# Compute exp(x) - 1
result = np.expm1(x)

# Print the result
print("expm1(0.001):", result)

Output:

expm1(0.001): 0.0010005001667083417

2. Computing expm1 for an Array of Values

We compute expm1(x) for multiple values in an array.

</>
Copy
import numpy as np

# Define an array of input values
x_values = np.array([-1, 0, 0.1, 0.5, 1])

# Compute exp(x) - 1 for each value
result = np.expm1(x_values)

# Print the results
print("Input values:", x_values)
print("expm1 values:", result)

Output:

Input values: [-1.   0.   0.1  0.5  1. ]
expm1 values: [-0.63212056  0.          0.10517092  0.64872127  1.71828183]

3. Using the out Parameter

Using an output array to store results instead of creating a new array.

</>
Copy
import numpy as np

# Define an array of input values
x_values = np.array([-0.5, 0.2, 0.8])

# Create an output array with the same shape
output_array = np.empty_like(x_values)

# Compute exp(x) - 1 and store the result in output_array
np.expm1(x_values, out=output_array)

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

Output:

Computed expm1 values: [-0.39346934  0.22140276  1.22554093]

4. Using the where Parameter

Using a condition to compute expm1(x) only for selected elements.

</>
Copy
import numpy as np

# Define an array of input values
x_values = np.array([-1, 0, 0.5, 1])

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

# Compute exp(x) - 1 where mask is True
result = np.expm1(x_values, where=mask)

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

Output:

Computed expm1 values with mask: [-0.63212056  0.          0.64872127  0.        ]

The expm1 values are computed only for elements where mask=True. The other values remain unchanged.