-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspline.h
515 lines (422 loc) · 13 KB
/
spline.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
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
#ifndef SPLINE_H
#define SPLINE_H 1
/* spline.c
Cubic interpolating spline. */
/************************************************/
/* */
/* CMATH. Copyright (c) 1989 Design Software */
/* */
/************************************************/
/*-----------------------------------------------------------------*/
template <class T>
inline T linspace(T *Array, double d1, double d2, int n){
int i,j;
double Increment;
j = 0;
Increment = (d2-d1)/((double)(n-1));
for (i = 0; i < n-1; i++){
Array[i] = d1+ j*Increment;
j++;
}
Array[n-1] = d2;
return 0.0;
}
inline int spline (int n, int end1, int end2,
double slope1, double slope2,
double x[], double y[],
double b[], double c[], double d[],
int *iflag)
/* Purpose ...
-------
Evaluate the coefficients b[i], c[i], d[i], i = 0, 1, .. n-1 for
a cubic interpolating spline
S(xx) = Y[i] + b[i] * w + c[i] * w**2 + d[i] * w**3
where w = xx - x[i]
and x[i] <= xx <= x[i+1]
The n supplied data points are x[i], y[i], i = 0 ... n-1.
Input :
-------
n : The number of data points or knots (n >= 2)
end1,
end2 : = 1 to specify the slopes at the end points
= 0 to obtain the default conditions
slope1,
slope2 : the slopes at the end points x[0] and x[n-1]
respectively
x[] : the abscissas of the knots in strictly
increasing order
y[] : the ordinates of the knots
Output :
--------
b, c, d : arrays of spline coefficients as defined above
(See note 2 for a definition.)
iflag : status flag
= 0 normal return
= 1 less than two data points; cannot interpolate
= 2 x[] are not in ascending order
This C code written by ... Peter & Nigel,
---------------------- Design Software,
42 Gubberley St,
Kenmore, 4069,
Australia.
Version ... 1.1, 30 September 1987
------- 2.0, 6 April 1989 (start with zero subscript)
remove ndim from parameter list
2.1, 28 April 1989 (check on x[])
2.2, 10 Oct 1989 change number order of matrix
Notes ...
-----
(1) The accompanying function seval() may be used to evaluate the
spline while deriv will provide the first derivative.
(2) Using p to denote differentiation
y[i] = S(X[i])
b[i] = Sp(X[i])
c[i] = Spp(X[i])/2
d[i] = Sppp(X[i])/6 ( Derivative from the right )
(3) Since the zero elements of the arrays ARE NOW used here,
all arrays to be passed from the main program should be
dimensioned at least [n]. These routines will use elements
[0 .. n-1].
(4) Adapted from the text
Forsythe, G.E., Malcolm, M.A. and Moler, C.B. (1977)
"Computer Methods for Mathematical Computations"
Prentice Hall
(5) Note that although there are only n-1 polynomial segments,
n elements are requird in b, c, d. The elements b[n-1],
c[n-1] and d[n-1] are set to continue the last segment
past x[n-1].
*/
/*----------------------------------------------------------------*/
{ /* begin procedure spline() */
int nm1, ib, i;
double t;
nm1 = n - 1;
*iflag = 0;
if (n < 2) {
/* no possible interpolation */
*iflag = 1;
return 0;
}
for (i = 1; i < n; ++i) {
if (x[i] <= x[i-1]) {
*iflag = 2;
return 0;
}
}
if (n >= 3) {
/* ---- At least quadratic ---- */
/* ---- Set up the symmetric tri-diagonal system
b = diagonal
d = offdiagonal
c = right-hand-side */
d[0] = x[1] - x[0];
c[1] = (y[1] - y[0]) / d[0];
for (i = 1; i < nm1; ++i) {
d[i] = x[i+1] - x[i];
b[i] = 2.0 * (d[i-1] + d[i]);
c[i+1] = (y[i+1] - y[i]) / d[i];
c[i] = c[i+1] - c[i];
}
/* ---- Default End conditions
Third derivatives at x[0] and x[n-1] obtained
from divided differences */
b[0] = -d[0];
b[nm1] = -d[n-2];
c[0] = 0.0;
c[nm1] = 0.0;
if (n != 3) {
c[0] = c[2] / (x[3] - x[1]) - c[1] / (x[2] - x[0]);
c[nm1] = c[n-2] / (x[nm1] - x[n-3]) - c[n-3] / (x[n-2] - x[n-4]);
c[0] = c[0] * d[0] * d[0] / (x[3] - x[0]);
c[nm1] = -c[nm1] * d[n-2] * d[n-2] / (x[nm1] - x[n-4]);
}
/* Alternative end conditions -- known slopes */
if (end1 == 1) {
b[0] = 2.0 * (x[1] - x[0]);
c[0] = (y[1] - y[0]) / (x[1] - x[0]) - slope1;
}
if (end2 == 1) {
b[nm1] = 2.0 * (x[nm1] - x[n-2]);
c[nm1] = slope2 - (y[nm1] - y[n-2]) / (x[nm1] - x[n-2]);
}
/* Forward elimination */
for (i = 1; i < n; ++i) {
t = d[i-1] / b[i-1];
b[i] = b[i] - t * d[i-1];
c[i] = c[i] - t * c[i-1];
}
/* Back substitution */
c[nm1] = c[nm1] / b[nm1];
for (ib = 0; ib < nm1; ++ib) {
i = n - ib - 2;
c[i] = (c[i] - d[i] * c[i+1]) / b[i];
}
/* c[i] is now the sigma[i] of the text */
/* Compute the polynomial coefficients */
b[nm1] = (y[nm1] - y[n-2]) / d[n-2] + d[n-2] * (c[n-2] + 2.0 * c[nm1]);
for (i = 0; i < nm1; ++i) {
b[i] = (y[i+1] - y[i]) / d[i] - d[i] * (c[i+1] + 2.0 * c[i]);
d[i] = (c[i+1] - c[i]) / d[i];
c[i] = 3.0 * c[i];
}
c[nm1] = 3.0 * c[nm1];
d[nm1] = d[n-2];
} else { /* if n >= 3 */
/* linear segment only */
b[0] = (y[1] - y[0]) / (x[1] - x[0]);
c[0] = 0.0;
d[0] = 0.0;
b[1] = b[0];
c[1] = 0.0;
d[1] = 0.0;
}
return 0;
} /* end of spline() */
/*-------------------------------------------------------------------*/
inline double seval (int n, double u,
double x[], double y[],
double b[], double c[], double d[],
int *last)
/*Purpose ...
-------
Evaluate the cubic spline function
S(xx) = y[i] + b[i] * w + c[i] * w**2 + d[i] * w**3
where w = u - x[i]
and x[i] <= u <= x[i+1]
Note that Horner's rule is used.
If u < x[0] then i = 0 is used.
If u > x[n-1] then i = n-1 is used.
Input :
-------
n : The number of data points or knots (n >= 2)
u : the abscissa at which the spline is to be evaluated
Last : the segment that was last used to evaluate U
x[] : the abscissas of the knots in strictly increasing order
y[] : the ordinates of the knots
b, c, d : arrays of spline coefficients computed by spline().
Output :
--------
seval : the value of the spline function at u
Last : the segment in which u lies
Notes ...
-----
(1) If u is not in the same interval as the previous call then a
binary search is performed to determine the proper interval.
*/
/*-------------------------------------------------------------------*/
{ /* begin function seval() */
int i, j, k;
double w;
i = *last;
if (i >= n-1)
i = 0;
else if (i < 0)
i = 0;
if ((x[i] > u) || (x[i+1] < u)) {
/* ---- perform a binary search ---- */
i = 0;
j = n;
do {
k = (i + j) / 2; /* split the domain to search */
if (u < x[k]) /* move the upper bound */
j = k;
else /* move the lower bound */
i = k;
} while (j > i+1); /* there are no more segments to search */
}
*last = i;
/* ---- Evaluate the spline ---- */
w = u - x[i];
w = y[i] + w * (b[i] + w * (c[i] + w * d[i]));
return (w);
}
/*-------------------------------------------------------------------*/
inline double deriv (int n, double u,
double x[],
double b[], double c[], double d[],
int *last)
/* Purpose ...
-------
Evaluate the derivative of the cubic spline function
S(x) = B[i] + 2.0 * C[i] * w + 3.0 * D[i] * w**2
where w = u - X[i]
and X[i] <= u <= X[i+1]
Note that Horner's rule is used.
If U < X[0] then i = 0 is used.
If U > X[n-1] then i = n-1 is used.
Input :
-------
n : The number of data points or knots (n >= 2)
u : the abscissa at which the derivative is to be evaluated
last : the segment that was last used
x : the abscissas of the knots in strictly increasing order
b, c, d : arrays of spline coefficients computed by spline()
Output :
--------
deriv : the value of the derivative of the spline
function at u
last : the segment in which u lies
Notes ...
-----
(1) If u is not in the same interval as the previous call then a
binary search is performed to determine the proper interval.
*/
/*-------------------------------------------------------------------*/
{ /* begin function deriv() */
int i, j, k;
double w;
i = *last;
if (i >= n-1)
i = 0;
else if (i < 0)
i = 0;
if ((x[i] > u) || (x[i+1] < u)) {
/* ---- perform a binary search ---- */
i = 0;
j = n;
do {
k = (i + j) / 2; /* split the domain to search */
if (u < x[k]) /* move the upper bound */
j = k;
else /* move the lower bound */
i = k;
} while (j > i+1); /* there are no more segments to search */
}
*last = i;
/* ---- Evaluate the derivative ---- */
w = u - x[i];
w = b[i] + w * (2.0 * c[i] + w * 3.0 * d[i]);
return (w);
} /* end of deriv() */
/*-------------------------------------------------------------------*/
inline double sinteg (int n, double u,
double x[], double y[],
double b[], double c[], double d[],
int *last)
/*Purpose ...
-------
Integrate the cubic spline function
S(xx) = y[i] + b[i] * w + c[i] * w**2 + d[i] * w**3
where w = u - x[i]
and x[i] <= u <= x[i+1]
The integral is zero at u = x[0].
If u < x[0] then i = 0 segment is extrapolated.
If u > x[n-1] then i = n-1 segment is extrapolated.
Input :
-------
n : The number of data points or knots (n >= 2)
u : the abscissa at which the spline is to be evaluated
Last : the segment that was last used to evaluate U
x[] : the abscissas of the knots in strictly increasing order
y[] : the ordinates of the knots
b, c, d : arrays of spline coefficients computed by spline().
Output :
--------
sinteg : the value of the spline function at u
Last : the segment in which u lies
Notes ...
-----
(1) If u is not in the same interval as the previous call then a
binary search is performed to determine the proper interval.
*/
/*-------------------------------------------------------------------*/
{ /* begin function sinteg() */
int i, j, k;
double sum, dx;
i = *last;
if (i >= n-1)
i = 0;
else if (i < 0)
i = 0;
if ((x[i] > u) || (x[i+1] < u)) {
/* ---- perform a binary search ---- */
i = 0;
j = n;
do {
k = (i + j) / 2; /* split the domain to search */
if (u < x[k]) /* move the upper bound */
j = k;
else /* move the lower bound */
i = k;
} while (j > i+1); /* there are no more segments to search */
}
*last = i;
sum = 0.0;
/* ---- Evaluate the integral for segments x < u ---- */
for (j = 0; j < i; ++j) {
dx = x[j+1] - x[j];
sum += dx *
(y[j] + dx *
(0.5 * b[j] + dx *
(c[j] / 3.0 + dx * 0.25 * d[j])));
}
/* ---- Evaluate the integral fot this segment ---- */
dx = u - x[i];
sum += dx *
(y[i] + dx *
(0.5 * b[i] + dx *
(c[i] / 3.0 + dx * 0.25 * d[i])));
return (sum);
}
/*-------------------------------------------------------------------*/
inline double deriv2 (int n, double u,
double x[],
double b[], double c[], double d[],
int *last)
/* Purpose ...
-------
Evaluate the 2nd derivative of the cubic spline function
S(x) = 2.0 * C[i]+ 6.0 * D[i] * w
where w = u - X[i]
and X[i] <= u <= X[i+1]
Note that Horner's rule is used.
If U < X[0] then i = 0 is used.
If U > X[n-1] then i = n-1 is used.
Input :
-------
n : The number of data points or knots (n >= 2)
u : the abscissa at which the derivative is to be evaluated
last : the segment that was last used
x : the abscissas of the knots in strictly increasing order
b, c, d : arrays of spline coefficients computed by spline()
Output :
--------
deriv : the value of the derivative of the spline
function at u
last : the segment in which u lies
Notes ...
-----
(1) If u is not in the same interval as the previous call then a
binary search is performed to determine the proper interval.
*/
/*-------------------------------------------------------------------*/
{ /* begin function deriv2() */
//avoid compiler warning:
(void) b;
int i, j, k;
double w;
i = *last;
if (i >= n-1)
i = 0;
else if (i < 0)
i = 0;
if ((x[i] > u) || (x[i+1] < u)) {
/* ---- perform a binary search ---- */
i = 0;
j = n;
do {
k = (i + j) / 2; /* split the domain to search */
if (u < x[k]) /* move the upper bound */
j = k;
else /* move the lower bound */
i = k;
} while (j > i+1); /* there are no more segments to search */
}
*last = i;
/* ---- Evaluate the derivative ---- */
w = u - x[i];
w = 2.0 * c[i] + w * 6.0 * d[i];
return (w);
} /* end of deriv2() */
/*-------------------------------------------------------------------*/
#endif