| 1 |
#include <stdlib.h> |
| 2 |
#include <string.h> |
| 3 |
#include <math.h> |
| 4 |
#include <bs.h> |
| 5 |
|
| 6 |
#if defined(_MSC_VER) |
| 7 |
#define INLINE _inline // __inline in newer versions |
| 8 |
#define RESTRICT __restrict |
| 9 |
#else |
| 10 |
#define INLINE inline |
| 11 |
#define RESTRICT restrict |
| 12 |
#endif |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include <stdio.h> |
| 18 |
|
| 19 |
void print_a_and_b(double first[5], double last[5], |
| 20 |
double *A, double *b, int M) |
| 21 |
{ |
| 22 |
int i; |
| 23 |
|
| 24 |
printf("\nfirst: [ %f %f %f %f %f ]\n", |
| 25 |
first[0], first[1], first[2], first[3], first[4]); |
| 26 |
|
| 27 |
for (i=0; i<M; i++) |
| 28 |
printf("row %d : | %f %f %f | | %f |\n", |
| 29 |
i, A[3*i+0], A[3*i+1], A[3*i+2], b[i]); |
| 30 |
|
| 31 |
printf("last: [ %f %f %f %f %f ]\n", |
| 32 |
last[0], last[1], last[2], last[3], last[4]); |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
static int find_index_from(double *values, int n, double x, int start) |
| 48 |
{ |
| 49 |
int i; |
| 50 |
|
| 51 |
if (start <= -1) { |
| 52 |
|
| 53 |
i = 0; |
| 54 |
while (i < n && x >= values[i]) i++; |
| 55 |
return i-1; |
| 56 |
} |
| 57 |
else if (start >= n-1) { |
| 58 |
|
| 59 |
i = n - 1; |
| 60 |
while (i > -1 && x < values[i]) i--; |
| 61 |
return i; |
| 62 |
} |
| 63 |
else if (x >= values[start]) { |
| 64 |
|
| 65 |
i = start + 1; |
| 66 |
while (i < n && x >= values[i]) i++; |
| 67 |
return i-1; |
| 68 |
} |
| 69 |
else { |
| 70 |
|
| 71 |
i = start - 1; |
| 72 |
while (i > -1 && x < values[i]) i--; |
| 73 |
return i; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
static int find_index_binary(double *values, int n, double x) |
| 80 |
{ |
| 81 |
int lo, hi, mid; |
| 82 |
|
| 83 |
lo = 0; |
| 84 |
hi = n; |
| 85 |
mid = n/2; |
| 86 |
|
| 87 |
if (x < values[0]) return -1; |
| 88 |
if (x >= values[n-1]) return n-1; |
| 89 |
|
| 90 |
while (hi - lo > 1) { |
| 91 |
if (x >= values[mid]) lo = mid; |
| 92 |
else hi = mid; |
| 93 |
mid = lo + (hi - lo) / 2; |
| 94 |
} |
| 95 |
|
| 96 |
return mid; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
static int is_monotonic(bs_array x) |
| 101 |
{ |
| 102 |
int i; |
| 103 |
int ok; |
| 104 |
ok = 1; |
| 105 |
for (i=1; i<x.size; i++) { |
| 106 |
ok &= (x.data[i*x.stride] >= x.data[(i-1)*x.stride]); |
| 107 |
} |
| 108 |
return ok; |
| 109 |
} |
| 110 |
|
| 111 |
static int min_points(bs_bctype left, bs_bctype right) |
| 112 |
{ |
| 113 |
|
| 114 |
return 2 + (left == BS_NOTAKNOT) + (right == BS_NOTAKNOT); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
static double* alloc_knots(bs_array x) |
| 124 |
{ |
| 125 |
int N; |
| 126 |
double *knots; |
| 127 |
int i; |
| 128 |
|
| 129 |
N = x.size; |
| 130 |
knots = malloc((N + 5) * sizeof(double)); |
| 131 |
|
| 132 |
|
| 133 |
knots += 2; |
| 134 |
|
| 135 |
|
| 136 |
for (i=0; i < N; i++) knots[i] = x.data[i * x.stride]; |
| 137 |
|
| 138 |
|
| 139 |
knots[-2] = knots[0] - 2.0 * (knots[1] - knots[0]); |
| 140 |
knots[-1] = knots[0] - 1.0 * (knots[1] - knots[0]); |
| 141 |
|
| 142 |
|
| 143 |
knots[N] = knots[N-1] + 1.0 * (knots[N-1] - knots[N-2]); |
| 144 |
knots[N+1] = knots[N-1] + 2.0 * (knots[N-1] - knots[N-2]); |
| 145 |
knots[N+2] = knots[N-1] + 3.0 * (knots[N-1] - knots[N-2]); |
| 146 |
|
| 147 |
return knots; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
static void free_knots(double *knots) { |
| 152 |
free(knots - 2); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
static double* alloc_constants(double *knots, int n) |
| 160 |
{ |
| 161 |
int i; |
| 162 |
double *constants; |
| 163 |
|
| 164 |
constants = malloc(4 * n * sizeof(double)); |
| 165 |
|
| 166 |
for (i=0; i<n; i++) { |
| 167 |
constants[4*i+0] = 1.0 / ((knots[i+1] - knots[i-2]) * |
| 168 |
(knots[i+1] - knots[i-1]) * |
| 169 |
(knots[i+1] - knots[i ])); |
| 170 |
|
| 171 |
constants[4*i+1] = 1.0 / ((knots[i+3] - knots[i ]) * |
| 172 |
(knots[i+2] - knots[i ]) * |
| 173 |
(knots[i+1] - knots[i ])); |
| 174 |
|
| 175 |
constants[4*i+2] = 1.0 / ((knots[i+2] - knots[i-1]) * |
| 176 |
(knots[i+1] - knots[i-1]) * |
| 177 |
(knots[i+1] - knots[i ])); |
| 178 |
|
| 179 |
constants[4*i+3] = 1.0 / ((knots[i+2] - knots[i-1]) * |
| 180 |
(knots[i+2] - knots[i ]) * |
| 181 |
(knots[i+1] - knots[i ])); |
| 182 |
} |
| 183 |
|
| 184 |
return constants; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
static void b3nonzeros(double x, int i, double* RESTRICT t, |
| 204 |
double* RESTRICT consts, double* RESTRICT out) |
| 205 |
{ |
| 206 |
double* RESTRICT c = consts + 4*i; |
| 207 |
|
| 208 |
double dx1 = x - t[i-2]; |
| 209 |
double dx2 = x - t[i-1]; |
| 210 |
double dx3 = x - t[i]; |
| 211 |
double dx4 = t[i+1] - x; |
| 212 |
double dx5 = t[i+2] - x; |
| 213 |
double dx6 = t[i+3] - x; |
| 214 |
|
| 215 |
double tmp1 = dx4 * dx4 * c[0]; |
| 216 |
double tmp2 = dx3 * dx3 * c[1]; |
| 217 |
double tmp3 = dx2 * dx4 * c[2] + dx5 * dx3 * c[3]; |
| 218 |
|
| 219 |
out[0] = dx4 * tmp1; |
| 220 |
out[1] = dx1 * tmp1 + dx5 * tmp3; |
| 221 |
out[2] = dx6 * tmp2 + dx2 * tmp3; |
| 222 |
out[3] = dx3 * tmp2; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
static void db3nonzeros(double x, int i, double* RESTRICT t, |
| 228 |
double* RESTRICT consts, double out[4]) |
| 229 |
{ |
| 230 |
double* RESTRICT c = consts + 4*i; |
| 231 |
|
| 232 |
double dx1 = x - t[i-2]; |
| 233 |
double dx2 = x - t[i-1]; |
| 234 |
double dx3 = x - t[i]; |
| 235 |
double dx4 = t[i+1] - x; |
| 236 |
double dx5 = t[i+2] - x; |
| 237 |
double dx6 = t[i+3] - x; |
| 238 |
|
| 239 |
double tmp1 = dx4 * c[0]; |
| 240 |
double tmp2 = dx3 * c[1]; |
| 241 |
double tmp3 = dx2 * c[2]; |
| 242 |
double tmp4 = dx5 * c[3]; |
| 243 |
|
| 244 |
out[0] = -3.0 * dx4 * tmp1; |
| 245 |
|
| 246 |
out[1] = (( dx4 - 2.0 * dx1) * tmp1 + |
| 247 |
(- dx4 - dx5) * tmp3 + |
| 248 |
(- 2.0 * dx3 + dx5) * tmp4 + |
| 249 |
dx5 * dx4 * c[2]); |
| 250 |
|
| 251 |
out[2] = ((- dx3 + 2.0 * dx6) * tmp2 + |
| 252 |
(2.0 * dx4 - dx2) * tmp3 + |
| 253 |
( dx3 + dx2) * tmp4 |
| 254 |
- dx2 * dx3 * c[3]); |
| 255 |
|
| 256 |
out[3] = 3.0 * dx3 * tmp2; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
static void d2b3nonzeros(double x, int i, double* RESTRICT t, |
| 262 |
double* RESTRICT consts, double out[4]) |
| 263 |
{ |
| 264 |
double* RESTRICT c = consts + 4*i; |
| 265 |
|
| 266 |
double dx1 = x - t[i-2]; |
| 267 |
double dx2 = x - t[i-1]; |
| 268 |
double dx3 = x - t[i]; |
| 269 |
double dx4 = t[i+1] - x; |
| 270 |
double dx5 = t[i+2] - x; |
| 271 |
double dx6 = t[i+3] - x; |
| 272 |
|
| 273 |
out[0] = 6.0 * dx4 * c[0]; |
| 274 |
|
| 275 |
out[1] = (- 2.0 * dx4 * c[0] |
| 276 |
- 2.0 * (dx4 - dx1) * c[0] |
| 277 |
- (dx4 - dx2) * c[2] |
| 278 |
+ (-dx5 - dx4) * c[2] |
| 279 |
- (dx5 - dx2) * c[2] |
| 280 |
- 2.0 * (dx5 - dx3) * c[3] |
| 281 |
- 2.0 * dx5 * c[3]); |
| 282 |
|
| 283 |
out[2] = (- 2.0 * dx3 * c[1] |
| 284 |
+ 2.0 * (dx6 - dx3) * c[1] |
| 285 |
+ 2.0 * (dx4 - dx2) * c[2] |
| 286 |
- 2.0 * dx2 * c[2] |
| 287 |
+ (dx5 - dx3) * c[3] |
| 288 |
- (dx2 + dx3) * c[3] |
| 289 |
+ (dx5 - dx2) * c[3]); |
| 290 |
|
| 291 |
out[3] = 6.0 * dx3 * c[1]; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
static void d3b3nonzeros(int i, double* RESTRICT consts, double out[4]) |
| 296 |
{ |
| 297 |
double* RESTRICT c = consts + 4*i; |
| 298 |
|
| 299 |
out[0] = -6.0 * c[0]; |
| 300 |
out[1] = 6.0 * (c[0] + c[2] + c[3]); |
| 301 |
out[2] = -6.0 * (c[1] + c[2] + c[3]); |
| 302 |
out[3] = 6.0 * c[1]; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
static const double ONESIXTH = 0.1666666666666666666; |
| 316 |
|
| 317 |
static void b3unonzeros(double x, double out[4]) |
| 318 |
{ |
| 319 |
double dx1 = x + 2.0; |
| 320 |
double dx2 = x + 1.0; |
| 321 |
double dx4 = 1.0 - x; |
| 322 |
double dx5 = 2.0 - x; |
| 323 |
double dx6 = 3.0 - x; |
| 324 |
|
| 325 |
double tmp1 = ONESIXTH * dx4 * dx4; |
| 326 |
double tmp2 = ONESIXTH * x * x; |
| 327 |
double tmp3 = ONESIXTH * (dx2 * dx4 + dx5 * x); |
| 328 |
|
| 329 |
out[0] = dx4 * tmp1; |
| 330 |
out[1] = dx1 * tmp1 + dx5 * tmp3; |
| 331 |
out[2] = dx6 * tmp2 + dx2 * tmp3; |
| 332 |
out[3] = x * tmp2; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
typedef struct { |
| 525 |
double *first; |
| 526 |
double *rows; |
| 527 |
double *last; |
| 528 |
} banded_matrix; |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
static banded_matrix alloc_banded_matrix(int M) |
| 534 |
{ |
| 535 |
double* first = malloc((5 + 3*(M-1) + 5) * sizeof(double)); |
| 536 |
banded_matrix m = {first, first + 5, first + 5 + 3*(M-1)}; |
| 537 |
return m; |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
static void free_banded_matrix(banded_matrix A) |
| 542 |
{ |
| 543 |
free(A.first); |
| 544 |
A.first = NULL; |
| 545 |
A.rows = NULL; |
| 546 |
A.last = NULL; |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
static void copy_banded_matrix(banded_matrix dst, banded_matrix src, int M) |
| 551 |
{ |
| 552 |
size_t nbytes = (10 + 3*(M-1)) * sizeof(double); |
| 553 |
memcpy(dst.first, src.first, nbytes); |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
static void solve(banded_matrix mat, double* RESTRICT b, int n) |
| 558 |
{ |
| 559 |
int i; |
| 560 |
double tmp; |
| 561 |
double* RESTRICT first = mat.first; |
| 562 |
double* RESTRICT A = mat.rows; |
| 563 |
double* RESTRICT last = mat.last; |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
for (i=1; i<4; i++) { |
| 573 |
b[i] /= A[3*i]; |
| 574 |
A[3*i+2] /= A[3*i]; |
| 575 |
A[3*i+1] /= A[3*i]; |
| 576 |
A[3*i] = 1.0; |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
b[0] -= first[0] * b[1]; |
| 586 |
first[2] -= first[0] * A[3*1+2]; |
| 587 |
first[1] -= first[0] * A[3*1+1]; |
| 588 |
first[0] = 0.0; |
| 589 |
|
| 590 |
b[0] -= first[1] * b[2]; |
| 591 |
first[3] -= first[1] * A[3*2+2]; |
| 592 |
first[2] -= first[1] * A[3*2+1]; |
| 593 |
first[1] = 0.0; |
| 594 |
|
| 595 |
b[0] /= first[2]; |
| 596 |
first[4] /= first[2]; |
| 597 |
first[3] /= first[2]; |
| 598 |
first[2] = 1.0; |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
b[3] -= A[3*3+0] * b[0]; |
| 607 |
A[3*3+2] -= A[3*3+0] * first[4]; |
| 608 |
A[3*3+1] -= A[3*3+0] * first[3]; |
| 609 |
A[3*3+0] = 0.0; |
| 610 |
|
| 611 |
b[3] /= A[3*3+1]; |
| 612 |
A[3*3+2] /= A[3*3+1]; |
| 613 |
A[3*3+1] = 1.0; |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
tmp = b[0]; |
| 621 |
b[0] = b[1]; |
| 622 |
A[3*0+0] = A[3*1+0]; |
| 623 |
A[3*0+1] = A[3*1+1]; |
| 624 |
A[3*0+2] = A[3*1+2]; |
| 625 |
|
| 626 |
b[1] = b[2]; |
| 627 |
A[3*1+0] = A[3*2+0]; |
| 628 |
A[3*1+1] = A[3*2+1]; |
| 629 |
A[3*1+2] = A[3*2+2]; |
| 630 |
|
| 631 |
b[2] = tmp; |
| 632 |
A[3*2+0] = first[2]; |
| 633 |
A[3*2+1] = first[3]; |
| 634 |
A[3*2+2] = first[4]; |
| 635 |
|
| 636 |
|
| 637 |
for (i=4; i<n-1; i++) { |
| 638 |
b[i] -= A[3*i+0] * b[i-1]; |
| 639 |
A[3*i+1] -= A[3*i+0] * A[3*(i-1)+2]; |
| 640 |
A[3*i+0] = 0.0; |
| 641 |
|
| 642 |
b[i] /= A[3*i+1]; |
| 643 |
A[3*i+2] /= A[3*i+1]; |
| 644 |
A[3*i+1] = 1.0; |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
b[n-1] -= last[0] * b[n-5]; |
| 658 |
if (n-5 < 3) { |
| 659 |
last[2] -= last[0] * A[3*(n-5)+2]; |
| 660 |
last[1] -= last[0] * A[3*(n-5)+1]; |
| 661 |
} |
| 662 |
else { |
| 663 |
last[1] -= last[0] * A[3*(n-5)+2]; |
| 664 |
} |
| 665 |
last[0] = 0.0; |
| 666 |
|
| 667 |
|
| 668 |
b[n-1] -= last[1] * b[n-4]; |
| 669 |
if (n-4 < 3) { |
| 670 |
last[3] -= last[1] * A[3*(n-4)+2]; |
| 671 |
last[2] -= last[1] * A[3*(n-4)+1]; |
| 672 |
} |
| 673 |
else { |
| 674 |
last[2] -= last[1] * A[3*(n-4)+2]; |
| 675 |
} |
| 676 |
last[1] = 0.0; |
| 677 |
|
| 678 |
|
| 679 |
b[n-1] -= last[2] * b[n-3]; |
| 680 |
if (n-3 < 3) { |
| 681 |
last[4] -= last[2] * A[3*(n-3)+2]; |
| 682 |
last[3] -= last[2] * A[3*(n-3)+1]; |
| 683 |
} |
| 684 |
else { |
| 685 |
last[3] -= last[2] * A[3*(n-3)+2]; |
| 686 |
} |
| 687 |
last[2] = 0.0; |
| 688 |
|
| 689 |
|
| 690 |
b[n-1] -= last[3] * b[n-2]; |
| 691 |
last[4] -= last[3] * A[3*(n-2)+2]; |
| 692 |
last[3] = 0.0; |
| 693 |
|
| 694 |
|
| 695 |
b[n-1] /= last[4]; |
| 696 |
last[4] = 1.0; |
| 697 |
|
| 698 |
|
| 699 |
for (i=n-2; i>=3; i--) { |
| 700 |
b[i] -= b[i+1] * A[3*i+2]; |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
b[2] -= b[3] * A[3*2+1] + b[4] * A[3*2+2]; |
| 713 |
b[1] -= b[2] * A[3*1+1] + b[3] * A[3*1+2]; |
| 714 |
b[0] -= b[1] * A[3*0+1] + b[2] * A[3*0+2]; |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
static void notaknot_row(double *consts, int i, double row[5]) |
| 723 |
{ |
| 724 |
int j; |
| 725 |
double buf[4]; |
| 726 |
|
| 727 |
d3b3nonzeros(i-1, consts, row); |
| 728 |
d3b3nonzeros(i, consts, buf); |
| 729 |
row[4] = 0.0; |
| 730 |
for (j=0; j<4; j++) { |
| 731 |
row[j+1] -= buf[j]; |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
static void fill_banded_matrix(banded_matrix A, double* RESTRICT knots, |
| 737 |
double* RESTRICT consts, int N, |
| 738 |
bs_bctype bctypes[2]) |
| 739 |
{ |
| 740 |
int i; |
| 741 |
double* RESTRICT first = A.first; |
| 742 |
double* RESTRICT rows = A.rows; |
| 743 |
double* RESTRICT last = A.last; |
| 744 |
|
| 745 |
|
| 746 |
switch (bctypes[0]) { |
| 747 |
case BS_DERIV1: |
| 748 |
db3nonzeros(knots[0], 0, knots, consts, first); |
| 749 |
first[3] = first[4] = 0.0; |
| 750 |
break; |
| 751 |
case BS_DERIV2: |
| 752 |
d2b3nonzeros(knots[0], 0, knots, consts, first); |
| 753 |
first[3] = first[4] = 0.0; |
| 754 |
break; |
| 755 |
case BS_NOTAKNOT: |
| 756 |
notaknot_row(consts, 1, first); |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
for (i=0; i<N; i++) { |
| 762 |
b3nonzeros(knots[i], i, knots, consts, rows + 3*(i+1)); |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
switch (bctypes[1]) { |
| 767 |
case BS_DERIV1: |
| 768 |
db3nonzeros(knots[N-1], N-1, knots, consts, last); |
| 769 |
for (i=4; i>1; i--) last[i] = last[i-2]; |
| 770 |
last[0] = last[1] = 0.0; |
| 771 |
break; |
| 772 |
case BS_DERIV2: |
| 773 |
d2b3nonzeros(knots[N-1], N-1, knots, consts, last); |
| 774 |
for (i=4; i>1; i--) last[i] = last[i-2]; |
| 775 |
last[0] = last[1] = 0.0; |
| 776 |
break; |
| 777 |
case BS_NOTAKNOT: |
| 778 |
notaknot_row(consts, N-2, last); |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
static void find_1d_coefficients(banded_matrix A, |
| 788 |
bs_array values, double bcvalues[2], |
| 789 |
double* RESTRICT coeffs) |
| 790 |
{ |
| 791 |
int i; |
| 792 |
int N = values.size; |
| 793 |
int M = N+2; |
| 794 |
|
| 795 |
coeffs[0] = bcvalues[0]; |
| 796 |
for (i=0; i<N; i++) { |
| 797 |
coeffs[i+1] = values.data[i * values.stride]; |
| 798 |
} |
| 799 |
coeffs[M-1] = bcvalues[1]; |
| 800 |
|
| 801 |
solve(A, coeffs, M); |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
bs_errorcode bs_spline1d_create(bs_array x, bs_array y, bs_bcs bcs, |
| 809 |
bs_exts exts, bs_spline1d **out) |
| 810 |
{ |
| 811 |
int N, M; |
| 812 |
bs_spline1d* spline; |
| 813 |
banded_matrix A; |
| 814 |
bs_bctype bctypes[2] = {bcs.left.type, bcs.right.type}; |
| 815 |
double bcvalues[2]; |
| 816 |
|
| 817 |
*out = NULL; |
| 818 |
|
| 819 |
|
| 820 |
if (x.size != y.size) return BS_SIZEMISMATCH; |
| 821 |
if (!is_monotonic(x)) return BS_NOTMONOTONIC; |
| 822 |
if (x.size < min_points(bcs.left.type, bcs.right.type)) |
| 823 |
return BS_TOOFEWPOINTS; |
| 824 |
|
| 825 |
spline = malloc(sizeof(bs_spline1d)); |
| 826 |
|
| 827 |
N = x.size; |
| 828 |
M = N + 2; |
| 829 |
|
| 830 |
spline->knots = alloc_knots(x); |
| 831 |
spline->n = N; |
| 832 |
spline->exts = exts; |
| 833 |
spline->consts = alloc_constants(spline->knots, N); |
| 834 |
spline->coeffs = malloc(M * sizeof(double)); |
| 835 |
|
| 836 |
A = alloc_banded_matrix(M); |
| 837 |
|
| 838 |
fill_banded_matrix(A, spline->knots, spline->consts, N, bctypes); |
| 839 |
|
| 840 |
bcvalues[0] = (bcs.left.type == BS_NOTAKNOT) ? 0.0 : bcs.left.value; |
| 841 |
bcvalues[1] = (bcs.right.type == BS_NOTAKNOT) ? 0.0 : bcs.right.value; |
| 842 |
|
| 843 |
find_1d_coefficients(A, y, bcvalues, spline->coeffs); |
| 844 |
|
| 845 |
free_banded_matrix(A); |
| 846 |
|
| 847 |
*out = spline; |
| 848 |
return BS_OK; |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
void bs_spline1d_free(bs_spline1d* spline) |
| 853 |
{ |
| 854 |
if (spline != NULL) { |
| 855 |
free_knots(spline->knots); |
| 856 |
free(spline->consts); |
| 857 |
free(spline->coeffs); |
| 858 |
free(spline); |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
bs_errorcode bs_spline1d_eval(bs_spline1d *spline, bs_array x, bs_array out) |
| 864 |
{ |
| 865 |
int i; |
| 866 |
int j; |
| 867 |
double xval; |
| 868 |
double b3vals[4]; |
| 869 |
|
| 870 |
|
| 871 |
i = find_index_binary(spline->knots, spline->n, x.data[0]); |
| 872 |
|
| 873 |
for (j=0; j<x.size; j++) { |
| 874 |
xval = x.data[j*x.stride]; |
| 875 |
i = find_index_from(spline->knots, spline->n, xval, i); |
| 876 |
|
| 877 |
|
| 878 |
if (i == -1) { |
| 879 |
switch (spline->exts.left.type) { |
| 880 |
case BS_EXTRAPOLATE: |
| 881 |
i = 0; |
| 882 |
break; |
| 883 |
case BS_CONSTANT: |
| 884 |
i = 0; |
| 885 |
xval = spline->knots[0]; |
| 886 |
break; |
| 887 |
case BS_VALUE: |
| 888 |
out.data[j * out.stride] = spline->exts.left.value; |
| 889 |
continue; |
| 890 |
case BS_RAISE: |
| 891 |
return BS_DOMAINERROR; |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
else if (i == spline->n - 1) { |
| 897 |
switch (spline->exts.right.type) { |
| 898 |
case BS_EXTRAPOLATE: |
| 899 |
i = spline->n - 2; |
| 900 |
break; |
| 901 |
case BS_CONSTANT: |
| 902 |
i = spline->n-2; |
| 903 |
xval = spline->knots[spline->n-1]; |
| 904 |
break; |
| 905 |
case BS_VALUE: |
| 906 |
out.data[j * out.stride] = spline->exts.right.value; |
| 907 |
continue; |
| 908 |
case BS_RAISE: |
| 909 |
return BS_DOMAINERROR; |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
b3nonzeros(xval, i, spline->knots, spline->consts, b3vals); |
| 915 |
out.data[j*out.stride] = (spline->coeffs[i] * b3vals[0] + |
| 916 |
spline->coeffs[i+1] * b3vals[1] + |
| 917 |
spline->coeffs[i+2] * b3vals[2] + |
| 918 |
spline->coeffs[i+3] * b3vals[3]); |
| 919 |
} |
| 920 |
|
| 921 |
return BS_OK; |
| 922 |
} |
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
static double get_bcarray_value(bs_bcarray bc, int i) |
| 931 |
{ |
| 932 |
return (bc.type == BS_NOTAKNOT) ? 0.0 : bc.data[i * bc.stride]; |
| 933 |
} |
| 934 |
|
| 935 |
static int bcarray_size_match(bs_bcarray bc, int size) |
| 936 |
{ |
| 937 |
|
| 938 |
return (bc.type == BS_NOTAKNOT) ? 1 : (bc.size == size); |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
bs_errorcode bs_spline2d_create(bs_array x, bs_array y, bs_array2d z, |
| 943 |
bs_bcarray_pair xbcs, bs_bcarray_pair ybcs, |
| 944 |
bs_exts xexts, bs_exts yexts, |
| 945 |
bs_spline2d **out) |
| 946 |
{ |
| 947 |
int i, j; |
| 948 |
int nx, mx, ny, my; |
| 949 |
bs_bctype bctypes[2] = {ybcs.left.type, ybcs.right.type}; |
| 950 |
double bcvalues[2]; |
| 951 |
bs_spline2d* spline; |
| 952 |
double *coeffs; |
| 953 |
banded_matrix A, Awork; |
| 954 |
bs_array zslice, coeffs_slice; |
| 955 |
double *buf; |
| 956 |
|
| 957 |
*out = NULL; |
| 958 |
|
| 959 |
if ((x.size != z.sizes[0]) || (y.size != z.sizes[1])) |
| 960 |
return BS_SIZEMISMATCH; |
| 961 |
if (!is_monotonic(x) || !is_monotonic(y)) |
| 962 |
return BS_NOTMONOTONIC; |
| 963 |
if ((x.size < min_points(xbcs.left.type, xbcs.right.type)) || |
| 964 |
(y.size < min_points(ybcs.left.type, ybcs.right.type))) |
| 965 |
return BS_TOOFEWPOINTS; |
| 966 |
|
| 967 |
|
| 968 |
if (!(bcarray_size_match(xbcs.left, y.size) && |
| 969 |
bcarray_size_match(xbcs.right, y.size) && |
| 970 |
bcarray_size_match(ybcs.left, x.size) && |
| 971 |
bcarray_size_match(ybcs.left, x.size))) |
| 972 |
return BS_BCSIZEMISMATCH; |
| 973 |
|
| 974 |
spline = malloc(sizeof(bs_spline2d)); |
| 975 |
|
| 976 |
nx = x.size; |
| 977 |
mx = nx + 2; |
| 978 |
|
| 979 |
ny = y.size; |
| 980 |
my = ny + 2; |
| 981 |
|
| 982 |
spline->xknots = alloc_knots(x); |
| 983 |
spline->xconsts = alloc_constants(spline->xknots, nx); |
| 984 |
spline->nx = nx; |
| 985 |
spline->xexts = xexts; |
| 986 |
|
| 987 |
spline->yknots = alloc_knots(y); |
| 988 |
spline->yconsts = alloc_constants(spline->yknots, ny); |
| 989 |
spline->ny = ny; |
| 990 |
spline->yexts = yexts; |
| 991 |
|
| 992 |
coeffs = malloc(mx * my * sizeof(double)); |
| 993 |
|
| 994 |
|
| 995 |
A = alloc_banded_matrix(my); |
| 996 |
Awork = alloc_banded_matrix(my); |
| 997 |
|
| 998 |
fill_banded_matrix(A, spline->yknots, spline->yconsts, spline->ny, |
| 999 |
bctypes); |
| 1000 |
|
| 1001 |
for (i=0; i<nx; i++) { |
| 1002 |
zslice.data = z.data + z.strides[0]*i; |
| 1003 |
zslice.size = z.sizes[1]; |
| 1004 |
zslice.stride = z.strides[1]; |
| 1005 |
|
| 1006 |
bcvalues[0] = get_bcarray_value(ybcs.left, i); |
| 1007 |
bcvalues[1] = get_bcarray_value(ybcs.right, i); |
| 1008 |
|
| 1009 |
copy_banded_matrix(Awork, A, my); |
| 1010 |
find_1d_coefficients(Awork, zslice, bcvalues, coeffs+(i*my)); |
| 1011 |
} |
| 1012 |
|
| 1013 |
free_banded_matrix(A); |
| 1014 |
free_banded_matrix(Awork); |
| 1015 |
|
| 1016 |
|
| 1017 |
A = alloc_banded_matrix(mx); |
| 1018 |
Awork = alloc_banded_matrix(mx); |
| 1019 |
|
| 1020 |
bctypes[0] = xbcs.left.type; |
| 1021 |
bctypes[1] = xbcs.right.type; |
| 1022 |
fill_banded_matrix(A, spline->xknots, spline->xconsts, spline->nx, |
| 1023 |
bctypes); |
| 1024 |
|
| 1025 |
buf = malloc(mx * sizeof(double)); |
| 1026 |
for (i=0; i<my; i++) { |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
copy_banded_matrix(Awork, A, mx); |
| 1031 |
coeffs_slice.data = coeffs + i; |
| 1032 |
coeffs_slice.size = nx; |
| 1033 |
coeffs_slice.stride = my; |
| 1034 |
|
| 1035 |
bcvalues[0] = get_bcarray_value(xbcs.left, i); |
| 1036 |
bcvalues[1] = get_bcarray_value(xbcs.right, i); |
| 1037 |
|
| 1038 |
find_1d_coefficients(Awork, coeffs_slice, bcvalues, buf); |
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
for (j=0; j<mx; j++) coeffs[i+my*j] = buf[j]; |
| 1043 |
} |
| 1044 |
|
| 1045 |
free_banded_matrix(A); |
| 1046 |
free_banded_matrix(Awork); |
| 1047 |
free(buf); |
| 1048 |
|
| 1049 |
spline->coeffs = coeffs; |
| 1050 |
*out = spline; |
| 1051 |
|
| 1052 |
return BS_OK; |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
void bs_spline2d_free(bs_spline2d* spline) |
| 1057 |
{ |
| 1058 |
if (spline != NULL) { |
| 1059 |
free_knots(spline->xknots); |
| 1060 |
free(spline->xconsts); |
| 1061 |
free_knots(spline->yknots); |
| 1062 |
free(spline->yconsts); |
| 1063 |
free(spline->coeffs); |
| 1064 |
free(spline); |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
|
| 1069 |
bs_errorcode bs_spline2d_eval(bs_spline2d *spline, bs_array x, bs_array y, |
| 1070 |
bs_array2d out) |
| 1071 |
{ |
| 1072 |
|
| 1073 |
int i = find_index_binary(spline->xknots, spline->nx, x.data[0]); |
| 1074 |
int j0 = find_index_binary(spline->yknots, spline->ny, y.data[0]); |
| 1075 |
int j, k, l; |
| 1076 |
int my = spline->ny + 2; |
| 1077 |
double xb3vals[4]; |
| 1078 |
double yb3vals[4]; |
| 1079 |
double xval, yval; |
| 1080 |
|
| 1081 |
for (k=0; k<x.size; k++) { |
| 1082 |
xval = x.data[k*x.stride]; |
| 1083 |
i = find_index_from(spline->xknots, spline->nx, xval, i); |
| 1084 |
|
| 1085 |
|
| 1086 |
if (i == -1) { |
| 1087 |
switch (spline->xexts.left.type) { |
| 1088 |
case BS_EXTRAPOLATE: |
| 1089 |
i = 0; |
| 1090 |
break; |
| 1091 |
case BS_CONSTANT: |
| 1092 |
i = 0; |
| 1093 |
xval = spline->xknots[0]; |
| 1094 |
break; |
| 1095 |
case BS_VALUE: |
| 1096 |
for (l=0; l<y.size; l++) { |
| 1097 |
out.data[k * out.strides[0] + l * out.strides[1]] = |
| 1098 |
spline->xexts.left.value; |
| 1099 |
} |
| 1100 |
continue; |
| 1101 |
case BS_RAISE: |
| 1102 |
return BS_DOMAINERROR; |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
|
| 1107 |
else if (i == spline->nx - 1) { |
| 1108 |
switch (spline->xexts.right.type) { |
| 1109 |
case BS_EXTRAPOLATE: |
| 1110 |
i = spline->nx - 2; |
| 1111 |
break; |
| 1112 |
case BS_CONSTANT: |
| 1113 |
i = spline->nx - 2; |
| 1114 |
xval = spline->xknots[spline->nx-1]; |
| 1115 |
break; |
| 1116 |
case BS_VALUE: |
| 1117 |
for (l=0; l<y.size; l++) { |
| 1118 |
out.data[k * out.strides[0] + l * out.strides[1]] = |
| 1119 |
spline->xexts.right.value; |
| 1120 |
} |
| 1121 |
continue; |
| 1122 |
case BS_RAISE: |
| 1123 |
return BS_DOMAINERROR; |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
|
| 1128 |
b3nonzeros(xval, i, spline->xknots, spline->xconsts, xb3vals); |
| 1129 |
|
| 1130 |
|
| 1131 |
j = j0; |
| 1132 |
for (l=0; l<y.size; l++) { |
| 1133 |
yval = y.data[l*y.stride]; |
| 1134 |
j = find_index_from(spline->yknots, spline->ny, yval, j); |
| 1135 |
|
| 1136 |
|
| 1137 |
if (j == -1) { |
| 1138 |
switch (spline->yexts.left.type) { |
| 1139 |
case BS_EXTRAPOLATE: |
| 1140 |
j = 0; |
| 1141 |
break; |
| 1142 |
case BS_CONSTANT: |
| 1143 |
j = 0; |
| 1144 |
yval = spline->yknots[0]; |
| 1145 |
break; |
| 1146 |
case BS_VALUE: |
| 1147 |
out.data[k * out.strides[0] + l * out.strides[1]] = |
| 1148 |
spline->yexts.left.value; |
| 1149 |
continue; |
| 1150 |
case BS_RAISE: |
| 1151 |
return BS_DOMAINERROR; |
| 1152 |
} |
| 1153 |
} |
| 1154 |
|
| 1155 |
|
| 1156 |
else if (j == spline->ny - 1) { |
| 1157 |
switch (spline->yexts.right.type) { |
| 1158 |
case BS_EXTRAPOLATE: |
| 1159 |
j = spline->ny - 2; |
| 1160 |
break; |
| 1161 |
case BS_CONSTANT: |
| 1162 |
j = spline->ny - 2; |
| 1163 |
yval = spline->yknots[spline->ny-1]; |
| 1164 |
break; |
| 1165 |
case BS_VALUE: |
| 1166 |
out.data[k * out.strides[0] + l * out.strides[1]] = |
| 1167 |
spline->yexts.right.value; |
| 1168 |
continue; |
| 1169 |
case BS_RAISE: |
| 1170 |
return BS_DOMAINERROR; |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
b3nonzeros(yval, j, spline->yknots, spline->yconsts, yb3vals); |
| 1176 |
|
| 1177 |
out.data[k * out.strides[0] + l * out.strides[1]] = |
| 1178 |
(spline->coeffs[(i )*my+j] * xb3vals[0] * yb3vals[0] + |
| 1179 |
spline->coeffs[(i )*my+j+1] * xb3vals[0] * yb3vals[1] + |
| 1180 |
spline->coeffs[(i )*my+j+2] * xb3vals[0] * yb3vals[2] + |
| 1181 |
spline->coeffs[(i )*my+j+3] * xb3vals[0] * yb3vals[3] + |
| 1182 |
|
| 1183 |
spline->coeffs[(i+1)*my+j] * xb3vals[1] * yb3vals[0] + |
| 1184 |
spline->coeffs[(i+1)*my+j+1] * xb3vals[1] * yb3vals[1] + |
| 1185 |
spline->coeffs[(i+1)*my+j+2] * xb3vals[1] * yb3vals[2] + |
| 1186 |
spline->coeffs[(i+1)*my+j+3] * xb3vals[1] * yb3vals[3] + |
| 1187 |
|
| 1188 |
spline->coeffs[(i+2)*my+j] * xb3vals[2] * yb3vals[0] + |
| 1189 |
spline->coeffs[(i+2)*my+j+1] * xb3vals[2] * yb3vals[1] + |
| 1190 |
spline->coeffs[(i+2)*my+j+2] * xb3vals[2] * yb3vals[2] + |
| 1191 |
spline->coeffs[(i+2)*my+j+3] * xb3vals[2] * yb3vals[3] + |
| 1192 |
|
| 1193 |
spline->coeffs[(i+3)*my+j] * xb3vals[3] * yb3vals[0] + |
| 1194 |
spline->coeffs[(i+3)*my+j+1] * xb3vals[3] * yb3vals[1] + |
| 1195 |
spline->coeffs[(i+3)*my+j+2] * xb3vals[3] * yb3vals[2] + |
| 1196 |
spline->coeffs[(i+3)*my+j+3] * xb3vals[3] * yb3vals[3]); |
| 1197 |
} |
| 1198 |
} |
| 1199 |
|
| 1200 |
return BS_OK; |
| 1201 |
} |
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
static void fill_banded_matrix_u(banded_matrix A, int N, double didx, |
| 1207 |
bs_bctype bctypes[2]) |
| 1208 |
{ |
| 1209 |
int i, j; |
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
const double b3vals[3] = {1.0/6.0, 2.0/3.0, 1.0/6.0}; |
| 1214 |
const double db3vals[3] = {-0.5 * didx, 0.0, 0.5 * didx}; |
| 1215 |
const double d2b3vals[3] = {didx * didx, -2.0 * didx * didx, didx * didx}; |
| 1216 |
const double notaknot_row[5] = {-1.0, 4.0, -6.0, 4.0, -1.0}; |
| 1217 |
|
| 1218 |
|
| 1219 |
switch (bctypes[0]) { |
| 1220 |
case BS_DERIV1: |
| 1221 |
for (i=0; i<3; i++) A.first[i] = db3vals[i]; |
| 1222 |
A.first[3] = A.first[4] = 0.0; |
| 1223 |
break; |
| 1224 |
case BS_DERIV2: |
| 1225 |
for (i=0; i<3; i++) A.first[i] = d2b3vals[i]; |
| 1226 |
A.first[3] = A.first[4] = 0.0; |
| 1227 |
break; |
| 1228 |
case BS_NOTAKNOT: |
| 1229 |
for (i=0; i<5; i++) A.first[i] = notaknot_row[i]; |
| 1230 |
} |
| 1231 |
|
| 1232 |
|
| 1233 |
for (i=0; i<N; i++) { |
| 1234 |
for (j=0; j<3; j++) A.rows[3*(i+1)+j] = b3vals[j]; |
| 1235 |
} |
| 1236 |
|
| 1237 |
|
| 1238 |
switch (bctypes[1]) { |
| 1239 |
case BS_DERIV1: |
| 1240 |
A.last[0] = A.last[1] = 0.0; |
| 1241 |
for (i=0; i<3; i++) A.last[i+2] = db3vals[i]; |
| 1242 |
break; |
| 1243 |
case BS_DERIV2: |
| 1244 |
A.last[0] = A.last[1] = 0.0; |
| 1245 |
for (i=0; i<3; i++) A.last[i+2] = d2b3vals[i]; |
| 1246 |
break; |
| 1247 |
case BS_NOTAKNOT: |
| 1248 |
for (i=0; i<5; i++) A.last[i] = notaknot_row[i]; |
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
bs_errorcode bs_uspline1d_create(bs_range x, bs_array y, bs_bcs bcs, |
| 1255 |
bs_exts exts, bs_uspline1d **out) |
| 1256 |
{ |
| 1257 |
bs_uspline1d* spline; |
| 1258 |
int i, N, M; |
| 1259 |
double didx; |
| 1260 |
double* RESTRICT b; |
| 1261 |
banded_matrix A; |
| 1262 |
bs_bctype bctypes[2] = {bcs.left.type, bcs.right.type}; |
| 1263 |
|
| 1264 |
if (y.size < min_points(bcs.left.type, bcs.right.type)) |
| 1265 |
return BS_TOOFEWPOINTS; |
| 1266 |
|
| 1267 |
spline = malloc(sizeof(bs_uspline1d)); |
| 1268 |
|
| 1269 |
N = y.size; |
| 1270 |
M = N + 2; |
| 1271 |
didx = (N - 1) / (x.max - x.min); |
| 1272 |
|
| 1273 |
spline->x = x; |
| 1274 |
spline->didx = didx; |
| 1275 |
|
| 1276 |
spline->n = N; |
| 1277 |
spline->exts = exts; |
| 1278 |
|
| 1279 |
b = malloc(M * sizeof(double)); |
| 1280 |
|
| 1281 |
A = alloc_banded_matrix(M); |
| 1282 |
|
| 1283 |
fill_banded_matrix_u(A, N, didx, bctypes); |
| 1284 |
|
| 1285 |
|
| 1286 |
b[0] = (bcs.left.type == BS_NOTAKNOT)? 0.0: bcs.left.value; |
| 1287 |
b[M-1] = (bcs.right.type == BS_NOTAKNOT)? 0.0: bcs.right.value; |
| 1288 |
for (i=0; i<N; i++) { |
| 1289 |
b[i+1] = y.data[i * y.stride]; |
| 1290 |
} |
| 1291 |
|
| 1292 |
solve(A, b, M); |
| 1293 |
free_banded_matrix(A); |
| 1294 |
|
| 1295 |
spline->coeffs = b; |
| 1296 |
*out = spline; |
| 1297 |
return BS_OK; |
| 1298 |
} |
| 1299 |
|
| 1300 |
|
| 1301 |
void bs_uspline1d_free(bs_uspline1d* spline) |
| 1302 |
{ |
| 1303 |
if (spline != NULL) { |
| 1304 |
free(spline->coeffs); |
| 1305 |
free(spline); |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
|
| 1310 |
bs_errorcode bs_uspline1d_eval(bs_uspline1d *spline, bs_array x, bs_array out) |
| 1311 |
{ |
| 1312 |
int i, j; |
| 1313 |
double xval; |
| 1314 |
double xfloor; |
| 1315 |
double b3vals[4]; |
| 1316 |
for (j=0; j<x.size; j++) { |
| 1317 |
|
| 1318 |
xval = (x.data[j*x.stride] - spline->x.min) * spline->didx; |
| 1319 |
xfloor = floor(xval); |
| 1320 |
i = (int)xfloor; |
| 1321 |
|
| 1322 |
|
| 1323 |
if (i < 0) { |
| 1324 |
switch (spline->exts.left.type) { |
| 1325 |
case BS_EXTRAPOLATE: |
| 1326 |
i = 0; |
| 1327 |
xfloor = 0.0; |
| 1328 |
break; |
| 1329 |
case BS_CONSTANT: |
| 1330 |
i = 0; |
| 1331 |
xfloor = 0.0; |
| 1332 |
xval = 0.0; |
| 1333 |
break; |
| 1334 |
case BS_VALUE: |
| 1335 |
out.data[j * out.stride] = spline->exts.left.value; |
| 1336 |
continue; |
| 1337 |
case BS_RAISE: |
| 1338 |
return BS_DOMAINERROR; |
| 1339 |
} |
| 1340 |
} |
| 1341 |
|
| 1342 |
|
| 1343 |
else if (i >= spline->n-1) { |
| 1344 |
switch (spline->exts.right.type) { |
| 1345 |
case BS_EXTRAPOLATE: |
| 1346 |
i = spline->n - 2; |
| 1347 |
xfloor = i; |
| 1348 |
break; |
| 1349 |
case BS_CONSTANT: |
| 1350 |
i = spline->n - 2; |
| 1351 |
xfloor = i; |
| 1352 |
xval = xfloor+1.0; |
| 1353 |
break; |
| 1354 |
case BS_VALUE: |
| 1355 |
out.data[j * out.stride] = spline->exts.right.value; |
| 1356 |
continue; |
| 1357 |
case BS_RAISE: |
| 1358 |
return BS_DOMAINERROR; |
| 1359 |
} |
| 1360 |
} |
| 1361 |
|
| 1362 |
|
| 1363 |
b3unonzeros(xval - xfloor, b3vals); |
| 1364 |
out.data[j*out.stride] = (spline->coeffs[i] * b3vals[0] + |
| 1365 |
spline->coeffs[i+1] * b3vals[1] + |
| 1366 |
spline->coeffs[i+2] * b3vals[2] + |
| 1367 |
spline->coeffs[i+3] * b3vals[3]); |
| 1368 |
} |
| 1369 |
|
| 1370 |
return BS_OK; |
| 1371 |
} |