I want to calculate the square root of a numpy array of negative numbers.
I tried with np.sqrt() but it gives error beacuse the domain.
Then, I found that for complex numbers you can use cmath.sqrt(x) but it also gives me an error.
Here's my code
import numpy as np
import cmath
from cmath import sqrt
x = np.arange(-10, 10, 0.01)
E = 1
p1 = cmath.sqrt(E - x**2)
And got this error
Traceback (most recent call last):
File "C:\Users\os.py", line 49, in <module>
p1 = cmath.sqrt(E - x**2)
TypeError: only length-1 arrays can be converted to Python scalars
Later I tried to use a for loop and it's not possible either. Here's the code:
import numpy as np
import cmath
from cmath import sqrt
x = np.arange(-10, 10, 0.01)
E = 1
for i in range(0, len(x)):
p1 = cmath.sqrt(E - x(i)**2)
and the message error
Traceback (most recent call last):
File "C:\Users\os.py", line 48, in <module>
p1 = cmath.sqrt(E - x(i)**2)
TypeError: 'numpy.ndarray' object is not callable
I don't know what am I dpoing wrong, can anyone help me?, please. I need to calculate the square root of an numpy array of negative numbers, does anyone know how to do this?
emathbut want to also mention the problem you are having in the loop version is just a syntax error.x(i)should probably bex[i]. It seems like you are trying to index the array, but the syntax is calling it like a function.