Reputation: 35
I keep getting the error only length-1 arrays can be converted to Python scalars
. I am getting error due to the variable 'uc' as it is an array of 214 elements.
The error states:
File "/home/lokesh/PycharmProjects/Cross_Range_Imaging/Cross_Range.py", line 68, in <module>
dis = cmath.sqrt(Xc ** 2 + (Yc + yn[i] - uc) ** 2)
TypeError: only length-1 arrays can be converted to Python scalars
Tried code:
import math
import cmath
import numpy as np
cj = cmath.sqrt(-1)
pi2 = 2 * cmath.pi
c = 3e8
fc = 200e6
Xc = 1e3
Yc = 0
Y0 = 100
L = 400
theta_c = cmath.atan(Yc / Xc)
L_min = max(Y0, L)
lamda = c / fc
Xcc = Xc / np.power(np.cos(theta_c),2)
duc = (Xcc * lamda) / (4 * Y0)
mc = 2 * math.ceil(L_min / duc)
uc = duc * np.arange(-mc / 2, mc/2)
k = pi2 / lamda
yn = [0, 70, 66.2500, -80]
fn = [1, 1, 1, 1]
ntarget = 4
s = np.zeros((1, mc))
for i in np.arange(ntarget):
dis = cmath.sqrt(Xc ** 2 + (Yc + yn[i] - uc) ** 2)
s = s + np.multiply(fn[i] * np.exp(-cj * 2 * k * dis), (abs(uc) <= L))
The expected result values of variable 'dis' are 1x214 double
and variable 's' are 1x214 complex double
Upvotes: 1
Views: 372
Reputation: 151
Try dis = np.sqrt(Xc ** 2 + (Yc + yn[i] - uc) ** 2)
.
cmath.sqrt operates on scalars, not arrays. You have to use an array operation like numpy.sqrt()
to get the square root of each value in the array.
Note: this will create dis
with shape (214,)
, which will still work for setting s
, but if you want dis
to be (1,214)
you may want to add .reshape(1, 214)
to the end of my suggested code.
Upvotes: 2