如何向数组中添加新的复杂值?

2024-10-06 10:24:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经做了一个需要积分的复数数组,所以我把vlaues分解成图像值和实数值,然后分别积分,然后我想把这些值作为新的复数加到一个数组中。你知道吗

我试过使用numpy.complex(), numpy.vectorize() and normal input (x + 1j*y),但不起作用。我也试着切。你知道吗

我已经检查过这个问题和其他地方,他们称之为一个错误。我试过他们所有的解决办法,但都不适合我。你知道吗

import numpy as np
N = 400
Nx = 1334
Cn = np.zeros(shape = (N) )

x0 = 50*10**(-9)
L = 200*10**(-9)
x = np.linspace(0, L, Nx)
sig = 10**(-8)
k = 2289670634.4999747


expsig = np.exp(-((1/2)*(x-x0)**2)/(sig**2))
expimg = np.exp(1j*k*(x-x0))
Phi = ((1/(np.pi**(1/4)*np.sqrt(sig)))*expsig*expimg)

Boxfunc = np.zeros(shape = (N, Nx))
for i in range(0, N):
    SINnpi = np.sin(((i*np.pi)/L)*x)
    Boxfunc[i,:] = np.sqrt(2/L)*SINnpi
    Y = Boxfunc[i,:]*Phi

    realY = np.real(Y)
    imagY = np.imag(Y)

    RealCn = np.trapz(realY)*dx
    ImagCn = np.trapz(imagY)*dx

    Cn[i] = RealCn + 1j*ImagCn

我希望输出是作为一个复杂数组的Cn,但是我得到的只是:TypeError: can't convert complex to float", or "__main__:59: ComplexWarning: Casting complex values to real discards the imaginary part。你知道吗


Tags: numpynpzeros数组cn复数signx
1条回答
网友
1楼 · 发布于 2024-10-06 10:24:57

尝试为数组分配dtype

Cn = np.zeros(shape = (N)).astype('complex128')

默认情况下np.zeros使用float64。你知道吗

相关问题 更多 >