odeint:无法根据规则“安全”将数组数据从dtype('complex128')转换为dtype('float64')

问题描述 投票:0回答:1

以下代码给出错误:根据规则“安全”,无法将数组数据从dtype('complex128')强制转换为dtype('float64')。>

import numpy as np
from numpy.fft import fft
from scipy.integrate import odeint

t = np.linspace(0,9,10)

def func(y, t):
    k = 0
    dydt = fft(y)
    return dydt

y0 = 0
y = odeint(func, y0, t)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-4885da912033> in <module>
     10 
     11 y0 = 0
---> 12 y = odeint(func, y0, t)

~\AppData\Local\Continuum\anaconda3\envs\udacityDL\lib\site-packages\scipy\integrate\odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst)
    243                              full_output, rtol, atol, tcrit, h0, hmax, hmin,
    244                              ixpr, mxstep, mxhnil, mxordn, mxords,
--> 245                              int(bool(tfirst)))
    246     if output[-1] < 0:
    247         warning_msg = _msgs[output[-1]] + " Run with full_output = 1 to get quantitative information."

TypeError: Cannot cast array data from dtype('complex128') to dtype('float64') according to the rule 'safe'

但是,如果我从func中返回实值(而不是复数),如:

def func(y, t):
    k = 0
    dydt = fft(y)
    return np.abs(dydt)

然后odeint正常工作。

任何人都可以帮助我确定/解决此问题的根源吗?

提前感谢!

以下代码给出错误:无法根据规则'safe'从numpy.fft导入numpy,将数组数据从dtype('complex128')强制转换为dtype('float64'),从scipy.integrate导入fft。 。

python scipy differential-equations odeint ode45
1个回答
0
投票

您正在更改数据类型并返回复杂的值,而ODE求解器将在其中期望使用实际值。

© www.soinside.com 2019 - 2024. All rights reserved.