Skip to main content
 首页 » 编程设计

python之SciPy 无法将复数转换为 float

2025年05月04日7luoye11

我遇到了一个问题,我在下面做了简化。

所以,我有一个函数(矩阵),它返回矩阵的特征值。

第二个函数 (deriv) 计算特征值对 delta 的导数。 SciPy自带的求导函数很慢,所以用了复差法。

然后我对 r 和 theta 求被积函数的二重积分。我没有采用二重积分,而是在对 theta 进行一维积分后求解 r 的微分方程,这使得计算速度更快。

最后,整合工作。但是,如果我试图找到满足方程式的特定增量,则会出现错误:无法将复数转换为 float ,我不明白虚数从何而来。

我是 Python 世界的新手,非常感谢任何帮助。谢谢。

from scipy.integrate import quad 
from numpy import linalg as LA 
import numpy as np 
from pylab import * 
 
from scipy.integrate import odeint 
from scipy.integrate import ode 
from scipy.optimize import fsolve 
 
 
#This routine is for the matrix and returns the eigenvalues 
def matrix(r, theta, delta1, delta2): 
 
    mat = np.array([[r**2-1,r*np.cos(theta),0,delta1],r*np.cos(theta),r**2 - 1, -delta1,0],[0,-delta2,-r**2+1,-r*np.cos(theta)],[delta2,0,-r*np.cos(theta),-r**2+1]]) 
    return np.sort(LA.eigvals(mat))[2:4] 
 
#This routine takes the derivatives of eigenvalues with respect to the parameter delta. I set delta1 = delta2. 
def deriv(r, theta, delta): 
 
    h = 0.00000000001 
 
    return np.imag(matrix(r, theta, delta, delta+h*1j))/h 
 
#This is the integrand that we need to integrate over r and theta 
def integrand(theta,r, beta, delta): 
    ee = matrix(r, theta, delta, delta) 
    dd = deriv(r, theta, delta) 
    return (np.tanh(0.5*beta*ee[0])*dd[0]+np.tanh(0.5*beta*ee[1])*dd[1])*r*r*np.sin(theta) 
 
#Just integrate over theta 
def polarint(y,r,beta,delta): 
    return quad(integrand,0,np.pi,args = (r,beta,delta))[0] 
 
#Instead of integrating directly over r, solve the differential equation. 
#Lambda is the range of r. 
def givesclen(delta, beta, lam): 
    y0 = 0 
    t_out = np.linspace(0, lam, 2500); 
    rt = odeint(polarint,y0,t_out,args=(beta,delta)) 
    temp = (rt[-1]/delta)/(4*np.pi**2)-t_out[-1]/(2*np.pi**2) 
    return temp[0] 
 
#The goal is to find the delta; given sl, lam, beta 
#Such that the result of the integration is equal to sl 
def equationgap(delta, beta, lam,sl): 
    return givesclen(delta, beta, lam)*4*np.pi - sl 
 
#Test if the equationgap works, the result should be close to zero! 
print equationgap(.5,40,500,.1744) 
 
#Now use the fsolve function should find the delta to be .5! 
#beta = 40 
#lam = 500 
#sl = 0.174 
#fsolve(equationgap,.6,args = (beta, lam, sl)) 

编辑:

错误信息是:

Traceback (most recent call last): 
File "test.py", line 38, in polarint 
return quad(integrand,0,np.pi,args = (r,beta,delta))[0] 
File "/usr/local/anaconda/lib/python2.7/site-packages/scipy/integrate/quadpack.py", line 281, in quad 
retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points) 
File "/usr/local/anaconda/lib/python2.7/site-packages/scipy/integrate/quadpack.py", line 345, in _quad 
return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit) 
File "test.py", line 30, in integrand 
dd = deriv(r, theta, delta) 
File "test.py", line 22, in deriv 
return np.imag(matrix(r, theta, delta, delta+h*1j))/h 
File "test.py", line 14, in matrix 
mat = np.array([[r**2-1,r*np.cos(theta),0,delta1],[r*np.cos(theta),r**2 - 1, -delta1,0],[0,-delta2,-r**2+1,-r*np.cos(theta)],[delta2,0,-r*np.cos(theta),-r**2+1]]) 
TypeError: can't convert complex to float 

请您参考如下方法:

这是失败的最少代码

from numpy import array 
q = [[-1.0, 0.0, 0, array([ 0.6])], [0.0, -1.0, array([-0.6]), 0], [0, array([-0.6 -1.00000000e-11j]), 1.0, -0.0], [array([ 0.6 +1.00000000e-11j]), 0, -0.0, 1.0]] 
array(q) 

这有点奇怪。但是,请注意,有些数组的一个元素与纯数字混合在一起。这可以通过在给它之前打印出你给 array() 的对象来发现。

要修复它,请在您提供给 fsolve 的函数中将 delta 更改为 delta[0]:

def equationgap(delta, beta, lam,sl): 
    return givesclen(delta[0], beta, lam)*4*np.pi - sl 

因为其余代码期望 delta 是单个数字,而不是数组。 fsolve 将为要优化的函数提供一个数字数组,即使只有一个数字也是如此。