# NAG CL Interfacee01dac (dim2_​spline_​grid)

## 1Purpose

e01dac computes a bicubic spline interpolating surface through a set of data values, given on a rectangular grid in the $x$-$y$ plane.

## 2Specification

 #include
 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.

## 3Description

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 B-spline representation
 $s x,y = ∑ i=1 m x ∑ j=1 m y c ij M i x N j y$
such that
 $s x q , y r = f q,r ,$
where ${M}_{i}\left(x\right)$ and ${N}_{j}\left(y\right)$ denote normalized cubic B-splines, 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 B-splines.
Values and derivatives of the computed spline can subsequently be computed by calling e02dec, e02dfc and e02dhc as described in Section 9.2.

## 4References

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 B-splines 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

## 5Arguments

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[q-1\right]$ and ${\mathbf{y}}\left[r-1\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}}×{\mathbf{my}}\right]$const double Input
On entry: ${\mathbf{f}}\left[{m}_{y}×\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:
nxIntegerOutput
nyIntegerOutput
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.
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 B-spline representation.
mudouble *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.
cdouble *Output
On exit: the pointer to which memory of size ${\mathbf{mx}}×{\mathbf{my}}$ is internally allocated. $\mathbf{c}$ holds the coefficients of the spline interpolant. $\mathbf{c}\left[{m}_{y}×\left(i-1\right)+j-1\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).

## 6Error 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 ill-conditioned to compute B-spline coefficients.
NE_INT_ARG_LT
On entry, ${\mathbf{mx}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mx}}\ge 4$.
On entry, ${\mathbf{my}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{my}}\ge 4$.
NE_NOT_STRICTLY_INCREASING
The sequence x is not strictly increasing: ${\mathbf{x}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$, ${\mathbf{x}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$.
The sequence y is not strictly increasing: ${\mathbf{y}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$, ${\mathbf{y}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$.

## 7Accuracy

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.

## 8Parallelism and Performance

e01dac is not threaded in any implementation.

The time taken by e01dac is approximately proportional to ${m}_{x}{m}_{y}$.

### 9.1Outline of Method Used

The process of computing the spline consists of the following steps:
1. 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. 2.formation of the system
 $A x E = F ,$
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 B-splines 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. 3.use of Gaussian elimination to reduce this system to band triangular form,
4. 4.solution of this triangular system for $E$,
5. 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. 6.formation of the system
 $A y CT = ET ,$
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. 7.use of Gaussian elimination to reduce this system to band triangular form,
8. 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.2Evaluation 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}×\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}×\left(q-1\right)+r-1\right]$ of the array fg.

## 10Example

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 B-spline coefficients. Finally it evaluates the spline at a small sample of points on a rectangular grid.

### 10.1Program Text

Program Text (e01dace.c)

### 10.2Program Data

Program Data (e01dace.d)

### 10.3Program Results

Program Results (e01dace.r)