# NumPy ldexp: A Complete Guide

Hello! In this tutorial, we are going learn about the ldexp method present in the NumPy module in Python. In our previous tutorials, we have learnt about the NumPy functions exp() and exp2().

We know that, the `exp(x)` function returns `e^x`, where e is Euler’s constant having the value of approximately 2.718281 and the `exp2(x)` function calculates the value `2^x`.

Now, let us learn about the `ldexp()` function in NumPy.

## What is NumPy ldexp?

The `ldexp` function in NumPy takes two parameters x1 and x2 and returns x1*2**x2, element-wise. Mathematically, we can represent it as follows:

Note that, in Python, the ** operator has more precedence than the * operator. Hence, first the value 2**x2 is calculated and then it is multiplied by x1 to compute the final result.

## Syntax of NumPy ldexp

Let us have a look at the syntax of ldexp().

```numpy.ldexp(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
```

The shape of x1 and x2 should be equal.

Returns:

An n-dimensional array storing the result of x1***x2. This is scalar if both x1 and x2 are scalars.

Note that, complex data types are not supported and if provided, will raise a TypeError.

## Examples of NumPy ldexp

Now let us get right into the examples and understand how the function works.

### 1. When both the inputs are scalars

```import numpy as np

# case 1
x1_1 = 2
x2_1 = 3
ans_1 = np.ldexp(x1_1, x2_1)
print("Case 1:\nx1 = ", x1_1, ", x2 = ", x2_1, ", Result = ", ans_1)

# case 2
x1_2 = -4
x2_2 = 5
ans_2 = np.ldexp(x1_2, x2_2)
print("Case 2:\nx1 = ", x1_2, ", x2 = ", x2_2, ", Result = ", ans_2)

# case 3
x1_3 = 6.8
x2_3 = 2
ans_3 = np.ldexp(x1_3, x2_3)
print("Case 3:\nx1 = ", x1_3, ", x2 = ", x2_3, ", Result = ", ans_3)
```

Output:

```Case 1:
x1 =  2 , x2 =  3 , Result =  16.0
Case 2:
x1 =  -4 , x2 =  5 , Result =  -128.0
Case 3:
x1 =  6.8 , x2 =  2 , Result =  27.2
```

The outputs are computed as follows:

Case 1:

```2**x2  = 2**3 = 8
Result = x1*8 = 2*8 = 16
```

Case 2:

```2**x2  = 2**5 = 32
Result = x1*32 = (-4)*32 = -128
```

Case 3:

```2**x2  = 2**2 = 4
Result = x1*4 = 6.8*4 = 27.2
```

### 2. When both the inputs are arrays

```import numpy as np

list_1 = [-5, 2, 4, 6]
list_2 = [4, 3, -2, 1]

ans = np.ldexp(list_1, list_2)
print(ans)
```

Output:

```[-80.  16.   1.  12.]
```

The computation of the output is as follows:

```ans[0] = -5*2**4 = -5*16 = -80
ans[1] = 2*2**3 = 2*8 = 16
ans[2] = 4*2**-2 = 4*0.25 = 1
ans[3] = 6*2**1 = 6*2 = 12
```

### 3. When one input is an array and the other is a scalar

```import numpy as np

l = [8, 12, 3, 10]
x = 3

ans = np.ldexp(l, x)
print(ans)
```

Output:

```[64. 96. 24. 80.]
```

Here, since the second argument x is a scalar, for each value in the answer, the element in the list is multiplied by 2**x i.e. 2**3 = 8.

```ans[0] = 8*2**3 = 8*8 = 64
ans[1] = 12*2**3 = 12*8 = 96
ans[2] = 3*2**3 = 3*8 = 24
ans[3] = 10*2**3 = 10*8 = 80
```

## Conclusion

That’s all! In this tutorial, we learned about the Numpy ldexp method and practiced different types of examples using the same. If you want to learn more about NumPy, feel free to go through our NumPy tutorials.