The **RuntimeWarning: invalid value encountered in double_scalars **occurs when you perform a complex mathematical operation using NumPy that involves extremely small or very large numbers and also if we pass an invalid input such as ** NaN** or

**null**while performing

**NumPy**operations.

## How to reproduce the error?

We get the RuntimeWarning while performing the complex math operation with very large or extremely small numbers using **NumPy**.

Few libraries in Python cannot handle such complex numbers and raise **RuntimeWarning** Exception.

Let us take a simple example to reproduce this error. In the below example, we have two NumPy arrays performing the Mathematical operations.

```
import numpy as np
# define two NumPy arrays
arr1 = np.array([[1000, 1100]])
arr2 = np.array([[1200, 3000]])
# Complex mathematical operation
result = np.exp(-3*arr1).sum() / np.exp(-3*arr1).sum()
print(result)
```

**Output**

`RuntimeWarning: invalid value encountered in double_scalars`

We receive a **RuntimeWarning** because the denominator is a complex number and extremely small that is closer to zero.

Hence when we perform the division with an extremely small denominator, we will get a large complex number as an output that Python cannot handle and raises a **RuntimeWarning**

## How to fix the error?

Since NumPy cannot handle large complex numbers, we can fix the error by using a special function ** logsumexp() **from another library,

**SciPy.**

The SciPy is a scientific computation library that uses NumPy underneath. ** SciPy** stands for Scientific Python. The library is designed to handle such complex scientific scenarios.

The method can handle large and small complex numbers with exponents efficiently.

`logsumexp()`

Let us modify our code to use the** ** method from the SciPy library.

`logsumexp()`

```
import numpy as np
from scipy.special import logsumexp
# define two NumPy arrays
arr1 = np.array([[1000, 1100]])
arr2 = np.array([[1200, 3000]])
# Complex mathematical operation
result = np.exp(logsumexp(-3 * arr1) - logsumexp(-3 * arr2))
print(result)
```

**Output**

`3.7730203009299397e+260`

Notice that the output is extremely large **3.7730203009299397e+260. **Still, we did not receive any errors because we have used the special method from the SciPy library that is designed to handle such scientific numbers.

`logsumexp()`

## Conclusion

The **RuntimeWarning: invalid value encountered in double_scalars **mainly occurs when you perform a complex mathematical operation that results in extremely large or small numbers and also if we pass an invalid input such as NaN or null while performing NumPy operations.

We can resolve the error by using a special method from the SciPy library which is designed to handle such scientific numbers and complex operations.

`logsumexp()`