【问题标题】:Numpy array assignment with complex values gets error: "ComplexWarning: Casting complex values to real discards the imaginary part"具有复数值的 Numpy 数组赋值出错:“ComplexWarning:将复数值转换为实数会丢弃虚部”
【发布时间】:2021-01-24 15:16:05
【问题描述】:

我正在尝试做这些操作:

width = 23
alpha_i = np.array([1.84704762, 1.7878235 , 1.72701305, 1.66501652, 1.60228591,
   1.53930674, 1.47657613, 1.41457961, 1.35376915, 1.29454503])

w_s_i = (wc / c) * d * np.cos(alpha_i) 
ws_idx = np.zeros((width, 1)) # vettore che contiene "width" elementi spaziati di 1
ii = 0
for i in range(np.int(np.floor(width / 2)),-np.int(np.floor(width / 2))-1,-1):
   ws_idx[ii,:] =  i
   ii = ii + 1


# eq.(7)
steer_vecs = np.zeros((width, alpha_i.shape[0]));
   for i in range(0,10):
   steer_vecs[:, i] = np.squeeze(np.exp(1j * np.multiply(ws_idx ,w_s_i[i])),axis=1)

但我不知道为什么在最后一个 for 循环结束时,虚部被丢弃.. 我收到以下错误:

ComplexWarning: Casting complex values to real discards the imaginary part

有什么建议吗?谢谢!

【问题讨论】:

    标签: python numpy variable-assignment complex-numbers


    【解决方案1】:

    左侧steer_vecs[:, i]float64the default type 类型的数组。因此,如果您尝试将复杂值分配给浮点数组,则会收到警告。

    所以你需要将steer_vecs声明为一个复杂的数组:

    steer_vecs = np.zeros((width, alpha_i.shape[0]), dtype=np.complex)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多