I have made an array of complex numbers that I need to integrate, So I split the vlaues into imagenary and real values, then integrate idividually, then I want to add these values into an array as new complex numbers.
I have tried using numpy.complex(), numpy.vectorize() and normal input (x + 1j*y)
, but it does not work. I also tried to slice.
I've checked out this problem and other places they call it a bug. I have tried all their solutions but they don't work for me.
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
I expect the output to be Cn as an complex array, but all I get is: TypeError: can't convert complex to float", or "__main__:59: ComplexWarning: Casting complex values to real discards the imaginary part
.
Try assign dtype
for your array:
Cn = np.zeros(shape = (N)).astype('complex128')
By default np.zeros
is using float64
.