NAG Library Function Document

nag_1d_spline_deriv (e02bcc)


    1  Purpose
    7  Accuracy


nag_1d_spline_deriv (e02bcc) evaluates a cubic spline and its first three derivatives from its B-spline representation.


#include <nag.h>
#include <nage02.h>
void  nag_1d_spline_deriv (Nag_DerivType derivs, double x, double s[], Nag_Spline *spline, NagError *fail)


nag_1d_spline_deriv (e02bcc) evaluates the cubic spline s x  and its first three derivatives at a prescribed argument x . It is assumed that s x  is represented in terms of its B-spline coefficients c i , for i=1,2,, n - + 3 and (augmented) ordered knot set λ i , for i=1,2,, n - + 7, (see nag_1d_spline_fit_knots (e02bac)), i.e.,
s x = i=1 q c i N i x  
Here q = n - + 3 , n -  is the number of intervals of the spline and N i x  denotes the normalized B-spline of degree 3 (order 4) defined upon the knots λ i , λ i+1 , , λ i+4 . The prescribed argument x  must satisfy λ 4 x λ n - + 4 .
At a simple knot λ i  (i.e., one satisfying λ i-1 < λ i < λ i+1 ), the third derivative of the spline is in general discontinuous. At a multiple knot (i.e., two or more knots with the same value), lower derivatives, and even the spline itself, may be discontinuous. Specifically, at a point x=u  where (exactly) r  knots coincide (such a point is termed a knot of multiplicity r ), the values of the derivatives of order 4-j , for j=1,2,,r, are in general discontinuous. (Here 1 r 4 ; r > 4  is not meaningful.) You must specify whether the value at such a point is required to be the left- or right-hand derivative.
The method employed is based upon:
(i) carrying out a binary search for the knot interval containing the argument x  (see Cox (1978)),
(ii) evaluating the nonzero B-splines of orders 1,2,3 and 4 by recurrence (see Cox (1972) and Cox (1978)),
(iii) computing all derivatives of the B-splines of order 4 by applying a second recurrence to these computed B-spline values (see de Boor (1972)),
(iv) multiplying the 4th-order B-spline values and their derivative by the appropriate B-spline coefficients, and summing, to yield the values of s x  and its derivatives.
nag_1d_spline_deriv (e02bcc) can be used to compute the values and derivatives of cubic spline fits and interpolants produced by nag_1d_spline_fit_knots (e02bac), nag_1d_spline_fit (e02bec) or nag_1d_spline_interpolant (e01bac).
If only values and not derivatives are required, nag_1d_spline_evaluate (e02bbc) may be used instead of nag_1d_spline_deriv (e02bcc), which takes about 50% longer than nag_1d_spline_evaluate (e02bbc).


Cox M G (1972) The numerical evaluation of B-splines J. Inst. Math. Appl. 10 134–149
Cox M G (1978) The numerical evaluation of a spline from its B-spline representation J. Inst. Math. Appl. 21 135–143
de Boor C (1972) On calculating with B-splines J. Approx. Theory 6 50–62


1:     derivs Nag_DerivTypeInput
On entry: derivs, of type Nag_DerivType, specifies whether left- or right-hand values of the spline and its derivatives are to be computed (see Section 3). Left- or right-hand values are formed according to whether derivs is equal to Nag_LeftDerivs or Nag_RightDerivs respectively. If x  does not coincide with a knot, the value of derivs is immaterial. If x = splinelamda[3] , right-hand values are computed, and if x = splinelamda[splinen-4] ), left-hand values are formed, regardless of the value of derivs.
Constraint: derivs=Nag_LeftDerivs or Nag_RightDerivs.
2:     x doubleInput
On entry: the argument x  at which the cubic spline and its derivatives are to be evaluated.
Constraint: splinelamda[3] x splinelamda[splinen-4] .
3:     s[4] doubleOutput
On exit: s[j]  contains the value of the j th derivative of the spline at the argument x , for j=0,1,2,3. Note that s[0]  contains the value of the spline.
4:     spline Nag_Spline *
Pointer to structure of type Nag_Spline with the following members:
On entry: n - + 7 , where n -  is the number of intervals of the spline (which is one greater than the number of interior knots, i.e., the knots strictly within the range λ 4  to λ n - + 4  over which the spline is defined).
Constraint: splinen8 .
On entry: a pointer to which memory of size splinen must be allocated. splinelamda[j-1]  must be set to the value of the j th member of the complete set of knots, λ j , for j=1,2,, n - + 7.
Constraint: the λ j  must be in nondecreasing order with splinelamda[splinen-4] > splinelamda[3] .
On entry: a pointer to which memory of size splinen-4  must be allocated. splinec holds the coefficient c i  of the B-spline N i x , for i=1,2,, n - + 3.
Under normal usage, the call to nag_1d_spline_deriv (e02bcc) will follow a call to nag_1d_spline_fit_knots (e02bac), nag_1d_spline_interpolant (e01bac) or nag_1d_spline_fit (e02bec). In that case, the structure spline will have been set up correctly for input to nag_1d_spline_deriv (e02bcc).
5:     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

On entry, x must satisfy splinelamda[3] x splinelamda[splinen-4] :
splinelamda[3] = value, x=value , splinelamda[value] = value.
On entry, argument derivs had an illegal value.
On entry, splinen must not be less than 8: splinen=value .
On entry, the cubic spline range is invalid:
splinelamda[3] = value while splinelamda[splinen-4] = value.
These must satisfy splinelamda[3] < splinelamda[splinen-4] .


The computed value of s x  has negligible error in most practical situations. Specifically, this value has an absolute error bounded in modulus by 18 × c max ×  machine precision, where c max  is the largest in modulus of c j , c j+1 , c j+2  and c j+3 , and j  is an integer such that λ j+3 x λ j+4 . If c j , c j+1 , c j+2  and c j+3  are all of the same sign, then the computed value of s x  has relative error bounded by 20 ×  machine precision. For full details see Cox (1978).
No complete error analysis is available for the computation of the derivatives of s x . However, for most practical purposes the absolute errors in the computed derivatives should be small.

Parallelism and Performance

nag_1d_spline_deriv (e02bcc) is not threaded in any implementation.

Further Comments

The time taken by this function is approximately linear in log n - + 7 .
Note: the function does not test all the conditions on the knots given in the description of splinelamda in Section 5, since to do this would result in a computation time approximately linear in n - + 7  instead of log n - + 7 . All the conditions are tested in nag_1d_spline_fit_knots (e02bac), however, and the knots returned by nag_1d_spline_interpolant (e01bac) or nag_1d_spline_fit (e02bec) will satisfy the conditions.


Compute, at the 7 arguments x=0 , 1, 2, 3, 4, 5, 6, the left- and right-hand values and first 3 derivatives of the cubic spline defined over the interval 0 x 6  having the 6 interior knots x=1 , 3, 3, 3, 4, 4, the 8 additional knots 0, 0, 0, 0, 6, 6, 6, 6, and the 10 B-spline coefficients 10, 12, 13, 15, 22, 26, 24, 18, 14, 12.
The input data items (using the notation of Section 5) comprise the following values in the order indicated:
n - m
splinelamda[j] for j = 0 , 1 , , n - + 6
splinec[j] , for j = 0 , 1 , , n - + 2
x m values of x
The example program is written in a general form that will enable the values and derivatives of a cubic spline having an arbitrary number of knots to be evaluated at a set of arbitrary points. Any number of datasets may be supplied.

Program Text

Program Text (e02bcce.c)

Program Data

Program Data (e02bcce.d)

Program Results

Program Results (e02bcce.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017