将函数中生成的数组传递给python中另一个调用的函数(集成)

将函数中生成的数组传递给python中另一个调用的函数(集成),python,scipy,numerical-integration,Python,Scipy,Numerical Integration,我在做数值积分,要积分的函数用三次样条表示。三次样条曲线在函数MEcomputeassplc 现在,实际执行插值的被积函数需要三次样条线数组,因此我需要将splc传递给这个新函数。我被困在这里了 # function defining the integrand which uses the spline coef array to give interpolated values def integrand(xpoint): spline_array=splc result=i

我在做数值积分,要积分的函数用三次样条表示。三次样条曲线在函数
MEcompute
as
splc

现在,实际执行插值的被积函数需要三次样条线数组,因此我需要将
splc
传递给这个新函数。我被困在这里了

# function defining the integrand which uses the spline coef array to give interpolated values
def integrand(xpoint):
    spline_array=splc
    result=interpolate.splev(xpoint,spline_array,der=0)
    return result

#----------------------------------------
# function to the matrix element for certain rovibrational state
def MEcompute(psi1,psi2,psi_r, parameter, parameter_r ):

        # step 1: gen cubic spline coefs.
        splc=interpolate.splrep(parameter_r,parameter,s=0)

        # generate interpolated parameter for same xaxis as psi
        parameter_interp=interpolate.splev(psi_r,splc,der=0)

        # compute the pointwise products
        p1=np.multiply(psi1,psi2)
        p2=np.multiply(p1,psi_r)
        p3=np.multiply(p2,psi_r)
        product=np.multiply(p3,parameter_interp)

        # step 1: gen cubic spline coefs
        splc=interpolate.splrep(psi_r,product,s=0)

        # compute the integral using adaptive Quadrature
        #result=integrate.quadrature(integrand,0.2,4.48,tol=1.0e-9,maxiter=500)
        result=integrate.quadrature(integrand,0.2,4.48,tol=1.0e-9,maxiter=500)
        print("<psi1|parameter|psi2> = ",result)

#----------------------------------------
# computing the value

MEcompute(v1,v2,rwave,parameter1,distance)
#----------------------------------------
这是因为
integrand
函数没有看到在函数
MEcompute
中启动的
splc
数组

#----------------------------------------
# function to the matrix element for certain rovibrational state
def MEcompute(psi1,psi2,psi_r, parameter, parameter_r ):

        # step 1: gen cubic spline coefs.
        splc=interpolate.splrep(parameter_r,parameter,s=0)

        # function defining the integrand which uses the spline coef array to give interpolated values
        def integrand(xpoint):
            return interpolate.splev(xpoint,splc,der=0)

        # generate interpolated parameter for same xaxis as psi
        parameter_interp=interpolate.splev(psi_r,splc,der=0)

        # compute the pointwise products
        p1=np.multiply(psi1,psi2)
        p2=np.multiply(p1,psi_r)
        p3=np.multiply(p2,psi_r)
        product=np.multiply(p3,parameter_interp)

        # step 1: gen cubic spline coefs
        splc=interpolate.splrep(psi_r,product,s=0)

        # compute the integral using adaptive Quadrature
        #result=integrate.quadrature(integrand,0.2,4.48,tol=1.0e-9,maxiter=500)
        result=integrate.quadrature(integrand,0.2,4.48,tol=1.0e-9,maxiter=500)
        print("<psi1|parameter|psi2> = ",result)

#----------------------------------------
# computing the value

MEcompute(v1,v2,rwave,parameter1,distance)
#----------------------------------------
现在我有一个想法来克服这个问题:

  • 将数组
    splc
    MEcompute
    导出为txt文件,然后在
    integrand
    函数中加载此txt文件。这肯定会增加计算时间
  • 有人能提出更好的方法吗。

    使用向函数传递额外参数以进行集成:

    result = integrate.quadrature(integrand, 0.2, 4.48,
                                  tol=1.0e-9, maxiter=500,
                                  args=(splc,))
    
    并修改被积函数以接受参数:

    def integrand(xpoint, splc):
        spline_array=splc
        result=interpolate.splev(xpoint,spline_array,der=0)
        return result
    

    您还可以尝试在
    MEcompute
    中定义
    integrand

    #----------------------------------------
    # function to the matrix element for certain rovibrational state
    def MEcompute(psi1,psi2,psi_r, parameter, parameter_r ):
    
            # step 1: gen cubic spline coefs.
            splc=interpolate.splrep(parameter_r,parameter,s=0)
    
            # function defining the integrand which uses the spline coef array to give interpolated values
            def integrand(xpoint):
                return interpolate.splev(xpoint,splc,der=0)
    
            # generate interpolated parameter for same xaxis as psi
            parameter_interp=interpolate.splev(psi_r,splc,der=0)
    
            # compute the pointwise products
            p1=np.multiply(psi1,psi2)
            p2=np.multiply(p1,psi_r)
            p3=np.multiply(p2,psi_r)
            product=np.multiply(p3,parameter_interp)
    
            # step 1: gen cubic spline coefs
            splc=interpolate.splrep(psi_r,product,s=0)
    
            # compute the integral using adaptive Quadrature
            #result=integrate.quadrature(integrand,0.2,4.48,tol=1.0e-9,maxiter=500)
            result=integrate.quadrature(integrand,0.2,4.48,tol=1.0e-9,maxiter=500)
            print("<psi1|parameter|psi2> = ",result)
    
    #----------------------------------------
    # computing the value
    
    MEcompute(v1,v2,rwave,parameter1,distance)
    #----------------------------------------
    
    #----------------------------------------
    #某一振动状态下矩阵元的函数
    def MEcompute(psi1、psi2、psi_r、参数、参数r):
    #步骤1:生成三次样条曲线系数。
    splc=interpolate.splrep(参数,参数,s=0)
    #定义被积函数的函数,该函数使用样条曲线系数数组给出插值
    def被积函数(xpoint):
    返回interpolate.splev(xpoint,splc,der=0)
    #为与psi相同的X轴生成插值参数
    参数interp=interpolate.splev(psi\r,splc,der=0)
    #计算逐点积
    p1=np.乘法(psi1,psi2)
    p2=np.乘法(p1,psi\r)
    p3=np.乘法(p2,磅/平方英寸)
    乘积=np.乘法(p3,参数_interp)
    #步骤1:生成三次样条系数
    splc=interpolate.splrep(psi\r,乘积,s=0)
    #使用自适应求积计算积分
    #结果=积分正交(被积函数,0.2,4.48,tol=1.0e-9,maxiter=500)
    结果=积分正交(被积函数,0.2,4.48,tol=1.0e-9,maxiter=500)
    打印(“=”,结果)
    #----------------------------------------
    #计算值
    MEcompute(v1、v2、rwave、参数1、距离)
    #----------------------------------------
    
    请注意,
    (splc)
    不是一个元组,而是括号中的一个表达式,您输入了错误的。此外,您必须将
    splc
    作为
    被积函数的显式参数。如果在
    MEcompute
    函数中定义
    integrand
    函数,它可以看到所有变量,包括
    splc
    。感谢您的回答。您的解决方案有效,但我只能选择一个可接受的答案。谢谢。工作起来很有魅力。