NAG CL Interface
e01dac (dim2_spline_grid)
1
Purpose
e01dac computes a bicubic spline interpolating surface through a set of data values, given on a rectangular grid in the $x$$y$ plane.
2
Specification
void 
e01dac (Integer mx,
Integer my,
const double x[],
const double y[],
const double f[],
Nag_2dSpline *spline,
NagError *fail) 

The function may be called by the names: e01dac, nag_interp_dim2_spline_grid or nag_2d_spline_interpolant.
3
Description
e01dac determines a bicubic spline interpolant to the set of data points
$\left({x}_{\mathit{q}},{y}_{\mathit{r}},{f}_{\mathit{q},\mathit{r}}\right)$, for
$\mathit{q}=1,2,\dots ,{m}_{x}$ and
$\mathit{r}=1,2,\dots ,{m}_{y}$. The spline is given in the Bspline representation
such that
where
${M}_{i}\left(x\right)$ and
${N}_{j}\left(y\right)$ denote normalized cubic Bsplines, the former defined on the knots
${\lambda}_{i}$ to
${\lambda}_{i+4}$ and the latter on the knots
${\mu}_{j}$ to
${\mu}_{j+4}$, and the
${c}_{ij}$ are the spline coefficients. These knots, as well as the coefficients, are determined by the function, which is derived from the routine B2IRE in
Anthony et al. (1982). The method used is described in
Section 9.1.
For further information on splines, see
Hayes and Halliday (1974) for bicubic splines and
de Boor (1972) for normalized Bsplines.
Values and derivatives of the computed spline can subsequently be computed by calling
e02dec,
e02dfc and
e02dhc as described in
Section 9.2.
4
References
Anthony G T, Cox M G and Hayes J G (1982) DASL – Data Approximation Subroutine Library National Physical Laboratory
Cox M G (1975) An algorithm for spline interpolation J. Inst. Math. Appl. 15 95–108
de Boor C (1972) On calculating with Bsplines J. Approx. Theory 6 50–62
Hayes J G and Halliday J (1974) The least squares fitting of cubic spline surfaces to general data sets J. Inst. Math. Appl. 14 89–103
5
Arguments

1:
$\mathbf{mx}$ – Integer
Input

2:
$\mathbf{my}$ – Integer
Input

On entry:
mx and
my must specify
${m}_{x}$ and
${m}_{y}$ respectively, the number of points along the
$x$ and
$y$ axis that define the rectangular grid.
Constraint:
${\mathbf{mx}}\ge 4$ and ${\mathbf{my}}\ge 4$.

3:
$\mathbf{x}\left[{\mathbf{mx}}\right]$ – const double
Input

4:
$\mathbf{y}\left[{\mathbf{my}}\right]$ – const double
Input

On entry: ${\mathbf{x}}\left[q1\right]$ and ${\mathbf{y}}\left[r1\right]$ must contain ${x}_{\mathit{q}}$, for $\mathit{q}=1,2,\dots ,{m}_{x}$, and ${y}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,{m}_{y}$, respectively.
Constraints:
 ${\mathbf{x}}\left[\mathit{q}1\right]<{\mathbf{x}}\left[\mathit{q}\right]$, for $\mathit{q}=1,2,\dots ,{m}_{x}1$;
 ${\mathbf{y}}\left[\mathit{r}1\right]<{\mathbf{y}}\left[\mathit{r}\right]$, for $\mathit{r}=1,2,\dots ,{m}_{y}1$.

5:
$\mathbf{f}\left[{\mathbf{mx}}\times {\mathbf{my}}\right]$ – const double
Input

On entry: ${\mathbf{f}}\left[{m}_{y}\times \left(\mathit{q}1\right)+\mathit{r}1\right]$ must contain ${f}_{\mathit{q},\mathit{r}}$, for $\mathit{q}=1,2,\dots ,{m}_{x}$ and $\mathit{r}=1,2,\dots ,{m}_{y}$.

6:
$\mathbf{spline}$ – Nag_2dSpline *

Pointer to structure of type Nag_2dSpline with the following members:
 nx – IntegerOutput
 ny – IntegerOutput

