1

I am doing numerical integration, where the function to be integrated is represented using cubic spline. The cubic spline is initiated in the function MEcompute as splc

Now the integrand which is actually doing the interpolation needs ths cubic spline array hence I need to pass the splc to this new function. And here I am stuck.

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

I receive the error,

NameError: name 'splc' is not defined

which happeds because the integrand function does not see the splc array initiated within the function MEcompute.

Now I have one idea to get over this:

  1. Export the array splc from MEcompute maybe as a txt file, then load this txt file in the integrand function. This will definitely increase the computation time.

Can someone suggest a better way to do this.

3
  • 1
    Note that (splc) is not a tuple, but an expression in brackets, and you've mistyped the keyword argument args. Commented Jan 2, 2018 at 14:19
  • Also, you have to make splc an explicit argument to integrand. Commented Jan 2, 2018 at 14:20
  • 1
    If you define the integrand function inside the MEcompute function, it can see all variables, including splc. Commented Jan 2, 2018 at 14:21

2 Answers 2

1

Use the args= keyword argument to pass extra arguments to the function to integrate:

result = integrate.quadrature(integrand, 0.2, 4.48,
                              tol=1.0e-9, maxiter=500,
                              args=(splc,))

and modify your integrand to accept the argument:

def integrand(xpoint, splc):
    spline_array=splc
    result=interpolate.splev(xpoint,spline_array,der=0)
    return result
Sign up to request clarification or add additional context in comments.

Comments

1

You could also try defining integrand within MEcompute.

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

1 Comment

Thanks for the answer. Your solution works but I can select only one accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.