# NAG Library Routine Document

## 1Purpose

e02bff evaluates a cubic spline and up to its first three derivatives from its B-spline representation at a vector of points. e02bff can be used to compute the values and derivatives of cubic spline fits and interpolants produced by reference to e01baf, e02baf and e02bef.

## 2Specification

Fortran Interface
 Subroutine e02bff ( c, xord, x, nx, s, lds, iwrk,
 Integer, Intent (In) :: start, ncap7, deriv, xord, nx, lds, liwrk Integer, Intent (Inout) :: ixloc(nx), iwrk(liwrk), ifail Real (Kind=nag_wp), Intent (In) :: lamda(ncap7), c(ncap7), x(nx) Real (Kind=nag_wp), Intent (Inout) :: s(lds,*)
#include nagmk26.h
 void e02bff_ (const Integer *start, const Integer *ncap7, const double lamda[], const double c[], const Integer *deriv, const Integer *xord, const double x[], Integer ixloc[], const Integer *nx, double s[], const Integer *lds, Integer iwrk[], const Integer *liwrk, Integer *ifail)

## 3Description

e02bff evaluates the cubic spline $s\left(x\right)$ and optionally derivatives up to order $3$ for a vector of points ${x}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,{n}_{x}$. It is assumed that $s\left(x\right)$ is represented in terms of its B-spline coefficients ${c}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\stackrel{-}{n}+3$, and (augmented) ordered knot set ${\lambda }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\stackrel{-}{n}+7$, (see e02baf and e02bef), i.e.,
 $sx = ∑ i=1q ci Nix .$
Here $q=\stackrel{-}{n}+3$, $\stackrel{-}{n}$ is the number of intervals of the spline and ${N}_{i}\left(x\right)$ denotes the normalized B-spline of degree $3$ (order $4$) defined upon the knots ${\lambda }_{i},{\lambda }_{i+1},\dots ,{\lambda }_{i+4}$. The knots ${\lambda }_{5},{\lambda }_{6},\dots ,{\lambda }_{\stackrel{-}{n}+3}$ are the interior knots. The remaining knots, ${\lambda }_{1}$, ${\lambda }_{2}$, ${\lambda }_{3}$, ${\lambda }_{4}$ and ${\lambda }_{\stackrel{-}{n}+4}$, ${\lambda }_{\stackrel{-}{n}+5}$, ${\lambda }_{\stackrel{-}{n}+6}$, ${\lambda }_{\stackrel{-}{n+7}}$ are the exterior knots. The knots ${\lambda }_{4}$ and ${\lambda }_{\stackrel{-}{n}+4}$ are the boundaries of the spline.
Only abscissae satisfying,
 $λ4 ≤ xj ≤ λn-+4 ,$
will be evaluated. At a simple knot ${\lambda }_{i}$ (i.e., one satisfying ${\lambda }_{i-1}<{\lambda }_{i}<{\lambda }_{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-\mathit{j}$, for $\mathit{j}=1,2,\dots ,r$, are, in general, discontinuous. (Here $1\le r\le 4$; $r>4$ is not meaningful.) The maximum order of the derivatives to be evaluated ${D}_{\mathrm{ord}}$, and the left- or right-handedness of the computation when an abscissa corresponds exactly to an interior knot, are determined by the value of deriv.
Each abscissa (point at which the spline is to be evaluated) ${x}_{j}$ contained in x has an associated enclosing interval number, ${\mathit{ixloc}}_{j}$ either supplied or returned in ixloc (see argument start). A simple call to e02bff would set ${\mathbf{start}}=0$ and the contents of ixloc need never be set nor referenced, and the following description on modes of operation can be ignored. However, where efficiency is an important consideration, the following description will help to choose the appropriate mode of operation.
The interval numbers are used to determine which B-splines must be evaluated for a given abscissa, and are defined as
 $ixlocj = ≤0 xj < λ1 4 λ4 = xj k λk < xj < λk+1 k λ4 < λk = xj left derivatives k xj = λk+1 < λ n-+4 right derivatives or no derivatives n-+4 λn-+4 = xj >n-+7 xj > λn-+7$ (1)
The algorithm has two modes of vectorization, termed here sorted and unsorted, which are selectable by the argument start.
Furthermore, if the supplied abscissae are sufficiently ordered, as indicated by the argument xord, the algorithm will take advantage of significantly faster methods for the determination of both the interval numbers and the subsequent spline evaluations.
The sorted mode has two phases, a sorting phase and an evaluation phase. This mode is recommended if there are many abscissae to evaluate relative to the number of intervals of the spline, or the abscissae are distributed relatively densely over a subsection of the spline. In the first phase, ${\mathit{ixloc}}_{j}$ is determined for each ${x}_{j}$ and a permutation is calculated to sort the ${x}_{j}$ by interval number. The first phase may be either partially or completely by-passed using the argument start if the enclosing segments and/or the subsequent ordering are already known a priori, for example if multiple spline coefficients c are to be evaluated over the same set of knots lamda.
In the second phase of the sorted mode, spline approximations are evaluated by segment, so that non-abscissa dependent calculations over a segment may be reused in the evaluation for all abscissae belonging to a specific segment. For example, all third derivatives of all abscissae in the same segment will be identical.
In the unsorted mode of vectorization, no a priori segment sorting is performed, and if the abscissae are not sufficiently ordered, the evaluation at an abscissa will be independent of evaluations at other abscissae; also non-abscissa dependent calculations over a segment will be repeated for each abscissa in a segment. This may be quicker if the number of abscissa is small in comparison to the number of knots in the spline, and they are distributed sparsely throughout the domain of the spline. This is effectively a direct vectorization of e02bbf and e02bcf, although if the enclosing interval numbers ${\mathit{ixloc}}_{j}$ are known, these may again be provided.
If the abscissae are sufficiently ordered, then once the first abscissa in a segment is known, an efficient algorithm will be used to determine the location of the final abscissa in this segment. The spline will subsequently be evaluated in a vectorized manner for all the abscissae indexed between the first and last of the current segment.
If no derivatives are required, the spline evaluation is calculated by taking convex combinations due to de Boor (1972). Otherwise, the calculation of $s\left(x\right)$ and its derivatives is based upon,
 (i) evaluating the nonzero B-splines of orders $1$, $2$, $3$ and $4$ by recurrence (see Cox (1972) and Cox (1978)), (ii) 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)), (iii) multiplying the fourth-order B-spline values and their derivative by the appropriate B-spline coefficients, and summing, to yield the values of $s\left(x\right)$ and its derivatives.
The method of convex combinations is significantly faster than the recurrence based method. If higher derivatives of order $2$ or $3$ are not required, as much computation as possible is avoided.

## 4References

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

## 5Arguments

1:     $\mathbf{start}$ – IntegerInput
On entry: indicates the completion state of the first phase of the algorithm.
${\mathbf{start}}=0$
The enclosing interval numbers ${\mathit{ixloc}}_{j}$ for the abscissae ${x}_{j}$ contained in x have not been determined, and you wish to use the sorted mode of vectorization.
${\mathbf{start}}=1$
The enclosing interval numbers ${\mathit{ixloc}}_{j}$ have been determined and are provided in ixloc, however the required permutation and interval related information has not been determined and you wish to use the sorted mode of vectorization.
${\mathbf{start}}=2$
You wish to use the sorted mode of vectorization, and the entire first phase has been completed, with the enclosing interval numbers supplied in ixloc, and the required permutation and interval related information provided in iwrk (from a previous call to e02bff).
${\mathbf{start}}=10$
The enclosing interval numbers ${\mathit{ixloc}}_{j}$ for the abscissae ${x}_{j}$ contained in x have not been determined, and you wish to use the unsorted mode of vectorization.
${\mathbf{start}}=11$
The enclosing interval numbers ${\mathit{ixloc}}_{j}$ for the abscissae ${x}_{j}$ contained in x have been supplied in ixloc, and you wish to use the unsorted mode of vectorization.
Constraint: ${\mathbf{start}}=0$, $1$, $2$, $10$ or $11$.
Additional: ${\mathbf{start}}=0$ or $10$ should be used unless you are sure that the knot set is unchanged between calls.
2:     $\mathbf{ncap7}$ – IntegerInput
On entry: $\stackrel{-}{n}+7$, where $\stackrel{-}{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 ${\lambda }_{4}$ to ${\lambda }_{\stackrel{-}{n}+4}$ over which the spline is defined). Note that if e02bef was used to generate the knots and spline coefficients then ncap7 should contain the same value as returned in n by e02bef.
Constraint: ${\mathbf{ncap7}}\ge 8$.
3:     $\mathbf{lamda}\left({\mathbf{ncap7}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: ${\mathbf{lamda}}\left(\mathit{j}\right)$ must be set to the value of the $\mathit{j}$th member of the complete set of knots, ${\lambda }_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,\stackrel{-}{n}+7$.
Constraint: the ${\mathbf{lamda}}\left(j\right)$ must be in nondecreasing order with ${\mathbf{lamda}}\left({\mathbf{ncap7}}-3\right)>{\mathbf{lamda}}\left(4\right)$.
4:     $\mathbf{c}\left({\mathbf{ncap7}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the coefficient ${c}_{\mathit{i}}$ of the B-spline ${N}_{\mathit{i}}\left(x\right)$, for $\mathit{i}=1,2,\dots ,\stackrel{-}{n}+3$. The remaining elements of the array are not referenced.
5:     $\mathbf{deriv}$ – IntegerInput
On entry: determines the maximum order of derivatives required, ${D}_{\mathrm{ord}}$.
If ${\mathbf{deriv}}<0$ left derivatives are calculated, otherwise right derivatives are calculated. For abscissae satisfying ${x}_{j}={\lambda }_{4}$ or ${x}_{j}={\lambda }_{\stackrel{-}{n}+4}$ only right-handed or left-handed computation will be used respectively. For abscissae which do not coincide exactly with a knot, the handedness of the computation is immaterial.
${\mathbf{deriv}}=0$
No derivatives required.
${\mathbf{deriv}}=±1$
Only $s\left(x\right)$ and its first derivative are required.
${\mathbf{deriv}}=±2$
Only $s\left(x\right)$ and its first and second derivatives are required.
${\mathbf{deriv}}=±3$
$s\left(x\right)$ and its first, second and third derivatives are required.
Note: if $\left|{\mathbf{deriv}}\right|$ is greater than $3$ only the derivatives up to and including $3$ will be returned..
6:     $\mathbf{xord}$ – IntegerInput
On entry: indicates whether x is supplied in a sufficiently ordered manner. If x is sufficiently ordered e02bff will complete faster.
${\mathbf{xord}}\ne 0$
The abscissae in x are ordered at least by ascending interval, in that any two abscissae contained in the same interval are only separated by abscissae in the same interval, and the intervals are arranged in ascending order. For example, ${x}_{j}<{x}_{\mathit{j}+1}$, for $\mathit{j}=1,2,\dots ,{\mathbf{nx}}-1$.
${\mathbf{xord}}=0$
The abscissae in x are not sufficiently ordered.
7:     $\mathbf{x}\left({\mathbf{nx}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the abscissae ${x}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,{n}_{x}$. If ${\mathbf{start}}=0$ or $10$ then evaluations will only be performed for these ${x}_{j}$ satisfying ${\lambda }_{4}\le {x}_{j}\le {\lambda }_{\stackrel{-}{n}+4}$. Otherwise evaluation will be performed unless the corresponding element of ixloc contains an invalid interval number. Please note that if the ${\mathbf{ixloc}}\left(j\right)$ is a valid interval number then no check is made that ${\mathbf{x}}\left(j\right)$ actually lies in that interval.
Constraint: at least one abscissa must fall between ${\mathbf{lamda}}\left(4\right)$ and ${\mathbf{lamda}}\left({\mathbf{ncap7}}-3\right)$.
8:     $\mathbf{ixloc}\left({\mathbf{nx}}\right)$ – Integer arrayInput/Output
On entry: if ${\mathbf{start}}=1$, $2$ or $11$, if you wish ${x}_{j}$ to be evaluated, ${\mathbf{ixloc}}\left(j\right)$ must be the enclosing interval number ${\mathit{ixloc}}_{j}$ of the abscissae ${x}_{j}$ (see (1)). If you do not wish ${x}_{j}$ to be evaluated, you may set the interval number to be either less than $4$ or greater than $\stackrel{-}{n}+4$.
Otherwise, ixloc need not be set.
On exit: if ${\mathbf{start}}=1$, $2$ or $11$, ixloc is unchanged on exit.
Otherwise, ${\mathbf{ixloc}}\left(\mathit{j}\right)$, contains the enclosing interval number ${\mathit{ixloc}}_{\mathit{j}}$, for the abscissa supplied in ${\mathbf{x}}\left(\mathit{j}\right)$, for $\mathit{j}=1,2,\dots ,{n}_{x}$. Evaluations will only be performed for abscissae ${x}_{j}$ satisfying ${\lambda }_{4}\le {x}_{j}\le {\lambda }_{\stackrel{-}{n}+4}$. If evaluation is not performed ${\mathbf{ixloc}}\left(j\right)$ is set to $0$ if ${x}_{j}<{\lambda }_{4}$ or $\stackrel{-}{n}+7$ if ${x}_{j}>{\lambda }_{\stackrel{-}{n}+4}$.
Constraint: if ${\mathbf{start}}=1$, $2$ or $11$, at least one element of ixloc must be between $4$ and ${\mathbf{ncap7}}-3$.
9:     $\mathbf{nx}$ – IntegerInput
On entry: ${n}_{x}$, the total number of abscissae contained in x, including any that will not be evaluated.
Constraint: ${\mathbf{nx}}\ge 1$.
10:   $\mathbf{s}\left({\mathbf{lds}},*\right)$ – Real (Kind=nag_wp) arrayOutput
Note: the second dimension of the array s must be at least ${D}_{\mathrm{ord}}+1$, see deriv for the definition of ${D}_{\mathrm{ord}}$.
On exit: if ${x}_{j}$ is valid, ${\mathbf{s}}\left(\mathit{j},\mathit{d}\right)$ will contain the ($\mathit{d}-1$)th derivative of $s\left(x\right)$, for $\mathit{d}=1,2,\dots ,{D}_{\mathrm{ord}}+1$ and $\mathit{j}=1,2,\dots ,{n}_{x}$. In particular, ${\mathbf{s}}\left(j,1\right)$ will contain the approximation of $s\left({x}_{j}\right)$ for all legal values in x.
11:   $\mathbf{lds}$ – IntegerInput
On entry: the first dimension of the array s as declared in the (sub)program from which e02bff is called.
Constraint: ${\mathbf{lds}}\ge {\mathbf{nx}}$, regardless of the acceptability of the elements of x.
12:   $\mathbf{iwrk}\left({\mathbf{liwrk}}\right)$ – Integer arrayInput/Output
On entry: if ${\mathbf{start}}=2$, iwrk must be unchanged from a previous call to e02bff with ${\mathbf{start}}=0$ or $1$.
Otherwise, iwrk need not be set.
On exit: if ${\mathbf{start}}=10$ or $11$, iwrk is unchanged on exit.
Otherwise, iwrk contains the required permutation of elements of x, if any, and information related to the division of the abscissae ${x}_{j}$ between the intervals derived from lamda.
13:   $\mathbf{liwrk}$ – IntegerInput
On entry: the dimension of the array iwrk as declared in the (sub)program from which e02bff is called.
Constraint: if ${\mathbf{start}}=0$, $1$ or $2$, ${\mathbf{liwrk}}\ge 3+3×{\mathbf{nx}}$.
14:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, because for this routine the values of the output arguments may be useful even if ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}1$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Note: e02bff may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, at least one element of x has an enclosing interval number in ixloc outside the set allowed by the provided spline. The spline has been evaluated for all x with enclosing interval numbers inside the allowable set.
$〈\mathit{\text{value}}〉$ entries of x were indexed below the lower bound $〈\mathit{\text{value}}〉$.
$〈\mathit{\text{value}}〉$ entries of x were indexed above the upper bound $〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=2$
On entry, all elements of x had enclosing interval numbers in ixloc outside the domain allowed by the provided spline.
$〈\mathit{\text{value}}〉$ entries of x were indexed below the lower bound $〈\mathit{\text{value}}〉$.
$〈\mathit{\text{value}}〉$ entries of x were indexed above the upper bound $〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=11$
On entry, ${\mathbf{start}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{start}}=0$, $1$, $2$, $10$ or $11$.
${\mathbf{ifail}}=12$
On entry, ${\mathbf{start}}=2$ and nx is not consistent with the previous call to e02bff.
On entry, ${\mathbf{nx}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nx}}=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=21$
On entry, ${\mathbf{ncap7}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ncap7}}\ge 8$.
${\mathbf{ifail}}=31$
On entry, ${\mathbf{lamda}}\left(4\right)=〈\mathit{\text{value}}〉$, ${\mathbf{ncap7}}=〈\mathit{\text{value}}〉$ and ${\mathbf{lamda}}\left({\mathbf{ncap7}}-3\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{lamda}}\left(4\right)<{\mathbf{lamda}}\left({\mathbf{ncap7}}-3\right)$.
${\mathbf{ifail}}=91$
On entry, ${\mathbf{nx}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nx}}\ge 1$.
${\mathbf{ifail}}=111$
On entry, ${\mathbf{lds}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{lds}}\ge {\mathbf{nx}}=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=131$
On entry, ${\mathbf{liwrk}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{liwrk}}\ge 3×{\mathbf{nx}}+3=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

The computed value of $s\left(x\right)$ has negligible error in most practical situations. Specifically, this value has an absolute error bounded in modulus by , where $\mathit{cmax}$ 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 ${\lambda }_{j}+3. 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\left(x\right)$ has relative error bounded by . For full details see Cox (1978).
No complete error analysis is available for the computation of the derivatives of $s\left(x\right)$. However, for most practical purposes the absolute errors in the computed derivatives should be small. Note that this is in comparison to the derivatives of the spline, which may or may not be comparable to the derivatives of the function that has been approximated by the spline.

## 8Parallelism and Performance

e02bff is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

If using the sorted mode of vectorization, the time required for the first phase to determine the enclosing intervals is approximately proportional to $\mathit{O}\left({n}_{x}\mathrm{log}\left(\stackrel{-}{n}\right)\right)$. The time required to then generate the required permutations and interval information is $\mathit{O}\left({n}_{x}\right)$ if x is ordered sufficiently, or at worst $\mathit{O}\left({n}_{x}\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({n}_{x},\stackrel{-}{n}\right)\mathrm{log}\left(\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({n}_{x},\stackrel{-}{n}\right)\right)\right)$ if x is not ordered. The time required by the second phase is then proportional to $\mathit{O}\left({n}_{x}\right)$.
If using the unsorted mode of vectorization, the time required is proportional to $\mathit{O}\left({n}_{x}\mathrm{log}\left(\stackrel{-}{n}\right)\right)$ if the enclosing interval numbers are not provided, or $\mathit{O}\left({n}_{x}\right)$ if they are provided. However, the repeated calculation of various quantities will typically make this slower than the sorted mode when the ratio of abscissae to knots is high, or the abscissae are densely distributed over a relatively small subset of the intervals of the spline.
Note: the routine does not test all the conditions on the knots given in the description of lamda in Section 5, since to do this would result in a computation time with a linear dependency upon $\stackrel{-}{n}$ instead of $\mathrm{log}\left(\stackrel{-}{n}\right)$. All the conditions are tested in e02baf and e02bef, however.

## 10Example

This example fits a spline through a set of data points using e02bef and then evaluates the spline at a set of supplied abscissae.

### 10.1Program Text

Program Text (e02bffe.f90)

### 10.2Program Data

Program Data (e02bffe.d)

### 10.3Program Results

Program Results (e02bffe.r)

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