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

Passing array generated within a function to another called function (integration) in python

我正在进行数值积分,其中要积分的函数使用三次样条表示。三次样条在函数 MEcompute 中启动为 splc

现在实际进行插值的 integrand 需要三次样条数组,因此我需要将 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)
#----------------------------------------

我收到错误,

NameError: name 'splc' is not defined

这是因为 integrand 函数没有看到函数 MEcompute 中启动的 splc 数组。

现在我有一个想法来克服这个问题:

  1. MEcompute 导出数组 splc 可能作为 txt 文件,然后在 integrand 函数中加载此 txt 文件。这肯定会增加计算时间。

有人可以建议更好的方法吗?

使用 args= keyword argument 将额外参数传递给要集成的函数:

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