我遇到一些麻煩python的complex_ode求解。蟒蛇complex_ode通矩陣值參數
我試圖解決以下等式:
DY/DT = -iAy - ICOS(Omegat)乙ý
其中A和B是N×N個陣列和未知y是Nx1陣列,i是虛數單位,Ω是參數。
這裏是我的代碼:
import numpy as np
from scipy.integrate import ode,complex_ode
N = 3 #linear matrix dim
Omega = 1.0 #parameter
# define symmetric matrices A and B
A = np.random.ranf((N,N))
A = (A + A.T)/2.0
B = np.random.ranf((N,N))
B = (B + B.T)/2.0
# define RHS of ODE
def f(t,y,Omega,A,B):
return -1j*A.dot(y)-1j*np.cos(Omega*t)*B.dot(y)
# define list of parameter
params=[Omega,A,B]
# choose solver: need complex_ode for this ODE
#solver = ode(f)
solver = complex_ode(f)
solver.set_f_params(*params)
solver.set_integrator("dop853")
# set initial value
v0 = np.zeros((N,),dtype=np.float64)
v0[0] = 1.0
# check that the function f works properly
print f(0,v0,Omega,A,B)
# solve-check the ODE
solver.set_initial_value(v0)
solver.integrate(10.0)
print solver.successful()
運行此腳本將產生錯誤
capi_return is NULL
Call-back cb_fcn_in___user__routines failed.
Traceback (most recent call last):
File "ode_test.py", line 37, in <module>
solver.integrate(10.0)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/integrate/_ode.py", line 515, in integrate
y = ode.integrate(self, t, step, relax)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/integrate/_ode.py", line 388, in integrate
self.f_params, self.jac_params)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/integrate/_ode.py", line 946, in run
tuple(self.call_args) + (f_params,)))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/integrate/_ode.py", line 472, in _wrap
f = self.cf(*((t, y[::2] + 1j * y[1::2]) + f_args))
TypeError: f() takes exactly 5 arguments (2 given)
相反,如果我用求解= ODE(F),即。實值解算器,它運行良好。除了它不解決ODE我想要哪個是複數值:(
然後我嘗試通過使矩陣A和B全局變量來減少參數的數量。這樣,函數f接受的唯一參數是歐米茄,錯誤修改
capi_return is NULL
Call-back cb_fcn_in___user__routines failed.
Traceback (most recent call last):
File "ode_test.py", line 37, in <module>
solver.integrate(10.0)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/integrate/_ode.py", line 515, in integrate
y = ode.integrate(self, t, step, relax)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/integrate/_ode.py", line 388, in integrate
self.f_params, self.jac_params)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/integrate/_ode.py", line 946, in run
tuple(self.call_args) + (f_params,)))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/integrate/_ode.py", line 472, in _wrap
f = self.cf(*((t, y[::2] + 1j * y[1::2]) + f_args))
TypeError: 'float' object has no attribute '__getitem__'
,我想通了,浮指參數歐米茄[試圖通過一個整數。同樣,在這種情況下,「頌」單獨的作品也是如此。
末,我試圖值方程相同的配合物,但現在A和B僅僅是數字。我試圖他們兩個作爲參數傳遞,即PARAMS =ω,A,B]爲WEL l使它們成爲全局變量,在這種情況下,params =Ω。錯誤是
TypeError: 'float' object has no attribute '__getitem__'
錯誤 - 完整的錯誤是與上面相同。再次,這個問題對於實值「頌歌」不會發生。
我知道zvode是一種選擇,但它似乎成爲大N.在真正的問題我已經很慢,A是對角矩陣,但B是一個非稀疏全矩陣。
任何見解都非常感謝!我在(我)的替代方法來解決與數組值參數此復值ODE都感興趣,並且(ii)如何獲得complex_ode運行:)
謝謝!
,你能否告訴我們完整的追蹤嗎? TypeError似乎和[here]一樣(https://stackoverflow.com/questions/34577870/using-scipy-integrate-complex-ode-instead-of-scipy-integrate-ode)。簡而言之,不要在'complex_ode()'中使用'set_f_params()'。爲了解決複雜的ODE,你也可以使用'zvode()'。這照顧到問題編號2. – Reti43
我更新了顯示完整錯誤消息的帖子。我知道zvode,但是當矩陣大小變大時它有點慢。或者你是否意味着有一種方法可以結合complex_ode()和zvode來傳遞參數? –
這就是bug,沒關係。如果你打印在'_wrap()'中傳遞的值,你會發現如果你自己使用了'set_f_params()'或者修改了'solver.params',那麼它們都會被損壞(這就是'set_f_params )'在內部)。所以你應該在'_wrap()'中切分數組,所以你最終得到了浮點數,因此是'TypeError'。不,沒有兩個集成商結合在一起,你只需從頭開始使用'zvode'。爲什麼矩陣變大時積分器不能變慢?如果矩陣應該保持未知參數,則它們的數量增加N ** 2。 – Reti43