| 1 |
/* bspline/bspline.c |
| 2 |
* |
| 3 |
* Copyright (C) 2006 Patrick Alken |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "gsl__config.h" |
| 21 |
#include "gsl_errno.h" |
| 22 |
#include "gsl_bspline.h" |
| 23 |
|
| 24 |
/* |
| 25 |
* This module contains routines related to calculating B-splines. |
| 26 |
* The algorithms used are described in |
| 27 |
* |
| 28 |
* [1] Carl de Boor, "A Practical Guide to Splines", Springer |
| 29 |
* Verlag, 1978. |
| 30 |
*/ |
| 31 |
|
| 32 |
static int bspline_eval_all(const double x, gsl_vector *B, size_t *idx, |
| 33 |
gsl_bspline_workspace *w); |
| 34 |
|
| 35 |
static inline size_t bspline_find_interval(const double x, int *flag, |
| 36 |
gsl_bspline_workspace *w); |
| 37 |
|
| 38 |
/* |
| 39 |
gsl_bspline_alloc() |
| 40 |
Allocate space for a bspline workspace. The size of the |
| 41 |
workspace is O(5k + nbreak) |
| 42 |
|
| 43 |
Inputs: k - spline order (cubic = 4) |
| 44 |
nbreak - number of breakpoints |
| 45 |
|
| 46 |
Return: pointer to workspace |
| 47 |
*/ |
| 48 |
|
| 49 |
gsl_bspline_workspace * |
| 50 |
gsl_bspline_alloc(const size_t k, const size_t nbreak) |
| 51 |
{ |
| 52 |
if (k == 0) |
| 53 |
{ |
| 54 |
GSL_ERROR_NULL("k must be at least 1", GSL_EINVAL); |
| 55 |
} |
| 56 |
else if (nbreak < 2) |
| 57 |
{ |
| 58 |
GSL_ERROR_NULL("nbreak must be at least 2", GSL_EINVAL); |
| 59 |
} |
| 60 |
else |
| 61 |
{ |
| 62 |
gsl_bspline_workspace *w; |
| 63 |
|
| 64 |
w = (gsl_bspline_workspace *) |
| 65 |
calloc(1, sizeof(gsl_bspline_workspace)); |
| 66 |
if (w == 0) |
| 67 |
{ |
| 68 |
GSL_ERROR_NULL("failed to allocate space for workspace", GSL_ENOMEM); |
| 69 |
} |
| 70 |
|
| 71 |
w->k = k; |
| 72 |
w->km1 = k - 1; |
| 73 |
w->nbreak = nbreak; |
| 74 |
w->l = nbreak - 1; |
| 75 |
w->n = w->l + k - 1; |
| 76 |
|
| 77 |
w->knots = gsl_vector_alloc(w->n + k); |
| 78 |
if (w->knots == 0) |
| 79 |
{ |
| 80 |
gsl_bspline_free(w); |
| 81 |
GSL_ERROR_NULL("failed to allocate space for knots vector", GSL_ENOMEM); |
| 82 |
} |
| 83 |
|
| 84 |
w->deltal = gsl_vector_alloc(k); |
| 85 |
w->deltar = gsl_vector_alloc(k); |
| 86 |
if (!w->deltal || !w->deltar) |
| 87 |
{ |
| 88 |
gsl_bspline_free(w); |
| 89 |
GSL_ERROR_NULL("failed to allocate space for delta vectors", GSL_ENOMEM); |
| 90 |
} |
| 91 |
|
| 92 |
w->B = gsl_vector_alloc(k); |
| 93 |
if (w->B == 0) |
| 94 |
{ |
| 95 |
gsl_bspline_free(w); |
| 96 |
GSL_ERROR_NULL("failed to allocate space for temporary spline vector", GSL_ENOMEM); |
| 97 |
} |
| 98 |
|
| 99 |
return (w); |
| 100 |
} |
| 101 |
} /* gsl_bspline_alloc() */ |
| 102 |
|
| 103 |
/* Return number of coefficients */ |
| 104 |
size_t |
| 105 |
gsl_bspline_ncoeffs (gsl_bspline_workspace * w) |
| 106 |
{ |
| 107 |
return w->n; |
| 108 |
} |
| 109 |
|
| 110 |
/* Return order */ |
| 111 |
size_t |
| 112 |
gsl_bspline_order (gsl_bspline_workspace * w) |
| 113 |
{ |
| 114 |
return w->k; |
| 115 |
} |
| 116 |
|
| 117 |
/* Return number of breakpoints */ |
| 118 |
size_t |
| 119 |
gsl_bspline_nbreak (gsl_bspline_workspace * w) |
| 120 |
{ |
| 121 |
return w->nbreak; |
| 122 |
} |
| 123 |
|
| 124 |
double |
| 125 |
gsl_bspline_breakpoint (size_t i, gsl_bspline_workspace * w) |
| 126 |
{ |
| 127 |
size_t j = i + w->k - 1; |
| 128 |
return gsl_vector_get(w->knots, j); |
| 129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
gsl_bspline_free() |
| 133 |
Free a bspline workspace |
| 134 |
|
| 135 |
Inputs: w - workspace to free |
| 136 |
|
| 137 |
Return: none |
| 138 |
*/ |
| 139 |
|
| 140 |
void |
| 141 |
gsl_bspline_free(gsl_bspline_workspace *w) |
| 142 |
{ |
| 143 |
if (!w) |
| 144 |
return; |
| 145 |
|
| 146 |
if (w->knots) |
| 147 |
gsl_vector_free(w->knots); |
| 148 |
|
| 149 |
if (w->deltal) |
| 150 |
gsl_vector_free(w->deltal); |
| 151 |
|
| 152 |
if (w->deltar) |
| 153 |
gsl_vector_free(w->deltar); |
| 154 |
|
| 155 |
if (w->B) |
| 156 |
gsl_vector_free(w->B); |
| 157 |
|
| 158 |
free(w); |
| 159 |
} /* gsl_bspline_free() */ |
| 160 |
|
| 161 |
/* |
| 162 |
gsl_bspline_knots() |
| 163 |
Compute the knots from the given breakpoints: |
| 164 |
|
| 165 |
knots(1:k) = breakpts(1) |
| 166 |
knots(k+1:k+l-1) = breakpts(i), i = 2 .. l |
| 167 |
knots(n+1:n+k) = breakpts(l + 1) |
| 168 |
|
| 169 |
where l is the number of polynomial pieces (l = nbreak - 1) and |
| 170 |
n = k + l - 1 |
| 171 |
(using matlab syntax for the arrays) |
| 172 |
|
| 173 |
The repeated knots at the beginning and end of the interval |
| 174 |
correspond to the continuity condition there. See pg. 119 |
| 175 |
of [1]. |
| 176 |
|
| 177 |
Inputs: breakpts - breakpoints |
| 178 |
w - bspline workspace |
| 179 |
|
| 180 |
Return: success or error |
| 181 |
*/ |
| 182 |
|
| 183 |
int |
| 184 |
gsl_bspline_knots(const gsl_vector *breakpts, gsl_bspline_workspace *w) |
| 185 |
{ |
| 186 |
if (breakpts->size != w->nbreak) |
| 187 |
{ |
| 188 |
GSL_ERROR("breakpts vector has wrong size", GSL_EBADLEN); |
| 189 |
} |
| 190 |
else |
| 191 |
{ |
| 192 |
size_t i; /* looping */ |
| 193 |
|
| 194 |
for (i = 0; i < w->k; ++i) |
| 195 |
gsl_vector_set(w->knots, i, gsl_vector_get(breakpts, 0)); |
| 196 |
|
| 197 |
for (i = 1; i < w->l; ++i) |
| 198 |
{ |
| 199 |
gsl_vector_set(w->knots, w->k - 1 + i, |
| 200 |
gsl_vector_get(breakpts, i)); |
| 201 |
} |
| 202 |
|
| 203 |
for (i = w->n; i < w->n + w->k; ++i) |
| 204 |
gsl_vector_set(w->knots, i, gsl_vector_get(breakpts, w->l)); |
| 205 |
|
| 206 |
return GSL_SUCCESS; |
| 207 |
} |
| 208 |
} /* gsl_bspline_knots() */ |
| 209 |
|
| 210 |
/* |
| 211 |
gsl_bspline_knots_uniform() |
| 212 |
Construct uniformly spaced knots on the interval [a,b] using |
| 213 |
the previously specified number of breakpoints. 'a' is the position |
| 214 |
of the first breakpoint and 'b' is the position of the last |
| 215 |
breakpoint. |
| 216 |
|
| 217 |
Inputs: a - left side of interval |
| 218 |
b - right side of interval |
| 219 |
w - bspline workspace |
| 220 |
|
| 221 |
Return: success or error |
| 222 |
|
| 223 |
Notes: 1) w->knots is modified to contain the uniformly spaced |
| 224 |
knots |
| 225 |
|
| 226 |
2) The knots vector is set up as follows (using octave syntax): |
| 227 |
|
| 228 |
knots(1:k) = a |
| 229 |
knots(k+1:k+l-1) = a + i*delta, i = 1 .. l - 1 |
| 230 |
knots(n+1:n+k) = b |
| 231 |
*/ |
| 232 |
|
| 233 |
int |
| 234 |
gsl_bspline_knots_uniform(const double a, const double b, |
| 235 |
gsl_bspline_workspace *w) |
| 236 |
{ |
| 237 |
size_t i; /* looping */ |
| 238 |
double delta; /* interval spacing */ |
| 239 |
double x; |
| 240 |
|
| 241 |
delta = (b - a) / (double) w->l; |
| 242 |
|
| 243 |
for (i = 0; i < w->k; ++i) |
| 244 |
gsl_vector_set(w->knots, i, a); |
| 245 |
|
| 246 |
x = a + delta; |
| 247 |
for (i = 0; i < w->l - 1; ++i) |
| 248 |
{ |
| 249 |
gsl_vector_set(w->knots, w->k + i, x); |
| 250 |
x += delta; |
| 251 |
} |
| 252 |
|
| 253 |
for (i = w->n; i < w->n + w->k; ++i) |
| 254 |
gsl_vector_set(w->knots, i, b); |
| 255 |
|
| 256 |
return GSL_SUCCESS; |
| 257 |
} /* gsl_bspline_knots_uniform() */ |
| 258 |
|
| 259 |
/* |
| 260 |
gsl_bspline_eval() |
| 261 |
Evaluate the basis functions B_i(x) for all i. This is |
| 262 |
basically a wrapper function for bspline_eval_all() |
| 263 |
which formats the output in a nice way. |
| 264 |
|
| 265 |
Inputs: x - point for evaluation |
| 266 |
B - (output) where to store B_i(x) values |
| 267 |
the length of this vector is |
| 268 |
n = nbreak + k - 2 = l + k - 1 = w->n |
| 269 |
w - bspline workspace |
| 270 |
|
| 271 |
Return: success or error |
| 272 |
|
| 273 |
Notes: The w->knots vector must be initialized prior to calling |
| 274 |
this function (see gsl_bspline_knots()) |
| 275 |
*/ |
| 276 |
|
| 277 |
int |
| 278 |
gsl_bspline_eval(const double x, gsl_vector *B, |
| 279 |
gsl_bspline_workspace *w) |
| 280 |
{ |
| 281 |
if (B->size != w->n) |
| 282 |
{ |
| 283 |
GSL_ERROR("B vector length does not match n", GSL_EBADLEN); |
| 284 |
} |
| 285 |
else |
| 286 |
{ |
| 287 |
size_t i; /* looping */ |
| 288 |
size_t idx = 0; |
| 289 |
size_t start; /* first non-zero spline */ |
| 290 |
|
| 291 |
/* find all non-zero B_i(x) values */ |
| 292 |
bspline_eval_all(x, w->B, &idx, w); |
| 293 |
|
| 294 |
/* store values in appropriate part of given vector */ |
| 295 |
|
| 296 |
start = idx - w->k + 1; |
| 297 |
for (i = 0; i < start; ++i) |
| 298 |
gsl_vector_set(B, i, 0.0); |
| 299 |
|
| 300 |
for (i = start; i <= idx; ++i) |
| 301 |
gsl_vector_set(B, i, gsl_vector_get(w->B, i - start)); |
| 302 |
|
| 303 |
for (i = idx + 1; i < w->n; ++i) |
| 304 |
gsl_vector_set(B, i, 0.0); |
| 305 |
|
| 306 |
return GSL_SUCCESS; |
| 307 |
} |
| 308 |
} /* gsl_bspline_eval() */ |
| 309 |
|
| 310 |
/**************************************** |
| 311 |
* INTERNAL ROUTINES * |
| 312 |
****************************************/ |
| 313 |
|
| 314 |
/* |
| 315 |
bspline_eval_all() |
| 316 |
Evaluate all non-zero B-splines B_i(x) using algorithm (8) |
| 317 |
of chapter X of [1] |
| 318 |
|
| 319 |
The idea is something like this. Given x \in [t_i, t_{i+1}] |
| 320 |
with t_i < t_{i+1} and the t_i are knots, the values of the |
| 321 |
B-splines not automatically zero fit into a triangular |
| 322 |
array as follows: |
| 323 |
0 |
| 324 |
0 |
| 325 |
0 B_{i-2,3} |
| 326 |
B_{i-1,2} |
| 327 |
B_{i,1} B_{i-1,3} ... |
| 328 |
B_{i,2} |
| 329 |
0 B_{i,3} |
| 330 |
0 |
| 331 |
0 |
| 332 |
|
| 333 |
where B_{i,k} is the ith B-spline of order k. The boundary 0s |
| 334 |
indicate that those B-splines not in the table vanish at x. |
| 335 |
|
| 336 |
To compute the non-zero B-splines of a given order k, we use |
| 337 |
Eqs. (4) and (5) of chapter X of [1]: |
| 338 |
|
| 339 |
(4) B_{i,1}(x) = { 1, t_i <= x < t_{i+1} |
| 340 |
0, else } |
| 341 |
|
| 342 |
(5) B_{i,k}(x) = (x - t_i) |
| 343 |
----------------- B_{i,k-1}(x) |
| 344 |
(t_{i+k-1} - t_i) |
| 345 |
|
| 346 |
t_{i+k} - x |
| 347 |
+ ----------------- B_{i+1,k-1}(x) |
| 348 |
t_{i+k} - t_{i+1} |
| 349 |
|
| 350 |
So (4) gives us the first column of the table and we can use |
| 351 |
the recurrence relation (5) to get the rest of the columns. |
| 352 |
|
| 353 |
Inputs: x - point at which to evaluate splines |
| 354 |
B - (output) where to store B-spline values (length k) |
| 355 |
idx - (output) B-spline function index of last output |
| 356 |
value (B_{idx}(x) is stored in the last slot of 'B') |
| 357 |
w - bspline workspace |
| 358 |
|
| 359 |
Return: success or error |
| 360 |
|
| 361 |
Notes: 1) the w->knots vector must be initialized before calling |
| 362 |
this function |
| 363 |
|
| 364 |
2) On output, B contains: |
| 365 |
|
| 366 |
B = [B_{i-k+1,k}, B_{i-k+2,k}, ..., B_{i-1,k}, B_{i,k}] |
| 367 |
|
| 368 |
where 'i' is stored in 'idx' on output |
| 369 |
|
| 370 |
3) based on PPPACK bsplvb |
| 371 |
*/ |
| 372 |
|
| 373 |
static int |
| 374 |
bspline_eval_all(const double x, gsl_vector *B, size_t *idx, |
| 375 |
gsl_bspline_workspace *w) |
| 376 |
{ |
| 377 |
if (B->size != w->k) |
| 378 |
{ |
| 379 |
GSL_ERROR("B vector not of length k", GSL_EBADLEN); |
| 380 |
} |
| 381 |
else |
| 382 |
{ |
| 383 |
size_t i; /* spline index */ |
| 384 |
size_t j; /* looping */ |
| 385 |
size_t ii; /* looping */ |
| 386 |
int flag = 0; /* error flag */ |
| 387 |
double saved; |
| 388 |
double term; |
| 389 |
|
| 390 |
i = bspline_find_interval(x, &flag, w); |
| 391 |
|
| 392 |
if (flag == -1) |
| 393 |
{ |
| 394 |
GSL_ERROR("x outside of knot interval", GSL_EINVAL); |
| 395 |
} |
| 396 |
else if (flag == 1) |
| 397 |
{ |
| 398 |
if (x <= gsl_vector_get(w->knots, i) + GSL_DBL_EPSILON) |
| 399 |
{ |
| 400 |
--i; |
| 401 |
} |
| 402 |
else |
| 403 |
{ |
| 404 |
GSL_ERROR("x outside of knot interval", GSL_EINVAL); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
*idx = i; |
| 409 |
|
| 410 |
gsl_vector_set(B, 0, 1.0); |
| 411 |
|
| 412 |
for (j = 0; j < w->k - 1; ++j) |
| 413 |
{ |
| 414 |
gsl_vector_set(w->deltar, j, |
| 415 |
gsl_vector_get(w->knots, i + j + 1) - x); |
| 416 |
gsl_vector_set(w->deltal, j, |
| 417 |
x - gsl_vector_get(w->knots, i - j)); |
| 418 |
|
| 419 |
saved = 0.0; |
| 420 |
|
| 421 |
for (ii = 0; ii <= j; ++ii) |
| 422 |
{ |
| 423 |
term = gsl_vector_get(B, ii) / |
| 424 |
(gsl_vector_get(w->deltar, ii) + |
| 425 |
gsl_vector_get(w->deltal, j - ii)); |
| 426 |
|
| 427 |
gsl_vector_set(B, ii, |
| 428 |
saved + |
| 429 |
gsl_vector_get(w->deltar, ii) * term); |
| 430 |
|
| 431 |
saved = gsl_vector_get(w->deltal, j - ii) * term; |
| 432 |
} |
| 433 |
|
| 434 |
gsl_vector_set(B, j + 1, saved); |
| 435 |
} |
| 436 |
|
| 437 |
return GSL_SUCCESS; |
| 438 |
} |
| 439 |
} /* bspline_eval_all() */ |
| 440 |
|
| 441 |
/* |
| 442 |
bspline_find_interval() |
| 443 |
Find knot interval such that |
| 444 |
|
| 445 |
t_i <= x < t_{i + 1} |
| 446 |
|
| 447 |
where the t_i are knot values. |
| 448 |
|
| 449 |
Inputs: x - x value |
| 450 |
flag - (output) error flag |
| 451 |
w - bspline workspace |
| 452 |
|
| 453 |
Return: i (index in w->knots corresponding to left limit of interval) |
| 454 |
|
| 455 |
Notes: The error conditions are reported as follows: |
| 456 |
|
| 457 |
Condition Return value Flag |
| 458 |
--------- ------------ ---- |
| 459 |
x < t_0 0 -1 |
| 460 |
t_i <= x < t_{i+1} i 0 |
| 461 |
t_{n+k-1} <= x l+k-1 +1 |
| 462 |
*/ |
| 463 |
|
| 464 |
static inline size_t |
| 465 |
bspline_find_interval(const double x, int *flag, |
| 466 |
gsl_bspline_workspace *w) |
| 467 |
{ |
| 468 |
size_t i; |
| 469 |
|
| 470 |
if (x < gsl_vector_get(w->knots, 0)) |
| 471 |
{ |
| 472 |
*flag = -1; |
| 473 |
return 0; |
| 474 |
} |
| 475 |
|
| 476 |
/* find i such that t_i <= x < t_{i+1} */ |
| 477 |
for (i = w->k - 1; i < w->k + w->l - 1; ++i) |
| 478 |
{ |
| 479 |
const double ti = gsl_vector_get(w->knots, i); |
| 480 |
const double tip1 = gsl_vector_get(w->knots, i + 1); |
| 481 |
|
| 482 |
if (tip1 < ti) |
| 483 |
{ |
| 484 |
GSL_ERROR("knots vector is not increasing", GSL_EINVAL); |
| 485 |
} |
| 486 |
|
| 487 |
if (ti <= x && x < tip1) |
| 488 |
break; |
| 489 |
} |
| 490 |
|
| 491 |
if (i == w->k + w->l - 1) |
| 492 |
*flag = 1; |
| 493 |
else |
| 494 |
*flag = 0; |
| 495 |
|
| 496 |
return i; |
| 497 |
} /* bspline_find_interval() */ |