在执行 sciPy 的四积分方法时"TypeError"消息,与 mpmath 函数集成



我正在尝试使用scipy.integrate.quad.计算两个积分,但是,由于 gamma 具有 pangund 第一个参数的函数在 scipy中未定义。必须从mpmath选择版本。运行以下代码后,

from scipy.integrate import *
from mpmath import *

low, up  = 5.630e5, 1.167e12
alpha, threshold = 1.05   , 2.15e10 
beta = 274
def g(x, beta, low, up):
    return gamma(-2/3) * (gammainc(-2/3, beta*(x/low)**3) - gammainc(-2/3, beta*(x/up)**3))
def Integrand1(x, low, threshold, alpha):
    return pow(x/threshold, alpha) * g
def Integrand2(x, up, threshold):
    return g
Integral1 = quad(Integrand1, low, threshold, args=(low, up, threshold, alpha, beta))
Integral2 = quad(Integrand2, threshold, up, args=(low, up, threshold, beta))
print(Integral1)
print(Integral2)

这是我不知道如何处理和需要帮助的错误消息:

trackback(最近的最新呼叫):文件" test.py",第19行 integral1 = quad(intemand1,low,threshold,args =(低,向上,阈值,alpha,beta))文件 "/home/username/anaconda3/lib/python3.6/site-packages/mpmath/calculus/quadrature.py", 第748行,在Quad 点[0],prec,epsilon,m,verbose)文件"/home/username/anaconda3/lib/python3.6/site-packages/mpmath/calculus/quadrature.py", 总结215行 对于i在Xrange(len(point)-1)中的i

我只能猜测原因可能是quad函数与使用mpmath.

定义的积分不兼容

导入语句

不要从两个地方导入 *,这是名称碰撞的食谱。mpmath有自己的quad方法,它在您的代码中取代了Scipy的quad

from scipy.integrate import quad
from mpmath import gamma, gammainc 

功能参数

如果您正在调用函数g,则必须为其提供参数。因此,写* g(x, beta, low, up)代替* g

当然,这些参数也必须可用于调用g的函数。这样:

def Integrand1(x, low, up, threshold, alpha, beta):
    return pow(x/threshold, alpha) * g(x, beta, low, up)
def Integrand2(x, low, up, threshold, alpha, beta):
    return g(x, beta, low, up)
Integral1 = quad(Integrand1, low, threshold, args=(low, up, threshold, alpha, beta))
Integral2 = quad(Integrand2, threshold, up, args=(low, up, threshold, alpha, beta))

请注意,传递给Integrand函数的参数与他们期望接收的内容匹配。他们获得X,并且在args QUAD参数中列出的所有内容。

上面的代码没有丢弃错误。我不确定该操作在数学上是否有意义,因为您将threshold都用于缩放和上限,但这是另一个故事。

相关内容

最新更新