PyPspline examples
- 1-D interpolation using not-a-knot boundary conditions:
import Numeric as N
from pypspline.pspline1_r8 import pspline
eps = 1.e-10
# original data
nx = 11
dx = 1./(nx-1)
x = N.arange(0, 1.+eps, dx)
y = x**3/3
# new grid
nxi = 2*(nx-1)+1
dxi = 1./(nxi-1)
xi = N.arange(0,1.+eps, dxi)
spl = pspline(x)
# compute spline coefficients
spl.setup(y)
# interpolate
yi = spl.interp(xi)
# 1st derivative
ypi = spl.derivative(1, xi)
# 2nd derivative
yppi= spl.derivative(2, xi)
- 2-D interpolation using periodic boundary conditions:
import Numeric as N
from pypspline.pspline2_r8 import pspline, griddata
eps = 1.e-10
# original data
nx, ny = 11, 21
dx, dy = 1./(nx-1), 2*N.pi/(ny-1)
x = N.arange(0, 1.+eps , dx)
y = N.arange(0, 2*N.pi+eps, dy)
xx, yy = griddata(x, y)
f = xx**3 * N.cos(yy)
# new grid
nxi, nyi = 2*(nx-1)+1, 2*(ny-1)+1
dxi, dyi = 1./(nxi-1), 2*N.pi/(nyi-1)
xi = N.arange(0, 1.+eps, dxi)
yi = N.arange(0, 2*N.pi+eps, dyi)
# not-a-knot in x, periodic in y
spl = pspline(x,y, bcs1=None, bcs2=1)
# compute spline coefficients
spl.setup(f)
# interpolate
fi = spl.interp(xi, yi)
# df/dx
fxi = spl.derivative(1,0, xi, yi)
# d^2f/dy^2
fyyi= spl.derivative(0,2, xi, yi)
- 3-D interpolation with mixed slope/second derivative conditions:
import Numeric as N
from pypspline.pspline3_r8 import pspline, griddata
eps = 1.e-10
# original data
nx, ny, nz = 11, 21, 31
dx, dy, dz = 1./(nx-1), 2*N.pi/(ny-1), 2./(nz-1)
x = N.arange(0, 1.+eps , dx)
y = N.arange(0, 2*N.pi+eps, dy)
z = N.arange(0, 2.+eps , dz)
xx, yy, zz = griddata(x, y, z)
# function to interpolate
f = xx**3 * N.cos(yy) + zz**3/3
# new grid
nxi, nyi, nzi = 2*(nx-1)+1, 2*(ny-1)+1, 2*(nz-1)+1
dxi, dyi, dzi = 1./(nxi-1), 2*N.pi/(nyi-1), 2./(nzi-1)
xi = N.arange(0, 1.+eps, dxi)
yi = N.arange(0, 2*N.pi+eps, dyi)
zi = N.arange(0, 2.+eps , dzi)
# not-a-knot in x, periodic in y
# slope @ zmin, 2nd derivative @ zmax
spl = pspline(x,y,z, bcs1=None, bcs2=1, bcs3=(1,2))
# set boundary condition df/dz @ zmin
# (note indexing order: [iz, iy, ix])
spl.bcval3min = zz[ 0,:,:]**2
# set boundary condition d^2f/dz^2 @ zmax
spl.bcval3max = 2*zz[-1,:,:]
# compute spline coefficients
spl.setup(f)
# interpolate
fi = spl.interp(xi, yi, zi)
Alexander Pletzer
Last modified: Thu Apr 1 08:48:17 EST 2004