NumPy ldexp()

The numpy.ldexp() function computes the value of x1 * 2**x2 for each element in the input arrays. It is useful for constructing floating-point numbers using a mantissa (x1) and an exponent (x2).

Syntax

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

Parameters

ParameterTypeDescription
x1array_likeArray of multipliers (mantissas).
x2array_like, intArray of two’s exponents. If x1.shape != x2.shape, 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 or scalar containing the computed values of x1 * 2**x2. If both x1 and x2 are scalars, the result is a scalar.


Examples

1. Computing ldexp for Single Values

Here, we compute ldexp for a single mantissa and exponent.

</>
Copy
import numpy as np

# Define a mantissa and an exponent
mantissa = 3.5
exponent = 2

# Compute the ldexp result
result = np.ldexp(mantissa, exponent)

# Print the result
print("3.5 * 2**2 =", result)

Output:

3.5 * 2**2 = 14.0

2. Computing ldexp for Arrays

We compute ldexp for multiple mantissas and exponents using arrays.

</>
Copy
import numpy as np

# Define arrays of mantissas and exponents
mantissas = np.array([1.0, 2.0, 3.0])
exponents = np.array([2, 3, 4])

# Compute the ldexp result element-wise
result = np.ldexp(mantissas, exponents)

# Print the results
print("Mantissas:", mantissas)
print("Exponents:", exponents)
print("Result of ldexp:", result)

Output:

Mantissas: [1. 2. 3.]
Exponents: [2 3 4]
Result of ldexp: [ 4. 16. 48.]

3. Using the out Parameter

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

</>
Copy
import numpy as np

# Define mantissas and exponents
mantissas = np.array([0.5, 1.5, 2.5])
exponents = np.array([1, 2, 3])

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

# Compute ldexp and store the result in output_array
np.ldexp(mantissas, exponents, out=output_array)

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

Output:

Computed ldexp values: [1.  6.  20.]

4. Using the where Parameter

Using a condition to compute ldexp only for selected elements.

</>
Copy
import numpy as np

# Define mantissas and exponents
mantissas = np.array([2.0, 3.0, 4.0])
exponents = np.array([2, 3, 4])

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

# Compute ldexp values where mask is True
result = np.ldexp(mantissas, exponents, where=mask)

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

Output:

Computed ldexp values with mask: [ 8.          0.55950035 64.        ]

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