This chapter describes functions for the computation of smoothing basis splines (B-splines). A smoothing spline differs from an interpolating spline in that the resulting curve is not required to pass through each datapoint.  See Interpolation, for information about interpolating splines.

The header file gsl_bspline.h contains the prototypes for the bspline functions and related declarations.

Overview
========

B-splines are commonly used as basis functions to fit smoothing curves to large data sets. To do this, the abscissa axis is broken up into some number of intervals, where the endpoints of each interval are called breakpoints. These breakpoints are then converted to knots by imposing various continuity and smoothness conditions at each interface. Given a nondecreasing knot vector $t = \t_0, t_1, \dots, t_n+k-1\$ t = {t_0, t_1, ..., t_{n+k-1}}, the n basis splines of order k are defined by

     B_(i,1)(x) = (1, t_i <= x < t_(i+1)
                  (0, else
     B_(i,k)(x) = [(x - t_i)/(t_(i+k-1) - t_i)] B_(i,k-1)(x)
                   + [(t_(i+k) - x)/(t_(i+k) - t_(i+1))] B_(i+1,k-1)(x)

for i = 0, ..., n-1. The common case of cubic B-splines is given by k = 4. The above recurrence relation can be evaluated in a numerically stable way by the de Boor algorithm.

If we define appropriate knots on an interval [a,b] then the B-spline basis functions form a complete set on that interval. Therefore we can expand a smoothing function as

     f(x) = \sum_i c_i B_(i,k)(x)

given enough (x_j, f(x_j)) data pairs. The coefficients c_i can be readily obtained from a least-squares fit.

Initializing the B-splines solver
=================================

The computation of B-spline functions requires a preallocated workspace of type gsl_bspline_workspace.

 -- Function: gsl_bspline_workspace * gsl_bspline_alloc (const size_t K, const size_t NBREAK)
     This function allocates a workspace for computing B-splines of order K. The number of breakpoints is given by NBREAK. This leads to n = nbreak + k - 2 basis functions. Cubic B-splines are specified by k = 4. The size of the workspace is O(2k^2 + 5k + nbreak).

 -- Function: void gsl_bspline_free (gsl_bspline_workspace * W)
     This function frees the memory associated with the workspace W.

Constructing the knots vector
=============================

 -- Function: int gsl_bspline_knots (const gsl_vector * BREAKPTS, gsl_bspline_workspace * W)
     This function computes the knots associated with the given breakpoints and stores them internally in w->knots.

 -- Function: int gsl_bspline_knots_uniform (const double A, const double B, gsl_bspline_workspace * W)
     This function assumes uniformly spaced breakpoints on [a,b] and constructs the corresponding knot vector using the previously specified NBREAK parameter. The knots are stored in w->knots.

Evaluation of B-splines
=======================

 -- Function: int gsl_bspline_eval (const double X, gsl_vector * B, gsl_bspline_workspace * W)
     This function evaluates all B-spline basis functions at the position X and stores them in the vector B, so that the i-th element is B_i(x). The vector B must be of length n = nbreak + k - 2.  This value may also be obtained by calling gsl_bspline_ncoeffs. Computing all the basis functions at once is more efficient than computing them individually, due to the nature of the defining recurrence relation.

 -- Function: int gsl_bspline_eval_nonzero (const double X, gsl_vector * BK, size_t * ISTART, size_t * IEND, gsl_bspline_workspace * W)
     This function evaluates all potentially nonzero B-spline basis functions at the position X and stores them in the vector BK, so that the i-th element is $B_(istart+i)(x)$ B_(istart+i)(x). The last element of BK is $B_iend(x)$ B_(iend)(x).  The vector BK must be of length k.  By returning only the nonzero basis functions, this function allows quantities involving linear combinations of the B_i(x) to be computed without unnecessary terms (such linear combinations occur, for example, when evaluating an interpolated function).

 -- Function: size_t gsl_bspline_ncoeffs (gsl_bspline_workspace * W)
     This function returns the number of B-spline coefficients given by n = nbreak + k - 2.

Evaluation of B-spline derivatives
==================================

 -- Function: int gsl_bspline_deriv_eval (const double X, const size_t NDERIV, gsl_matrix * DB, gsl_bspline_workspace * W)
     This function evaluates all B-spline basis function derivatives of orders 0 through nderiv (inclusive) at the position X and stores them in the matrix DB.  The (i,j)-th element of DB is d^jB_i(x)/dx^j.  The matrix DB must be of size n = nbreak + k - 2 by nderiv + 1. The value n may also be obtained by calling gsl_bspline_ncoeffs.  Note that function evaluations are included as the zeroth order derivatives in DB. Computing all the basis function derivatives at once is more efficient than computing them individually, due to the nature of the defining recurrence relation.

 -- Function: int gsl_bspline_deriv_eval_nonzero (const double X, const size_t NDERIV, gsl_matrix * DB, size_t * ISTART, size_t * IEND, gsl_bspline_workspace * W)
     This function evaluates all potentially nonzero B-spline basis function derivatives of orders 0 through nderiv (inclusive) at the position X and stores them in the matrix DB.  The (i,j)-th element of DB is $d^jB_(istart+i)(x)/dx^j$ d^j/dx^j B_(istart+i)(x).  The last row of DB contains $d^jB_iend(x)/dx^j$ d^j/dx^j B_(iend)(x).  The matrix DB must be of size k by at least nderiv + 1.  Note that function evaluations are included as the zeroth order derivatives in DB. By returning only the nonzero basis functions, this function allows quantities involving linear combinations of the B_i(x) and their derivatives to be computed without unnecessary terms.

Working with the Greville abscissae
===================================

The Greville abscissae are defined to be the mean location of k-1 consecutive knots in the knot vector for each basis spline function of order k.  With the first and last knots in the gsl_bspline_workspace knot vector excluded, there are gsl_bspline_ncoeffs Greville abscissae for any given B-spline basis.  These values are often used in B-spline collocation applications and may also be called Marsden-Schoenberg points.

 -- Function: double gsl_bspline_greville_abscissa (size_t I, gsl_bspline_workspace *W);
     Returns the location of the i-th Greville abscissa for the given B-spline basis.  For the ill-defined case when k=1, the implementation chooses to return breakpoint interval midpoints.

Examples
========

The following program computes a linear least squares fit to data using cubic B-spline basis functions with uniform breakpoints. The data is generated from the curve y(x) = \cos(x) \exp(-x/10) on the interval [0, 15] with Gaussian noise added.

          [file bspline.c not included]

The output can be plotted with GNU graph.

     $ ./a.out > bspline.txt
     chisq/dof = 1.118217e+00, Rsq = 0.989771
     $ graph -T ps -X x -Y y -x 0 15 -y -1 1.3 < bspline.txt > bspline.ps

B-Spline References and Further Reading
=======================================

Further information on the algorithms described in this section can be found in the following book,

  C. de Boor, A Practical Guide to Splines (1978), Springer-Verlag, ISBN 0-387-90356-9.

Further information of Greville abscissae and B-spline collocation can be found in the following paper,

  Richard W. Johnson, Higher order B-spline collocation at the Greville abscissae.  Applied Numerical Mathematics. vol. 52, 2005, 63--75.

A large collection of B-spline routines is available in the PPPACK library available at http://www.netlib.org/pppack, which is also part of SLATEC.