How can I take the square root of -1 using python?

When I take the square root of -1, this gives me an error:

invalid value encountered in sqrt

How to fix it?

///////// arr=sqrt(-1) print(arr) OUTPUT 
+6
source share
5 answers

You need to use sqrt from cmath module

 >>> import cmath >>> cmath.sqrt(-1) 1j 
+6
source

To avoid the warning / invalid value error, the argument of the numpy sqrt function should be complex:

 In [8]: import numpy as np In [9]: np.sqrt(-1+0j) Out[9]: 1j 

As @AshwiniChaudhary noted in a comment, you can also use the standard cmath library:

 In [10]: cmath.sqrt(-1) Out[10]: 1j 
+22
source

I just opened the numpy.lib.scimath.sqrt function described in the sqrt documentation. I use it as follows:

 >>> from numpy.lib.scimath import sqrt as csqrt >>> csqrt(-1) 1j 
+6
source

The square root of -1 is not a real number, but rather an imaginary number. IEEE 754 has no way of representing imaginary numbers.

numpy supports many numbers. I suggest you use this: http://docs.scipy.org/doc/numpy/user/basics.types.html

+1
source

Others probably suggested more desirable methods, but just to add to the conversation, you can always multiply any number less than 0 (the value you want to get in sqrt, -1 in this case) by -1, then take sqrt of that. Just know that your result is imaginary.

-1
source

Source: https://habr.com/ru/post/949906/


All Articles