Python tplquad:TypeError:&x27;浮动';对象不可调用
我试图运行此代码,但在标题中出现错误。我查找了关于tplquad的文档和示例,但无法理解我的问题。提前非常感谢 这是我的代码:Python tplquad:TypeError:&x27;浮动';对象不可调用,python,scipy,integrate,Python,Scipy,Integrate,我试图运行此代码,但在标题中出现错误。我查找了关于tplquad的文档和示例,但无法理解我的问题。提前非常感谢 这是我的代码: from numpy import * from pylab import * import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from math import * from scipy.integrate import quad,dblquad,tplquad from
from numpy import *
from pylab import *
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from math import *
from scipy.integrate import quad,dblquad,tplquad
from scipy.integrate import nquad
fig_width = 6.
fig_height = fig_width*0.75
fig_size = [fig_width,fig_height]
params = {'backend': 'TkAgg',
'axes.labelsize': 30,
'text.fontsize': 20,
'title.fontsize': 20,
'legend.fontsize': 20,
'xtick.labelsize': 20,
'ytick.labelsize': 20,
'text.usetex': False,
'font.family': 'sans-serif',
'figure.figsize': fig_size}
rcParams.update(params)
pi=3.14
pt_T=3.
#T=0.47
thetaP= -pi
precision=5
y=0
M_T=linspace(1.,7.,precision)
integral1d=[0]*precision
#chi now is def with a plus instead of the minus in the article
def chi(thetap1,p1,thetaP,T,M_T):
return abs((2*p1*T*sqrt(pt_T**2+(M_T**2+pt_T**2)*sinh(y)**2)*sin(thetaP)*sin(thetap1))**2 - (2*p1*T*(sqrt(M_T**2+pt_T**2)*cosh(y)- sqrt(pt_T**2+(M_T**2+pt_T**2)*sinh(y)**2)*cos(thetaP) *cos(thetap1) )-(T**2)* M_T**2)**2)+1
def p1max(thetaP, thetap1,T,M_T):
return (M_T**2)*T/(2*(sqrt(M_T**2+pt_T**2)*cosh(y)- sqrt(pt_T**2+(M_T**2+pt_T**2))*sinh(y)**2*cos(thetaP-thetap1)))-0.1
def p1min(thetaP, thetap1,T,M_T):
#NOT SURE ABOUT THE T AT DENOMINATOR
return (M_T**2)*T/(2*(sqrt(M_T**2+pt_T**2)*cosh(y)- sqrt(pt_T**2+(M_T**2+pt_T**2))*sinh(y)**2*cos(thetaP+thetap1))) +0.1
def integral(thetaP,T,M_T):
area =dblquad(lambda p1, thetap1: 5*(1/(18*pi**5))*sin(thetap1)*(p1/(sqrt(chi(thetap1,p1,thetaP,T,M_T))))*(1/(exp(p1/T) + 1))*(1/(exp((sqrt(M_T**2 + pt_T**2)*cosh(y) - p1/T) +1))) , -pi+0.1, -0.1, lambda p1: p1min(thetaP, p1,T,M_T), lambda p1: p1max(thetaP,p1,T,M_T)) #CHANGE 1., lambda p1:10.)
return area[0]
def integrand(M_T, p1,thetap1,T):
return pt_T*T*2*pi*5*(1/(18*pi**5))*sin(thetap1)*(p1/(sqrt(chi(thetap1,p1,thetaP,T,M_T))))*(1/(exp(p1/T) + 1))*(1/(exp((sqrt(M_T**2 + pt_T**2)*cosh(y) - p1/T) +1)))
def formula151(M_T):
area =tplquad(lambda p1, thetap1,T: pt_T*T*2*pi*5*(1/(18*pi**5))*sin(thetap1)*(p1/(sqrt(chi(thetap1,p1,thetaP,T,M_T))))*(1/(exp(p1/T) + 1))*(1/(exp((sqrt(M_T**2 + pt_T**2)*cosh(y) - p1/T) +1))) ,0.333, 20./3,lambda thetap1: -pi+0.1, -0.1, lambda thetap1, p1: p1min(thetaP, p1,T,M_T),lambda thetap1,p1: p1max(thetaP,p1,T,M_T) )
return area[0]
#solving the integral
for ind in range(0, precision):
integral1d[ind]=formula151( M_T[ind])
print integral1d[ind]
plot(M_T,integral1d)
xlabel('M/T')
ylabel('prod rate')
title('thetaP =-3.12')
plt.yscale('log')
#plt.xscale('log')
show()
错误来自第57行,在该行中使用了TPLQUARD,并且完整的回溯是
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
202 else:
203 filename = fname
--> 204 __builtin__.execfile(filename, *where)
/home/chiara/Scrivania/formula15a.py in <module>()
61 #solving the integral
62 for ind in range(0, precision):
---> 63 integral1d[ind]=formula151( M_T[ind])
64 print integral1d[ind]
65
/home/chiara/Scrivania/formula15a.py in formula151(M_T)
55
56 def formula151(M_T):
---> 57 area =tplquad(lambda p1, thetap1,T: pt_T*T*2*pi*5*(1/(18*pi**5))*sin(thetap1)*(p1/(sqrt(chi(thetap1,p1,thetaP,T,M_T))))*(1/(exp(p1/T) + 1))*(1/(exp((sqrt(M_T**2 + pt_T**2)*cosh(y) - p1/T) +1))) ,0.333, 20./3,lambda thetap1: -pi+0.1, -0.1, lambda thetap1, p1: p1min(thetaP, p1,T,M_T),lambda thetap1,p1: p1max(thetaP,p1,T,M_T) )
58 return area[0]
59
/usr/lib/python2.7/dist-packages/scipy/integrate/quadpack.pyc in tplquad(func, a, b, gfun, hfun, qfun, rfun, args, epsabs, epsrel)
498
499 """
--> 500 return dblquad(_infunc2,a,b,gfun,hfun,(func,qfun,rfun,args),epsabs=epsabs,epsrel=epsrel)
501
502
/usr/lib/python2.7/dist-packages/scipy/integrate/quadpack.pyc in dblquad(func, a, b, gfun, hfun, args, epsabs, epsrel)
433
434 """
--> 435 return quad(_infunc,a,b,(func,gfun,hfun,args),epsabs=epsabs,epsrel=epsrel)
436
437
/usr/lib/python2.7/dist-packages/scipy/integrate/quadpack.pyc in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
252 args = (args,)
253 if (weight is None):
--> 254 retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points)
255 else:
256 retval = _quad_weight(func,a,b,args,full_output,epsabs,epsrel,limlst,limit,maxp1,weight,wvar,wopts)
/usr/lib/python2.7/dist-packages/scipy/integrate/quadpack.pyc in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points)
317 if points is None:
318 if infbounds == 0:
--> 319 return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
320 else:
321 return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)
/usr/lib/python2.7/dist-packages/scipy/integrate/quadpack.pyc in _infunc(x, func, gfun, hfun, more_args)
379 def _infunc(x,func,gfun,hfun,more_args):
380 a = gfun(x)
--> 381 b = hfun(x)
382 myargs = (x,) + more_args
383 return quad(func,a,b,args=myargs)[0]
TypeError: 'float' object is not callable
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
/execfile(fname,*其中)中的usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc
202其他:
203 filename=fname
-->204 \uuuuu内置\uuuuuu.execfile(文件名,*其中)
/home/chiara/Scrivania/formula15a.py in()
61#求解积分
范围内的ind为62(0,精度):
--->63积分1d[ind]=公式151(M_T[ind])
64打印积分1D[ind]
65
/公式151中的home/chiara/Scrivania/formula15a.py(M_T)
55
56 def公式151(M_T):
--->57个区域=TPL4个四层(lambda p1,thetap1,thetap1,T:T:Ptu T*T*T*T*T*5*(1/(18*pi**5五点五五点五)面积=57个区域=TPL4个四层(lambda p1,thetap1,thetap1,thetap1,T,T,T:T:T T*T*T*T*T*5*5*5(1/(1/)*sin(thetap1)在(thetap1)中)宗宗,(1(1/(1/(1/(1/(1/(1//(1/(1/(一)宗宗宗宗)宗)在(该宗(该宗)宗(该宗(该宗)宗(该宗(该宗(该宗(该宗)宗)宗(该宗(该宗)宗(该宗(该宗(该宗)宗(该宗(该宗)(T)
58返回区[0]
59
/tplquad中的usr/lib/python2.7/dist-packages/scipy/integrate/quadpack.pyc(func、a、b、gfun、hfun、qfun、rfun、args、epsabs、epsrel)
498
499 """
-->500返回dblquad(_infunc2,a,b,gfun,hfun,(func,qfun,rfun,args),epsabs=epsabs,epsrel=epsrel)
501
502
/dblquad中的usr/lib/python2.7/dist-packages/scipy/integrate/quadpack.pyc(func、a、b、gfun、hfun、args、epsabs、epsrel)
433
434 """
-->435返回四元组(_infunc,a,b,(func,gfun,hfun,args),epsabs=epsabs,epsrel=epsrel)
436
437
/四元组中的usr/lib/python2.7/dist-packages/scipy/integrate/quadpack.pyc(func、a、b、args、full_输出、epsabs、epsrel、limit、points、weight、wvar、wopts、maxp1、limlst)
252 args=(args,)
253如果(重量为零):
-->254 retval=_四元(func、a、b、参数、完整输出、epsabs、epsrel、限制、点)
255其他:
256 retval=_quad_weight(函数、a、b、参数、全输出、epsabs、epsrel、limlst、limit、maxp1、weight、wvar、wopts)
/usr/lib/python2.7/dist-packages/scipy/integrate/quadpack.pyc in_quad(func、a、b、args、full_输出、epsabs、epsrel、limit、points)
317如果点为无:
318如果infbounds==0:
-->319返回四块数据包(函数、a、b、参数、全输出、epsabs、epsrel、limit)
320其他:
321返回_quadpack._qagie(func、bound、inbounds、args、full_输出、epsabs、epsrel、limit)
/usr/lib/python2.7/dist-packages/scipy/integrate/quadpack.pyc in_infunc(x,func,gfun,hfun,更多参数)
379定义信息(x、func、gfun、hfun、更多参数):
380 a=gfun(x)
-->381 b=hfun(x)
382 myargs=(x,)+更多参数
383返回四元组(func,a,b,args=myargs)[0]
TypeError:“float”对象不可调用
注:函数“积分”和“被积函数”已定义,但最终未使用……我只是将它们留在了
scipy.integrate
中,tplquad
的签名(计算三重积分的数值近似值)为:
scipy.integrate.tplquad(func,a,b,gfun,hfun,qfun,rfun,args=(),epsabs=1.49e-08,epsrel=1.49e-08)
其中,func
是三个待积分变量的函数,a
和b
是外积分的浮点极限,gfun
和hfun
是一个变量的函数,给出中间积分的极限,和qfun
和rfun
是两个变量的函数,给出了最内层积分的极限
我花了很长时间才弄清楚到底发生了什么,直到我将您的代码重新格式化,使其更具可读性。这是您对tplquad
的调用,已重新格式化以使线路长度略短:
area = tplquad(
lambda p1, thetap1, T: (
pt_T*T*2*pi*5*(1/(18*pi**5))*sin(thetap1)*
(p1/(sqrt(chi(thetap1,p1,thetaP,T,M_T))))*
(1/(exp(p1/T) + 1))*
(1/(exp((sqrt(M_T**2 + pt_T**2)*cosh(y) - p1/T) + 1)))
),
0.333, # a
20./3, # b
lambda thetap1: -pi + 0.1, # gfun
-0.1, # hfun
lambda thetap1, p1: p1min(thetaP, p1, T, M_T), # qfun
lambda thetap1, p1: p1max(thetaP, p1, T, M_T), # rfun
)
(正如@user2357112所建议的,将这些lambda
表达式从调用中拉出并将它们定义为单独的函数也有助于可读性。特别是,如果您为被积函数定义单独的函数,您将能够逐段执行计算,并且不需要将一个巨大的表达式放在一行上。)
重新格式化后,更容易看出问题所在:在调用tplquad
时,传递了hfun
的常量-0.1
。这是行不通的:在数学中,人们可以(ab)用一个常量值来表示一个常量函数,但编程语言(以及一些数学家)往往有点挑剔:这里需要一个实际的函数。将-0.1
替换为lambda thetap1:-0.1
顺便说一下,我也有点怀疑你这里的可变顺序。doc页面指出,虽然被积函数的输入顺序应该是
(z,y,x)
,gfun
和hfun
应该是x
的函数,而qfun
和rfun
应该是(x,y)
的函数(按该顺序)。这似乎与您拥有的不匹配。哪一行包含错误?你能显示完整的回溯吗
对不起…我用错误行和回溯编辑了这个问题@MarkDickinson我只是尝试了你的建议,但是我得到了这个错误:TypeError:tplquad()至少需要7个参数(给定6个),你可能不应该把所有这些回调和巨型表达式塞进一行。尝试将这些巨大的表达式分解成有意义的部分,并为这些部分定义临时变量;它将使事情更易于阅读和调试。您真的希望pi
成为3.14
?您已经导入了一个