Related Tags

function
numpy
python
communitycreator

What is the ldexp() function in NumPy?

Onyejiaku Theophilus Chidalu

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The numpy.ldexp() function in NumPy is used to return x1 ×$2^{x2}$, element-wise of input arrays (x1 and x2) passed to the function.

Mathematically, this is represented as follows:

numpy.ldexp(x1,x2,) = x1 ×$2^{x2}$

Syntax

Let's view the syntax for this function.

numpy.ldexp(x1, x2, /, out=None, *, where=True)
The syntax for the numpy.ldexp() function

Parameter values

The numpy.ldexp() function takes the following parameter values.

• x1: This represents the input array of multipliers and is a required parameter.
• x2: This represents an array of two exponents and is a required parameter.
• out: This represents the location where the result is stored and is an optional parameter.
• where: This is the condition over which the input is being broadcasted. At a given location where this condition is True, the resulting array will be set to the ufunc result. Otherwise, the resulting array will retain its original value. This is an optional parameter.
• **kwargs: This represents other keyword arguments and is an optional parameter.

Example

Let's view the code for this function.

import numpy as np

# creating input arrays
x1 = np.array([1, 2, 3, 4, 5])
x2 = np.array([2, 2, 2, 1, 1])
# implementing the ldexp() function
myarray = np.ldexp(x1, x2)

print(x1)
print(x2)
print("This is the ldexp values: ", myarray)
Implementing the ldexp() function

Explanation

• Line 1: We import the numpy module.
• Line 4: We create the input arrays x1 and x2 using the array() function.
• Line 7: We implement the numpy.ldexp() function on the input arrays. We assign the result to a variable myarray.
• Line 9–10: We print the input arrays x1 and x2to the console.
• Line 11: We print the variable myarray to the console.
Note: From the output, the first elements of x1 and x2 are 1 and 2, that is:
x1 = 1
x2 = 2
Therefore, from numpy.ldexp(x1, x2) = x1 × $2^{x2}$, numpy.ldexp (1, 2) = 1 × $2^{2} = 4$.

RELATED TAGS

function
numpy
python
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu