NAG C Library Function Document

nag_2d_spline_interpolant (e01dac)


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


#include <nag.h>
#include <nage01.h>
void  nag_2d_spline_interpolant (Integer mx, Integer my, const double x[], const double y[], const double f[], Nag_2dSpline *spline, NagError *fail)


nag_2d_spline_interpolant (e01dac) determines a bicubic spline interpolant to the set of data points x q , y r , f q,r , for q=1,2,, m x  and r=1,2,, 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 x  and N j y  denote normalized cubic B-splines, the former defined on the knots λ i  to λ i+4  and the latter on the knots μ j  to μ 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 nag_2d_spline_eval (e02dec), nag_2d_spline_eval_rect (e02dfc) and nag_2d_spline_deriv_rect (e02dhc) as described in Section 9.2.


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


1:     mx IntegerInput
2:     my IntegerInput
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: mx4  and my4 .
3:     x[mx] const doubleInput
4:     y[my] const doubleInput
On entry: x[q-1]  and y[r-1]  must contain x q , for q=1,2,, m x , and y r , for r=1,2,, m y , respectively.
  • x[q-1] < x[q] , for q=1,2,, m x - 1 ;
  • y[r-1] < y[r] , for r=1,2,, m y - 1 .
5:     f[mx×my] const doubleInput
On entry: f[ m y × q-1 + r - 1 ]  must contain f q,r , for q=1,2,, m x  and r=1,2,, m y .
6:     spline Nag_2dSpline *
Pointer to structure of type Nag_2dSpline with the following members:
On exit: nx and 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.
lamdadouble *Output
On exit: the pointer to which memory of size nx is internally allocated. lamda contains the complete set of knots λ i  associated with the x  variable, i.e., the interior knots lamda[4] , lamda[5] , , lamda[nx-5] , as well as the additional knots lamda[0] = lamda[1] = lamda[2] = lamda[3] = x[0]  and lamda[nx-4] = lamda[nx-3] = lamda[nx-2] = lamda[nx-1] = x[mx-1]  needed for the B-spline representation.
mudouble *Output
On exit: the pointer to which memory of size ny is internally allocated. mu contains the corresponding complete set of knots μ i  associated with the y  variable.
cdouble *Output
On exit: the pointer to which memory of size mx×my  is internally allocated. c holds the coefficients of the spline interpolant. c[ m y × i-1 + j - 1 ]  contains the coefficient c ij  described in Section 3.
Note that when the information contained in the pointers lamda, mu and c is no longer of use, or before a new call to nag_2d_spline_interpolant (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 fail.code  NE_NOERROR.
7:     fail NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

Error Indicators and Warnings

Dynamic memory allocation failed.
An intermediate set of linear equations is singular, the data is too ill-conditioned to compute B-spline coefficients.
On entry, mx=value.
Constraint: mx4.
On entry, my=value.
Constraint: my4.
The sequence x is not strictly increasing: x[value] = value, x[value] = value.
The sequence y is not strictly increasing: y[value] = value, y[value] = value.


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 ε , where ε  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.

Parallelism and Performance

nag_2d_spline_interpolant (e01dac) is not threaded in any implementation.

Further Comments

The time taken by nag_2d_spline_interpolant (e01dac) is approximately proportional to m x m y .

Outline of Method Used

The process of computing the spline consists of the following steps:
1. choice of the interior x -knots λ 5 , λ 6 , , λ m x  as λ i = x i-2 , for i=5,6,, m x ,
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. 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 μ 5 , μ 6 , , μ m y  as μ i = y i-2 , for i=5,6,, m y ,
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. use of Gaussian elimination to reduce this system to band triangular form,
8. solution of this triangular system for CT  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.

Evaluation of Computed Spline

The values of the computed spline at the points tx r-1 , ty r-1 , for r=1,2,,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 nag_2d_spline_interpolant (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 tx q-1 , for q=1,2,,kx, and the y  coordinates stored in ty r-1 , for r=1,2,,ky, returning the results in the array fg which is of length at least kx × 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 nag_2d_spline_interpolant (e01dac). The result of the spline evaluated at grid point q,r  is returned in element ky × q-1 + r - 1  of the array fg.


This program reads in values of m x , x q , for q=1,2,, m x , m y  and y r , for r=1,2,, m y , followed by values of the ordinates f q,r  defined at the grid points x q , y r . It then calls nag_2d_spline_interpolant (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.

Program Text

Program Text (e01dace.c)

Program Data

Program Data (e01dace.d)

Program Results

Program Results (e01dace.r)