使用 SciPy 的 fsolve 求解非线性哈密顿量
我正在求解二聚体的非线性哈密顿量,它由 2 个复杂的波函数组成。我通过迭代使用 SciPy 的根求解器方法。因此,我最初猜测的复数输入必须编码为实部和虚部,这将使初始 2x1 矩阵方程变成 4x1 矩阵方程。
我尝试过的:
from scipy.optimize import fsolve
import math
import numpy as np
import cmath
from math import pi
k = np.linspace(-pi, pi, 100)
v = 1
w = 1
H = np.zeros((2, 2), dtype=complex)
H[0, 1] = v+w*cmath.exp(-1j*k)
H[1, 0] = v+w*cmath.exp(1j*k)
E=np.zeros((2,2), )
E[0,1]=-(v**2+w**2+2*v*w*math.cos(k))**0.5
E[1,0]=(v**2+w**2+2*v*w*math.cos(k))**0.5
def f(x):
psi = np.zeros((2,1),float)
psi[0]=np.isreal(x[0])+1j*np.iscomplex(x[1])
psi[1]=np.isreal(x[2])+1j*np.iscomplex(x[3])
f=(H-E)*psi
return f
m = fsolve(f,np.array([[1],[2],[3],[4]]))
print (m.x)
我收到的错误消息如下,
TypeError: only length-1 arrays can be converted to Python scalars #for line 12
我需要帮助来纠正此错误。
I am in the midst of solving for a nonlinear Hamiltonian of a dimer, which consists of 2 complex wavefunctions. I am using SciPy's root solver method by iterations. Thus, the complex input for my initial guess has to be encoded into real and imaginary parts, which will then make the initial 2x1 matrix equation into a 4x1 matrix equation.
What I've tried :
from scipy.optimize import fsolve
import math
import numpy as np
import cmath
from math import pi
k = np.linspace(-pi, pi, 100)
v = 1
w = 1
H = np.zeros((2, 2), dtype=complex)
H[0, 1] = v+w*cmath.exp(-1j*k)
H[1, 0] = v+w*cmath.exp(1j*k)
E=np.zeros((2,2), )
E[0,1]=-(v**2+w**2+2*v*w*math.cos(k))**0.5
E[1,0]=(v**2+w**2+2*v*w*math.cos(k))**0.5
def f(x):
psi = np.zeros((2,1),float)
psi[0]=np.isreal(x[0])+1j*np.iscomplex(x[1])
psi[1]=np.isreal(x[2])+1j*np.iscomplex(x[3])
f=(H-E)*psi
return f
m = fsolve(f,np.array([[1],[2],[3],[4]]))
print (m.x)
The error message that I'm getting is as follows
TypeError: only length-1 arrays can be converted to Python scalars #for line 12
I need help to rectify this mistake.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论