On exit: $\mathbf{nx}$ and $\mathbf{ny}$ contain ${m}_{x}+4$ and ${m}_{y}+4$, the total number of knots of the computed spline with respect to the $x$ and $y$ variables, respectively.
 lamda – double *Output

On exit: the pointer to which memory of size $\mathbf{nx}$ is internally allocated. $\mathbf{lamda}$ contains the complete set of knots ${\lambda}_{i}$ associated with the $x$ variable, i.e., the interior knots $\mathbf{lamda}\left[4\right]$, $\mathbf{lamda}\left[5\right]$, $\dots $, $\mathbf{lamda}\left[\mathbf{nx}5\right]$, as well as the additional knots $\mathbf{lamda}\left[0\right]=\mathbf{lamda}\left[1\right]=\mathbf{lamda}\left[2\right]=\mathbf{lamda}\left[3\right]={\mathbf{x}}\left[0\right]$ and $\mathbf{lamda}\left[\mathbf{nx}4\right]=\mathbf{lamda}\left[\mathbf{nx}3\right]=\mathbf{lamda}\left[\mathbf{nx}2\right]=\mathbf{lamda}\left[\mathbf{nx}1\right]={\mathbf{x}}\left[{\mathbf{mx}}1\right]$ needed for the Bspline representation.
 mu – double *Output

On exit: the pointer to which memory of size $\mathbf{ny}$ is internally allocated. $\mathbf{mu}$ contains the corresponding complete set of knots ${\mu}_{i}$ associated with the $y$ variable.
 c – double *Output

On exit: the pointer to which memory of size
${\mathbf{mx}}\times {\mathbf{my}}$ is internally allocated.
$\mathbf{c}$ holds the coefficients of the spline interpolant.
$\mathbf{c}\left[{m}_{y}\times \left(i1\right)+j1\right]$ contains the coefficient
${c}_{ij}$ described in
Section 3.
Note that when the information contained in the pointers
$\mathbf{lamda}$,
$\mathbf{mu}$ and
$\mathbf{c}$ is no longer of use, or before a new call to
e01dac with the same
spline, you should free these pointers using the NAG macro
NAG_FREE. This storage will not have been allocated if this function returns with
${\mathbf{fail}}\mathbf{.}\mathbf{code}\ne $ NE_NOERROR.

7:
$\mathbf{fail}$ – NagError *
Input/Output

The NAG error argument (see
Section 7 in the Introduction to the NAG Library CL Interface).
6
Error Indicators and Warnings
 NE_ALLOC_FAIL

Dynamic memory allocation failed.
 NE_DATA_ILL_CONDITIONED

An intermediate set of linear equations is singular, the data is too illconditioned to compute Bspline coefficients.
 NE_INT_ARG_LT

On entry, ${\mathbf{mx}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{mx}}\ge 4$.
On entry, ${\mathbf{my}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{my}}\ge 4$.
 NE_NOT_STRICTLY_INCREASING

The sequence
x is not strictly increasing:
${\mathbf{x}}\left[\u2329\mathit{\text{value}}\u232a\right]=\u2329\mathit{\text{value}}\u232a$,
${\mathbf{x}}\left[\u2329\mathit{\text{value}}\u232a\right]=\u2329\mathit{\text{value}}\u232a$.
The sequence
y is not strictly increasing:
${\mathbf{y}}\left[\u2329\mathit{\text{value}}\u232a\right]=\u2329\mathit{\text{value}}\u232a$,
${\mathbf{y}}\left[\u2329\mathit{\text{value}}\u232a\right]=\u2329\mathit{\text{value}}\u232a$.
7
Accuracy
The main sources of rounding errors are in steps
1,
3,
6 and
7 of the algorithm described in
Section 9.1. It can be shown (
Cox (1975)) that the matrix
${A}_{x}$ formed in step
2 has elements differing relatively from their true values by at most a small multiple of
$3\epsilon $, where
$\epsilon $ is the
machine precision.
${A}_{x}$ is ‘totally positive’, and a linear system with such a coefficient matrix can be solved quite safely by elimination without pivoting. Similar comments apply to steps
6 and
7. Thus the complete process is numerically stable.
8
Parallelism and Performance
e01dac is not threaded in any implementation.
The time taken by e01dac is approximately proportional to ${m}_{x}{m}_{y}$.
9.1
Outline of Method Used
The process of computing the spline consists of the following steps:

