Debian Codegraph @

extern/bs.c

raw symbols SCIP
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 // debug stuff (remove later)
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 // search functions & helpers
39 //
40 // These all return i such that x>= values[i] and x<values[i+1].
41 // Return -1 if x < values[0].
42 // Return n-1 if x >= values[n-1].
43 //-----------------------------------------------------------------------------
44
45 // Linear search starting from guess that
46 // values[start] <= x < values[start+1].
47 static int find_index_from(double *values, int n, double x, int start)
48 {
49 int i;
50
51 if (start <= -1) {
52 // search down
53 i = 0;
54 while (i < n && x >= values[i]) i++;
55 return i-1;
56 }
57 else if (start >= n-1) {
58 // search down
59 i = n - 1;
60 while (i > -1 && x < values[i]) i--;
61 return i;
62 }
63 else if (x >= values[start]) {
64 // search up
65 i = start + 1;
66 while (i < n && x >= values[i]) i++;
67 return i-1;
68 }
69 else {
70 // search down
71 i = start - 1;
72 while (i > -1 && x < values[i]) i--;
73 return i;
74 }
75 }
76
77
78 // find index using binary search
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 // one additional point needed for each not-a-knot condition.
114 return 2 + (left == BS_NOTAKNOT) + (right == BS_NOTAKNOT);
115 }
116
117 //-----------------------------------------------------------------------------
118 // knots & constants
119 //-----------------------------------------------------------------------------
120
121 // fill spline knots based on x array (includes padding on either
122 // end of array).
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 // move pointer past initial two-element padding.
133 knots += 2;
134
135 // copy x into main part of knots
136 for (i=0; i < N; i++) knots[i] = x.data[i * x.stride];
137
138 // fill padded area before beginning
139 knots[-2] = knots[0] - 2.0 * (knots[1] - knots[0]);
140 knots[-1] = knots[0] - 1.0 * (knots[1] - knots[0]);
141
142 // fill padded area after end.
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 // constants used when evaluating a spline.
157 // constants + 4*i is a pointer to the four constants used
158 // when evaluating the spline in the range knots[i] <= x < knots[i+1].
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 // Compute the 4 basis functions that are nonzero, assuming t[i] <= x < t[i+1].
190 // These are: b_{3, i-3}(x), b_{3, i-2}(x), b_{3, i-1}(x), b_{3, i}(x)
191 //
192 // This is faster than computing them separately, as some parts of the
193 // calculation are shared. These can be derived by "manually inlining"
194 // recursive function calls in the formula for the basis function.
195 // (See tests for recursive version).
196 //
197 // consts[4*i] through consts[4*i+3] stores four constants used in calculation
198 // (constants are a function of knot spacings).
199 //
200 // t indicies from (i-2) to (i+3) are used.
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 // derivatives of previous function
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 // second derivatives
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 // third derivatives
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 // unit basis versions of b3nonzeros and friends
308 // knot locations in this basis are [0, 1, ..., N-1]
309 // For 0 <= x < 1 return b_{-3}(x), b_{-2}(x), b_{-1}(x), b_0(x)
310 // For i <= x < i+1 subtract i from x first. to get b_{i-3}(x), b_{i-2}(x), ...
311 // (works because all the basis functions are the same with a shift,
312 // so b_{-3}(x) = b_{i-3}(i+x).
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 // derivatives of previous function
338 static void db3unonzeros(double x, double out[4])
339 {
340 double dx1 = x + 2.0;
341 double dx2 = x + 1.0;
342 double dx4 = 1.0 - x;
343 double dx5 = 2.0 - x;
344 double dx6 = 3.0 - x;
345
346 double tmp1 = ONESIXTH * dx4;
347 double tmp2 = ONESIXTH * x;
348 double tmp3 = ONESIXTH * dx2;
349 double tmp4 = ONESIXTH * dx5;
350
351 out[0] = -3.0 * dx4 * tmp1;
352
353 out[1] = (( dx4 - 2.0 * dx1) * tmp1 +
354 (- dx4 - dx5) * tmp3 +
355 (- 2.0 * x + dx5) * tmp4 +
356 ONESIXTH * dx5 * dx4);
357
358 out[2] = ((- x + 2.0 * dx6) * tmp2 +
359 (2.0 * dx4 - dx2) * tmp3 +
360 ( x + dx2) * tmp4
361 - ONESIXTH * dx2 * x);
362
363 out[3] = 3.0 * x * tmp2;
364 }
365
366
367 // second derivatives
368 static void d2b3unonzeros(double x, double out[4])
369 {
370 double dx1 = x + 2.0;
371 double dx2 = x + 1.0;
372 double dx4 = 1.0 - x;
373 double dx5 = 2.0 - x;
374 double dx6 = 3.0 - x;
375
376 out[0] = ONESIXTH * 6.0 * dx4;
377
378 out[1] = ONESIXTH * (- 2.0 * dx4
379 - 2.0 * (dx4 - dx1)
380 - (dx4 - dx2)
381 + (-dx5 - dx4)
382 - (dx5 - dx2)
383 - 2.0 * (dx5 - x)
384 - 2.0 * dx5);
385
386 out[2] = ONESIXTH * (- 2.0 * x
387 + 2.0 * (dx6 - x)
388 + 2.0 * (dx4 - dx2)
389 - 2.0 * dx2
390 + (dx5 - x)
391 - (dx2 + x)
392 + (dx5 - dx2));
393
394 out[3] = ONESIXTH * 6.0 * x;
395 }
396
397 // third derivatives
398 static void d3b3unonzeros(double out[4])
399 {
400 out[0] = -1.0;
401 out[1] = 3.0;
402 out[2] = -3.0;
403 out[3] = 1.0;
404 }
405
406 */
407
408 //-----------------------------------------------------------------------------
409 // solve_simple()
410 //
411 // Solve A * x = b for x. The solution is stored in b.
412 //
413 // A is an almost tridiagonal n x n matrix with this form:
414 //
415 // | x x x |
416 // | x x x |
417 // | x x x |
418 // | ... |
419 // | x x x |
420 // | x x x |
421 // | x x x |
422 //
423 // Rows are contiguous in memory: e.g., A[0] through A[2]
424 // stores the second row (first row with three elements).
425 //
426 //-----------------------------------------------------------------------------
427
428 /*
429 static void solve_simple(double* RESTRICT A, double* RESTRICT b, int n)
430 {
431 int i;
432
433 // divide first row by upper left element
434 double t = A[0];
435 b[0] /= t;
436 A[2] /= t;
437 A[1] /= t;
438 A[0] = 1.0; // but not used again.
439
440 // subtract (first element of row 1) x (row 0) from row 1
441 // to eliminate first element of row 1.
442 t = A[3*1+0];
443 b[1] -= t * b[0];
444 A[3*1+2] -= t * A[2];
445 A[3*1+1] -= t * A[1];
446 A[3*1+0] = 0.0; // but not used again.
447
448 // divide row 1 by first nonzero element, to set it to 1.
449 t = A[3*1+1];
450 b[1] /= t;
451 A[3*1+2] /= t;
452 A[3*1+1] = 1.0; // but not used again.
453
454 for (i=2; i<n-1; i++) {
455
456 // subtract (first element of new row) * (previous row) from new row
457 // to eliminate first element.
458 t = A[3*i+0];
459 b[i] -= t * b[i-1];
460 // A[3*i+2] -= t * 0.0 // no-op b/c previous row is zero.
461 A[3*i+1] -= t * A[3*(i-1)+2];
462 A[3*i+0] = 0.0; // (previous row is 1.0) but not used again.
463
464 // divide new row by first non-zero element
465 t = A[3*i+1];
466 b[i] /= t;
467 A[3*i+2] /= t;
468 A[3*i+1] = 1.0;
469 }
470
471 // last row is different:
472 // subtract first element of last row * 3rd to last row from last row
473 b[n-1] -= A[3*(n-1)+0] * b[n-3];
474 // A[3*(n-1)+2] -= A[3*(n-1)+0] * 0.0; // no-op
475 A[3*(n-1)+1] -= A[3*(n-1)+0] * A[3*(n-3)+2];
476 A[3*(n-1)+0] = 0.0;
477
478 // subtract first non-zero element * previous row from last row
479 b[n-1] -= A[3*(n-1)+1] * b[n-2];
480 A[3*(n-1)+2] -= A[3*(n-1)+1] * A[3*(n-2)+2];
481 A[3*(n-1)+1] = 0.0;
482
483 // divide row by 1st non-zero element
484 b[n-1] /= A[3*(n-1)+2];
485 A[3*(n-1)+2] = 1.0;
486
487 // back substitute
488 for (i=n-2; i>0; i--) {
489 b[i] -= b[i+1] * A[3*i+2];
490 }
491
492 // first row is different
493 b[0] -= b[1] * A[1] + b[2] * A[2];
494 }
495 */
496
497
498 //-----------------------------------------------------------------------------
499 // solve()
500 //
501 // Solve A * x = b for x. The solution is stored in b.
502 //
503 // A is a matrix like this:
504 //
505 // | x x x x x |
506 // | x x x |
507 // | x x x |
508 // | ... |
509 // | x x x |
510 // | x x x |
511 // | x x x |
512 // | x x x x x |
513 //
514 // A is stored compactly in 3*n elements, with row i corresponding to A[3*i],
515 // with the exception of the first and last rows which are passed in
516 // separately because they are too large to be stored this way.
517 //
518 // Note that the first 3 and last 3 elements of A are initially empty as
519 // these row values are stored in `first` and `last`. In fact the last 3
520 // elements of A are not used at all.
521 //
522 //-----------------------------------------------------------------------------
523
524 typedef struct {
525 double *first;
526 double *rows; // first + 5
527 double *last; // first + 5 + 3*(M-1)
528 } banded_matrix;
529
530
531 // allocate storate for a banded matrix.
532 // M is total number of rows, including first and last.
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 // rows 1, 2, 3: divide by first non-zero
566 //
567 // x x x x x | y x x x x x | y
568 // x x x | y 1 x x | y
569 // x x x | y --> 1 x x | y
570 // x x x | y 1 x x | y
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 // eliminate first two elements of first row and divide by first non-zero.
580 //
581 // x x x x x | y 0 0 1 x x | y
582 // 1 x x | y 1 x x | y
583 // 1 x x | y --> 1 x x | y
584 // 1 x x | y 1 x x | y
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 // reduce row 3
601 //
602 // 0 0 1 x x | y 0 0 1 x x | y
603 // 1 x x | y 1 x x | y
604 // 1 x x | y --> 1 x x | y
605 // 1 x x | y 0 1 x | y
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 // permute first three rows:
616 // 0 0 1 x x | y 1 x x | y
617 // 1 x x | y 1 x x | y
618 // 1 x x | y --> 1 x x | y
619 // 0 1 x | y 0 1 x | y
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 // reduce rest of the middle rows
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 // we now have, e.g.,
648 // 1 x x | y
649 // 1 x x | y
650 // 1 x x | y (n-5)
651 // 0 1 x | y (n-4)
652 // 0 1 x | y (n-3)
653 // 0 1 x | y (n-2)
654 // x x x x x | y (n-1)
655
656 // eliminate first element of last row using the (n-5)th row.
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 // eliminate second element of last row using the (n-4)th row.
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 // eliminate third element of last row using the (n-3)rd row.
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 // eliminate forth element
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 // normalize last row
695 b[n-1] /= last[4];
696 last[4] = 1.0;
697
698 // back-substitute
699 for (i=n-2; i>=3; i--) {
700 b[i] -= b[i+1] * A[3*i+2];
701 }
702
703 // we now have:
704 // 1 x x | y
705 // 1 x x | y
706 // 1 x x | y
707 // 1 | y
708 // 1 | y
709 // ...
710 //
711 // eliminate the remaining elements.
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 // finding coefficients
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 // Left boundary condition
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 // fill rows 1 through M-1 with values of b_{i-3}, b_{i-2}, b{i-1}
760 // at knot i.
761 for (i=0; i<N; i++) {
762 b3nonzeros(knots[i], i, knots, consts, rows + 3*(i+1));
763 }
764
765 // Right boundary condition
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 // Find spline coefficients along one dimension.
784 // knots and consts are as belong to a spline.
785 // A should be allocated with M = values.size + 2.
786 // coeffs should be size M.
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 // spline1d
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; // In case of error, ensure that output pointer is NULL.
818
819 // checks
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 // for first index, it could be anywhere, so use binary search
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 // index outside left boundary
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 // index outside right boundary
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 // if we get this far, we're either extrapolating or xval is in range.
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 // spline2d
927 //-----------------------------------------------------------------------------
928
929 // get the i-th bc value
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 // if not-a-knot, value is not used, so size is OK.
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; // In case of error, ensure that output pointer is 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 // check if boundary condition sizes match x and y sizes.
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 // find coefficients along y (fast axis)
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 // find coefficients along x (slow axis)
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 // for this slice in constant y, the target values are the
1028 // `nx` coefficients we just found. They are strided in
1029 // `coeffs` by `my`.
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 // the results in `buf` are contiguous in x, but we need to
1041 // copy them back into the coefficients array strided.
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 // for first index, it could be anywhere, so use binary search
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; // for indexing coeffs.
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 // index outside left boundary
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 // index outside right boundary
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 // get basis function values for x coordinate
1128 b3nonzeros(xval, i, spline->xknots, spline->xconsts, xb3vals);
1129
1130 // x value is in range (or extrapolating); loop over y values:
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 // index outside left boundary
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 // index outside right boundary
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 // get basis function values for y coordinate
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 // uspline1d
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 // values, first and second derivatives of b3_{i-3}, b3_{i-2}, b3_{i-1}
1212 // at knot i.
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 // Left boundary condition
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 // rows
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 // Right boundary condition
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); // equal to 1 / (step size)
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 // fill RHS
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 // translate x onto unit basis
1318 xval = (x.data[j*x.stride] - spline->x.min) * spline->didx;
1319 xfloor = floor(xval);
1320 i = (int)xfloor;
1321
1322 // index outside left boundary
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 // index outside right boundary
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 // if we get this far, we're evaluating the spline
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 }