代码之家  ›  专栏  ›  技术社区  ›  Rebel

执行scipy的四元积分方法时显示“TypeError”消息。与mpmath函数集成

  •  0
  • Rebel  · 技术社区  · 7 年前

    我试着用 scipy.integrate.quad. 然而,自 伽马射线 具有的函数 消极的 第一个参数未在中定义 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)
    

    以下是我不知道如何处理的错误消息,需要帮助:

    回溯(最近一次调用last):文件“test.py”,第19行,in Integral1=quad(Integrand1,low,threshold,args=(low,up,threshold,alpha,beta))文件 “/home/username/anaconda3/lib/python3.6/site packages/mpmath/calcular/quadrature.py”, 线路748,四元 点[0],prec,epsilon,m,verbose)文件“/home/username/anaconda3/lib/python3.6/site-packages/mpmath/calculation/quadrature.py”, 第215行,总计 对于xrange中的i(len(points)-1):类型错误:“float”类型的对象没有len()

    我只能猜测原因可能是 quad 函数与使用定义的积分不兼容 mpmath.

    1 回复  |  直到 7 年前
        1
  •  1
  •   user6655984 user6655984    7 年前

    导入语句

    不要从两个地方导入*,这会导致名称冲突。MpMath有自己的 quad 方法,它取代了SciPy的方法 方庭 在您的代码中。

    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))
    

    请注意,传递给被积函数的参数与其预期接收的参数相匹配。他们得到了x,所有的东西都列在 args quad的参数。

    上述代码不会引发任何错误。我不确定这个运算在数学上是否有意义 threshold

    推荐文章