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:
- Export the array
splcfromMEcomputemaybe as a txt file, then load this txt file in theintegrandfunction. This will definitely increase the computation time.
Can someone suggest a better way to do this.
(splc)is not a tuple, but an expression in brackets, and you've mistyped the keyword argumentargs.splcan explicit argument tointegrand.integrandfunction inside theMEcomputefunction, it can see all variables, includingsplc.