# [Solved] RuntimeWarning: invalid value encountered in double_scalars

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 `logsumexp()` method can handle large and small complex numbers with exponents efficiently.

Let us modify our code to use the `logsumexp()` method from the SciPy library.

``````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 `logsumexp()` method from the SciPy library that is designed to handle such scientific numbers.

## 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 `logsumexp()` from the SciPy library which is designed to handle such scientific numbers and complex operations.

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

## AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’

Table of Contents Hide What is AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’?How to fix AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’?Example 1: Get the index position of an element…

## Python PermissionError: [Errno 13] Permission denied

Table of Contents Hide What is PermissionError: [Errno 13] Permission denied error?How to Fix PermissionError: [Errno 13] Permission denied error?Case 1: Insufficient privileges on the file or for PythonCase 2:…

## Python maximum recursion depth exceeded in comparison

Table of Contents Hide What is Recursion?A classic example of recursionWhy does Python throw maximum recursion depth exceeded in comparison?How to check maximum recursion depth in Python?How do you fix the…

## Python TypeError: ‘int’ object is not subscriptable

Table of Contents Hide What is Subscriptable in Python?How do you make an object Subscriptable?How to Fix TypeError: ‘int’ object is not subscriptable?SolutionConclusion In Python, we use Integers to store…

## [Solved] Error: command errored out with exit status 1

If you are installing auto-py-to-exe package on Python 3.8 or below, you will get an error stating ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full…

## Python bytearray()

Table of Contents Hide bytearray() Syntax bytearray() Parametersbytearray() Return ValueExample 1: Array of bytes of given integer sizeExample 2: Array of bytes from a stringExample 3: Array of bytes from an…