1.choice of the interior $x$knots ${\lambda}_{5}$, ${\lambda}_{6},\dots ,{\lambda}_{{m}_{x}}$ as ${\lambda}_{\mathit{i}}={x}_{\mathit{i}2}$, for $\mathit{i}=5,6,\dots ,{m}_{x}$,

2.formation of the system
where ${A}_{x}$ is a band matrix of order ${m}_{x}$ and bandwidth $4$, containing in its $q$th row the values at ${x}_{q}$ of the Bsplines in $x$, $F$ is the ${m}_{x}$ by ${m}_{y}$ rectangular matrix of values ${f}_{q,r}$, and $E$ denotes an ${m}_{x}$ by ${m}_{y}$ rectangular matrix of intermediate coefficients,

3.use of Gaussian elimination to reduce this system to band triangular form,

4.solution of this triangular system for $E$,

5.choice of the interior $y$ knots ${\mu}_{5}$, ${\mu}_{6},\dots ,{\mu}_{{m}_{y}}$ as ${\mu}_{\mathit{i}}={y}_{\mathit{i}2}$, for $\mathit{i}=5,6,\dots ,{m}_{y}$,

6.formation of the system
where ${A}_{y}$ is the counterpart of ${A}_{x}$ for the $y$ variable, and $C$ denotes the ${m}_{x}$ by ${m}_{y}$ rectangular matrix of values of ${c}_{ij}$,

7.use of Gaussian elimination to reduce this system to band triangular form,

8.solution of this triangular system for ${C}^{\mathrm{T}}$ and hence $C$.
For computational convenience, steps
2 and
3, and likewise steps
6 and
7, are combined so that the formation of
${A}_{x}$ and
${A}_{y}$ and the reductions to triangular form are carried out one row at a time.
9.2
Evaluation of Computed Spline
The values of the computed spline at the points
$\left(\mathbf{tx}\left[\mathit{r}1\right],\mathbf{ty}\left[\mathit{r}1\right]\right)$, for
$\mathit{r}=1,2,\dots ,\mathbf{n}$, may be obtained in the array
ff, of length at least
n, by the following call:
e02dec (n, tx, ty, ff, &spline, &fail)
where
spline is a structure of type Nag_2dSpline which is the output argument of
e01dac.
To evaluate the computed spline on a
kx by
ky rectangular grid of points in the
$x$
$y$ plane, which is defined by the
$x$ coordinates stored in
$\mathbf{tx}\left[\mathit{q}1\right]$, for
$\mathit{q}=1,2,\dots ,\mathbf{kx}$, and the
$y$ coordinates stored in
$\mathbf{ty}\left[\mathit{r}1\right]$, for
$\mathit{r}=1,2,\dots ,\mathbf{ky}$, returning the results in the array
fg which is of length at least
$\mathbf{kx}\times \mathbf{ky}$, the following call may be used:
e02dfc (kx, ky, tx, ty, fg, &spline, &fail)
where
spline is a structure of type Nag_2dSpline which is the output argument of
e01dac. The result of the spline evaluated at grid point
$\left(q,r\right)$ is returned in element
$\left[\mathbf{ky}\times \left(q1\right)+r1\right]$ of the array
fg.
10
Example
This program reads in values of ${m}_{x}$, ${x}_{\mathit{q}}$, for $\mathit{q}=1,2,\dots ,{m}_{x}$, ${m}_{y}$ and ${y}_{\mathit{r}}$, for $\mathit{r}=1,2,\dots ,{m}_{y}$, followed by values of the ordinates ${f}_{q,r}$ defined at the grid points $\left({x}_{q},{y}_{r}\right)$. It then calls e01dac to compute a bicubic spline interpolant of the data values, and prints the values of the knots and Bspline coefficients. Finally it evaluates the spline at a small sample of points on a rectangular grid.
10.1
Program Text
10.2
Program Data
10.3
Program Results