When I take the square root of -1 it gives me an error:
invalid value encountered in sqrt
How do I fix that?
from numpy import sqrt
arr = sqrt(-1)
print(arr)
I just discovered the convenience function numpy.lib.scimath.sqrt
explained in the sqrt documentation. I use it as follows:
>>> from numpy.lib.scimath import sqrt as csqrt
>>> csqrt(-1)
1j
To avoid the invalid value
warning/error, the argument to numpy's sqrt
function must be complex:
In [8]: import numpy as np
In [9]: np.sqrt(-1+0j)
Out[9]: 1j
As @AshwiniChaudhary pointed out in a comment, you could also use the cmath
standard library:
In [10]: cmath.sqrt(-1)
Out[10]: 1j
You need to use the sqrt from the cmath module (part of the standard library)
>>> import cmath
>>> cmath.sqrt(-1)
1j
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With