## module cubicSpline ''' k = curvatures(xData,yData). Returns the curvatures of cubic spline at its knots. y = evalSpline(xData,yData,k,x). Evaluates cubic spline at x. The curvatures k can be computed with the function 'curvatures'. ''' from numarray import zeros,ones,Float64,array from LUdecomp3 import * def curvatures(xData,yData): n = len(xData) - 1 c = zeros((n),type=Float64) d = ones((n+1),type=Float64) e = zeros((n),type=Float64) k = zeros((n+1),type=Float64) c[0:n-1] = xData[0:n-1] - xData[1:n] d[1:n] = 2.0*(xData[0:n-1] - xData[2:n+1]) e[1:n] = xData[1:n] - xData[2:n+1] k[1:n] =6.0*(yData[0:n-1] - yData[1:n]) \ /(xData[0:n-1] - xData[1:n]) \ -6.0*(yData[1:n] - yData[2:n+1]) \ /(xData[1:n] - xData[2:n+1]) LUdecomp3(c,d,e) LUsolve3(c,d,e,k) return k def evalSpline(xData,yData,k,x): def findSegment(xData,x): iLeft = 0 iRight = len(xData)- 1 while 1: if (iRight-iLeft) <= 1: return iLeft i =(iLeft + iRight)/2 if x < xData[i]: iRight = i else: iLeft = i i = findSegment(xData,x) h = xData[i] - xData[i+1] y = ((x - xData[i+1])**3/h - (x - xData[i+1])*h)*k[i]/6.0 \ - ((x - xData[i])**3/h - (x - xData[i])*h)*k[i+1]/6.0 \ + (yData[i]*(x - xData[i+1]) \ - yData[i+1]*(x - xData[i]))/h return y