1 @cindex basis splines, B-splines
4 This chapter describes functions for the computation of smoothing
5 basis splines (B-splines). The header file @file{gsl_bspline.h}
6 contains prototypes for the bspline functions and related declarations.
9 * Overview of B-splines::
10 * Initializing the B-splines solver::
11 * Constructing the knots vector::
12 * Evaluation of B-spline basis functions::
13 * Example programs for B-splines::
14 * References and Further Reading::
17 @node Overview of B-splines
19 @cindex basis splines, overview
21 B-splines are commonly used as basis functions to fit smoothing
22 curves to large data sets. To do this, the abscissa axis is
23 broken up into some number of intervals, where the endpoints
24 of each interval are called @dfn{breakpoints}. These breakpoints
25 are then converted to @dfn{knots} by imposing various continuity
26 and smoothness conditions at each interface. Given a nondecreasing
28 @c{$t = \{t_0, t_1, \dots, t_{n+k-1}\}$}
29 @math{t = @{t_0, t_1, @dots{}, t_@{n+k-1@}@}},
30 the @math{n} basis splines of order @math{k} are defined by
34 B_{i,1}(x) = \left\{\matrix{1, & t_i \le x < t_{i+1}\cr
38 B_{i,k}(x) = \left[(x - t_i)/(t_{i+k-1} - t_i)\right] B_{i,k-1}(x) +
39 \left[(t_{i+k} - x)/(t_{i+k} - t_{i+1})\right] B_{i+1,k-1}(x)
46 B_(i,1)(x) = (1, t_i <= x < t_(i+1)
48 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)
52 for @math{i = 0, @dots{}, n-1}. The common case of cubic B-splines
53 is given by @math{k = 4}. The above recurrence relation can be
54 evaluated in a numerically stable way by the de Boor algorithm.
56 If we define appropriate knots on an interval @math{[a,b]} then
57 the B-spline basis functions form a complete set on that interval.
58 Therefore we can expand a smoothing function as
62 f(x) = \sum_{i=0}^{n-1} c_i B_{i,k}(x)
69 f(x) = \sum_i c_i B_(i,k)(x)
73 given enough @math{(x_j, f(x_j))} data pairs. The @math{c_i} can
74 be readily obtained from a least-squares fit.
76 @node Initializing the B-splines solver
77 @section Initializing the B-splines solver
78 @cindex basis splines, initializing
80 @deftypefun {gsl_bspline_workspace *} gsl_bspline_alloc (const size_t @var{k}, const size_t @var{nbreak})
81 This function allocates a workspace for computing B-splines of order
82 @var{k}. The number of breakpoints is given by @var{nbreak}. This
83 leads to @math{n = nbreak + k - 2} basis functions. Cubic B-splines
84 are specified by @math{k = 4}. The size of the workspace is
85 @math{O(5k + nbreak)}.
88 @deftypefun void gsl_bspline_free (gsl_bspline_workspace * @var{w})
89 This function frees the memory associated with the workspace @var{w}.
92 @node Constructing the knots vector
93 @section Constructing the knots vector
96 @deftypefun int gsl_bspline_knots (const gsl_vector * @var{breakpts}, gsl_bspline_workspace * @var{w})
97 This function computes the knots associated with the given breakpoints
98 and stores them internally in @code{w->knots}.
101 @deftypefun int gsl_bspline_knots_uniform (const double a, const double b, gsl_bspline_workspace * @var{w})
102 This function assumes uniformly spaced breakpoints on @math{[a,b]}
103 and constructs the corresponding knot vector using the previously
104 specified @var{nbreak} parameter. The knots are stored in
108 @node Evaluation of B-spline basis functions
109 @section Evaluation of B-splines
110 @cindex basis splines, evaluation
112 @deftypefun int gsl_bspline_eval (const double @var{x}, gsl_vector * @var{B}, gsl_bspline_workspace * @var{w})
113 This function evaluates all B-spline basis functions at the position
114 @var{x} and stores them in @var{B}, so that the @math{i}th element
115 of @var{B} is @math{B_i(x)}. @var{B} must be of length
116 @math{n = nbreak + k - 2}. This value may also be obtained by calling
117 @code{gsl_bspline_ncoeffs}.
118 It is far more efficient to compute all of the basis functions at
119 once than to compute them individually, due to the nature of the
120 defining recurrence relation.
123 @deftypefun size_t gsl_bspline_ncoeffs (gsl_bspline_workspace * @var{w})
124 This function returns the number of B-spline coefficients given by
125 @math{n = nbreak + k - 2}.
128 @node Example programs for B-splines
129 @section Example programs for B-splines
130 @cindex basis splines, examples
132 The following program computes a linear least squares fit to data using
133 cubic B-spline basis functions with uniform breakpoints. The data is
134 generated from the curve @math{y(x) = \cos{(x)} \exp{(-x/10)}} on
135 @math{[0, 15]} with gaussian noise added.
138 @verbatiminclude examples/bspline.c
141 The output can be plotted with @sc{gnu} @code{graph}.
144 $ ./a.out > bspline.dat
145 chisq/dof = 1.118217e+00, Rsq = 0.989771
146 $ graph -T ps -X x -Y y -x 0 15 -y -1 1.3 < bspline.dat > bspline.ps
151 @center @image{bspline,3.4in}
154 @node References and Further Reading
155 @section References and Further Reading
157 Further information on the algorithms described in this section can be
158 found in the following book,
161 C. de Boor, @cite{A Practical Guide to Splines} (1978), Springer-Verlag,
166 A large collection of B-spline routines is available in the
167 @sc{pppack} library available at @uref{http://www.netlib.org/pppack}.