[Python] Cannot cast array data from dtype('complex128') to dtype('float64') according to the...

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to solve an optimization problem as shown below. But every time I get an error Cannot cast array data from dtype('complex128') to dtype('float64') according to the rule 'safe'. Can anyone help me to see what is wrong here in the code?

    def func(vec):

    linspec = -(kx**2)+((1.-nu)*(kx**4))
    lin = linspec*np.fft.fft(vec)
    nlin = np.zeros_like(lin)
    nlinre = vec*vec
    nlinspec = np.fft.fft(nlinre)
    nlin = (0.5*1j*kx*nlinspec)
    sol = lin+nlin
    rhs = np.zeros_like(sol, dtype='complex')
    sol -= rhs
    sol = np.fft.ifft(sol).real

    return sol

    def kssol(u0):

    u1 = np.ones((2*Mx,), dtype='complex')
    #u1 = 100.*u0
    u = scipy.optimize.fsolve(func, u1)

    return u

    Continue reading...

Compartilhe esta Página