"TypeError" 消息,同时使用 mpmath 函数执行 scipy.integrate 的四元积分方法
"TypeError" message while performing quad integration method of scipy.integrate with mpmath functions
我正在尝试使用 scipy.integrate.quad.
计算两个积分,但是,由于 gamma 函数没有定义第一个参数 negative在 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)
这是我不知道如何处理并需要帮助的错误消息:
Traceback (most recent call last): File "test.py", line 19, in
Integral1 = quad(Integrand1, low, threshold, args=(low, up, threshold, alpha, beta)) File
"/home/username/anaconda3/lib/python3.6/site-packages/mpmath/calculus/quadrature.py",
line 748, in quad
points[0], prec, epsilon, m, verbose) File "/home/username/anaconda3/lib/python3.6/site-packages/mpmath/calculus/quadrature.py",
line 215, in summation
for i in xrange(len(points)-1): TypeError: object of type 'float' has no len()
我只能猜测原因可能是 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,以及 quad 的 args
参数中列出的所有内容。
上面的代码没有抛出任何错误。我不确定该操作在数学上是否有意义,因为您将 threshold
用于缩放和上限,但这是另一回事了。
我正在尝试使用 scipy.integrate.quad.
计算两个积分,但是,由于 gamma 函数没有定义第一个参数 negative在 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)
这是我不知道如何处理并需要帮助的错误消息:
Traceback (most recent call last): File "test.py", line 19, in Integral1 = quad(Integrand1, low, threshold, args=(low, up, threshold, alpha, beta)) File "/home/username/anaconda3/lib/python3.6/site-packages/mpmath/calculus/quadrature.py", line 748, in quad points[0], prec, epsilon, m, verbose) File "/home/username/anaconda3/lib/python3.6/site-packages/mpmath/calculus/quadrature.py", line 215, in summation for i in xrange(len(points)-1): TypeError: object of type 'float' has no len()
我只能猜测原因可能是 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,以及 quad 的 args
参数中列出的所有内容。
上面的代码没有抛出任何错误。我不确定该操作在数学上是否有意义,因为您将 threshold
用于缩放和上限,但这是另一回事了。