ningshuxia
2022-12-01 ad494f13d2ddf31f142cf7fb908b3a6e90395a1a
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// <copyright file="CubicSpline.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2014 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
 
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace IStation.Numerics.Interpolation
{
    /// <summary>
    /// Cubic Spline Interpolation.
    /// </summary>
    /// <remarks>Supports both differentiation and integration.</remarks>
    public class CubicSpline : IInterpolation
    {
        readonly double[] _x;
        readonly double[] _c0;
        readonly double[] _c1;
        readonly double[] _c2;
        readonly double[] _c3;
        readonly Lazy<double[]> _indefiniteIntegral;
 
        /// <param name="x">sample points (N+1), sorted ascending</param>
        /// <param name="c0">Zero order spline coefficients (N)</param>
        /// <param name="c1">First order spline coefficients (N)</param>
        /// <param name="c2">second order spline coefficients (N)</param>
        /// <param name="c3">third order spline coefficients (N)</param>
        public CubicSpline(double[] x, double[] c0, double[] c1, double[] c2, double[] c3)
        {
            if (x.Length != c0.Length + 1 || x.Length != c1.Length + 1 || x.Length != c2.Length + 1 || x.Length != c3.Length + 1)
            {
                throw new ArgumentException("All vectors must have the same dimensionality.");
            }
 
            if (x.Length < 2)
            {
                throw new ArgumentException("The given array is too small. It must be at least 2 long.", nameof(x));
            }
 
            _x = x;
            _c0 = c0;
            _c1 = c1;
            _c2 = c2;
            _c3 = c3;
            _indefiniteIntegral = new Lazy<double[]>(ComputeIndefiniteIntegral);
        }
 
        /// <summary>
        /// Create a Hermite cubic spline interpolation from a set of (x,y) value pairs and their slope (first derivative), sorted ascendingly by x.
        /// </summary>
        public static CubicSpline InterpolateHermiteSorted(double[] x, double[] y, double[] firstDerivatives)
        {
            if (x.Length != y.Length || x.Length != firstDerivatives.Length)
            {
                throw new ArgumentException("All vectors must have the same dimensionality.");
            }
 
            if (x.Length < 2)
            {
                throw new ArgumentException("The given array is too small. It must be at least 2 long.", nameof(x));
            }
 
            var c0 = new double[x.Length - 1];
            var c1 = new double[x.Length - 1];
            var c2 = new double[x.Length - 1];
            var c3 = new double[x.Length - 1];
            for (int i = 0; i < c1.Length; i++)
            {
                double w = x[i + 1] - x[i];
                double w2 = w*w;
                c0[i] = y[i];
                c1[i] = firstDerivatives[i];
                c2[i] = (3*(y[i + 1] - y[i])/w - 2*firstDerivatives[i] - firstDerivatives[i + 1])/w;
                c3[i] = (2*(y[i] - y[i + 1])/w + firstDerivatives[i] + firstDerivatives[i + 1])/w2;
            }
 
            return new CubicSpline(x, c0, c1, c2, c3);
        }
 
        /// <summary>
        /// Create a Hermite cubic spline interpolation from an unsorted set of (x,y) value pairs and their slope (first derivative).
        /// WARNING: Works in-place and can thus causes the data array to be reordered.
        /// </summary>
        public static CubicSpline InterpolateHermiteInplace(double[] x, double[] y, double[] firstDerivatives)
        {
            if (x.Length != y.Length || x.Length != firstDerivatives.Length)
            {
                throw new ArgumentException("All vectors must have the same dimensionality.");
            }
 
            if (x.Length < 2)
            {
                throw new ArgumentException("The given array is too small. It must be at least 2 long.", nameof(x));
            }
 
            Sorting.Sort(x, y, firstDerivatives);
            return InterpolateHermiteSorted(x, y, firstDerivatives);
        }
 
        /// <summary>
        /// Create a Hermite cubic spline interpolation from an unsorted set of (x,y) value pairs and their slope (first derivative).
        /// </summary>
        public static CubicSpline InterpolateHermite(IEnumerable<double> x, IEnumerable<double> y, IEnumerable<double> firstDerivatives)
        {
            // note: we must make a copy, even if the input was arrays already
            return InterpolateHermiteInplace(x.ToArray(), y.ToArray(), firstDerivatives.ToArray());
        }
 
        /// <summary>
        /// Create an Akima cubic spline interpolation from a set of (x,y) value pairs, sorted ascendingly by x.
        /// Akima splines are robust to outliers.
        /// </summary>
        public static CubicSpline InterpolateAkimaSorted(double[] x, double[] y)
        {
            if (x.Length != y.Length)
            {
                throw new ArgumentException("All vectors must have the same dimensionality.");
            }
 
            if (x.Length < 5)
            {
                throw new ArgumentException("The given array is too small. It must be at least 5 long.", nameof(x));
            }
 
            /* Prepare divided differences (diff) and weights (w) */
 
            var diff = new double[x.Length - 1];
            var weights = new double[x.Length - 1];
 
            for (int i = 0; i < diff.Length; i++)
            {
                diff[i] = (y[i + 1] - y[i])/(x[i + 1] - x[i]);
            }
 
            for (int i = 1; i < weights.Length; i++)
            {
                weights[i] = Math.Abs(diff[i] - diff[i - 1]);
            }
 
            /* Prepare Hermite interpolation scheme */
 
            var dd = new double[x.Length];
 
            for (int i = 2; i < dd.Length - 2; i++)
            {
                dd[i] = weights[i - 1].AlmostEqual(0.0) && weights[i + 1].AlmostEqual(0.0)
                    ? (((x[i + 1] - x[i])*diff[i - 1]) + ((x[i] - x[i - 1])*diff[i]))/(x[i + 1] - x[i - 1])
                    : ((weights[i + 1]*diff[i - 1]) + (weights[i - 1]*diff[i]))/(weights[i + 1] + weights[i - 1]);
            }
 
            dd[0] = DifferentiateThreePoint(x, y, 0, 0, 1, 2);
            dd[1] = DifferentiateThreePoint(x, y, 1, 0, 1, 2);
            dd[x.Length - 2] = DifferentiateThreePoint(x, y, x.Length - 2, x.Length - 3, x.Length - 2, x.Length - 1);
            dd[x.Length - 1] = DifferentiateThreePoint(x, y, x.Length - 1, x.Length - 3, x.Length - 2, x.Length - 1);
 
            /* Build Akima spline using Hermite interpolation scheme */
 
            return InterpolateHermiteSorted(x, y, dd);
        }
 
        /// <summary>
        /// Create an Akima cubic spline interpolation from an unsorted set of (x,y) value pairs.
        /// Akima splines are robust to outliers.
        /// WARNING: Works in-place and can thus causes the data array to be reordered.
        /// </summary>
        public static CubicSpline InterpolateAkimaInplace(double[] x, double[] y)
        {
            if (x.Length != y.Length)
            {
                throw new ArgumentException("All vectors must have the same dimensionality.");
            }
 
            Sorting.Sort(x, y);
            return InterpolateAkimaSorted(x, y);
        }
 
        /// <summary>
        /// Create an Akima cubic spline interpolation from an unsorted set of (x,y) value pairs.
        /// Akima splines are robust to outliers.
        /// </summary>
        public static CubicSpline InterpolateAkima(IEnumerable<double> x, IEnumerable<double> y)
        {
            // note: we must make a copy, even if the input was arrays already
            return InterpolateAkimaInplace(x.ToArray(), y.ToArray());
        }
 
        /// <summary>
        /// Create a piecewise cubic Hermite interpolating polynomial from an unsorted set of (x,y) value pairs.
        /// Monotone-preserving interpolation with continuous first derivative.
        /// </summary>
        public static CubicSpline InterpolatePchipSorted(double[] x, double[] y)
        {
            // Implementation based on "Numerical Computing with Matlab" (Moler, 2004).
 
            if (x.Length != y.Length)
            {
                throw new ArgumentException("All vectors must have the same dimensionality.");
            }
 
            if (x.Length < 3)
            {
                throw new ArgumentException("The given array is too small. It must be at least 3 long.", nameof(x));
            }
 
            var m = new double[x.Length - 1];
            
            for (int i = 0; i < m.Length; i++)
            {
                m[i] = (y[i + 1] - y[i])/(x[i + 1] - x[i]);
            }
 
            var dd = new double[x.Length];
            var hPrev = x[1] - x[0];
            // This check is quite costly as it usually involves a Math.Pow().
            var mPrevIs0 = m[0].AlmostEqual(0.0);
 
            for (var i = 1; i < x.Length - 1; ++i)
            {
                var h = x[i + 1] - x[i];
                var mIs0 = m[i].AlmostEqual(0.0);
 
                if (mIs0 || mPrevIs0 || Math.Sign(m[i]) != Math.Sign(m[i - 1]))
                {
                    dd[i] = 0;
                }
                else
                {
                    // Weighted harmonic mean of each slope.
                    var w1 = 2 * h + hPrev;
                    var w2 = h + 2 * hPrev;
                    dd[i] = (w1 + w2) / (w1 / m[i - 1] + w2 / m[i]);
                }
 
                hPrev = h;
                mPrevIs0 = mIs0;
            }
            
            // Special case end-points.
            dd[0] = PchipEndPoints(x[1] - x[0], x[2] - x[1], m[0], m[1]);
            dd[dd.Length - 1] = PchipEndPoints(
                x[x.Length - 1] - x[x.Length - 2], x[x.Length - 2] - x[x.Length - 3],
                m[m.Length - 1], m[m.Length - 2]);
 
            return InterpolateHermiteSorted(x, y, dd);
        }
 
        static double PchipEndPoints(double h0, double h1, double m0, double m1)
        {
            // One-sided, shape-preserving, three-point estimate for the derivative.
            var d = ((2 * h0 + h1) * m0 - h0 * m1) / (h0 + h1);
 
            if (Math.Sign(d) != Math.Sign(m0))
            {
                return 0.0;
            }
 
            if (Math.Sign(m0) != Math.Sign(m1) && (Math.Abs(d) > 3 * Math.Abs(m0)))
            {
                return 3 * m0;
            }
 
            return d;
        }
 
        /// <summary>
        /// Create a piecewise cubic Hermite interpolating polynomial from an unsorted set of (x,y) value pairs.
        /// Monotone-preserving interpolation with continuous first derivative.
        /// WARNING: Works in-place and can thus causes the data array to be reordered.
        /// </summary>
        public static CubicSpline InterpolatePchipInplace(double[] x, double[] y)
        {
            if (x.Length != y.Length)
            {
                throw new ArgumentException("All vectors must have the same dimensionality.");
            }
 
            Sorting.Sort(x, y);
            return InterpolatePchipSorted(x, y);
        }
 
        /// <summary>
        /// Create a piecewise cubic Hermite interpolating polynomial from an unsorted set of (x,y) value pairs.
        /// Monotone-preserving interpolation with continuous first derivative.
        /// </summary>
        public static CubicSpline InterpolatePchip(IEnumerable<double> x, IEnumerable<double> y)
        {
            // note: we must make a copy, even if the input was arrays already
            return InterpolatePchipInplace(x.ToArray(), y.ToArray());
        }
 
        /// <summary>
        /// Create a cubic spline interpolation from a set of (x,y) value pairs, sorted ascendingly by x,
        /// and custom boundary/termination conditions.
        /// </summary>
        public static CubicSpline InterpolateBoundariesSorted(double[] x, double[] y,
            SplineBoundaryCondition leftBoundaryCondition, double leftBoundary,
            SplineBoundaryCondition rightBoundaryCondition, double rightBoundary)
        {
            if (x.Length != y.Length)
            {
                throw new ArgumentException("All vectors must have the same dimensionality.");
            }
 
            if (x.Length < 2)
            {
                throw new ArgumentException("The given array is too small. It must be at least 2 long.", nameof(x));
            }
 
            int n = x.Length;
 
            // normalize special cases
            if ((n == 2)
                && (leftBoundaryCondition == SplineBoundaryCondition.ParabolicallyTerminated)
                && (rightBoundaryCondition == SplineBoundaryCondition.ParabolicallyTerminated))
            {
                leftBoundaryCondition = SplineBoundaryCondition.SecondDerivative;
                leftBoundary = 0d;
                rightBoundaryCondition = SplineBoundaryCondition.SecondDerivative;
                rightBoundary = 0d;
            }
 
            if (leftBoundaryCondition == SplineBoundaryCondition.Natural)
            {
                leftBoundaryCondition = SplineBoundaryCondition.SecondDerivative;
                leftBoundary = 0d;
            }
 
            if (rightBoundaryCondition == SplineBoundaryCondition.Natural)
            {
                rightBoundaryCondition = SplineBoundaryCondition.SecondDerivative;
                rightBoundary = 0d;
            }
 
            var a1 = new double[n];
            var a2 = new double[n];
            var a3 = new double[n];
            var b = new double[n];
 
            // Left Boundary
            switch (leftBoundaryCondition)
            {
                case SplineBoundaryCondition.ParabolicallyTerminated:
                    a1[0] = 0;
                    a2[0] = 1;
                    a3[0] = 1;
                    b[0] = 2*(y[1] - y[0])/(x[1] - x[0]);
                    break;
                case SplineBoundaryCondition.FirstDerivative:
                    a1[0] = 0;
                    a2[0] = 1;
                    a3[0] = 0;
                    b[0] = leftBoundary;
                    break;
                case SplineBoundaryCondition.SecondDerivative:
                    a1[0] = 0;
                    a2[0] = 2;
                    a3[0] = 1;
                    b[0] = (3*((y[1] - y[0])/(x[1] - x[0]))) - (0.5*leftBoundary*(x[1] - x[0]));
                    break;
                default:
                    throw new NotSupportedException("Invalid Left Boundary Condition.");
            }
 
            // Central Conditions
            for (int i = 1; i < x.Length - 1; i++)
            {
                a1[i] = x[i + 1] - x[i];
                a2[i] = 2*(x[i + 1] - x[i - 1]);
                a3[i] = x[i] - x[i - 1];
                b[i] = (3*(y[i] - y[i - 1])/(x[i] - x[i - 1])*(x[i + 1] - x[i])) + (3*(y[i + 1] - y[i])/(x[i + 1] - x[i])*(x[i] - x[i - 1]));
            }
 
            // Right Boundary
            switch (rightBoundaryCondition)
            {
                case SplineBoundaryCondition.ParabolicallyTerminated:
                    a1[n - 1] = 1;
                    a2[n - 1] = 1;
                    a3[n - 1] = 0;
                    b[n - 1] = 2*(y[n - 1] - y[n - 2])/(x[n - 1] - x[n - 2]);
                    break;
                case SplineBoundaryCondition.FirstDerivative:
                    a1[n - 1] = 0;
                    a2[n - 1] = 1;
                    a3[n - 1] = 0;
                    b[n - 1] = rightBoundary;
                    break;
                case SplineBoundaryCondition.SecondDerivative:
                    a1[n - 1] = 1;
                    a2[n - 1] = 2;
                    a3[n - 1] = 0;
                    b[n - 1] = (3*(y[n - 1] - y[n - 2])/(x[n - 1] - x[n - 2])) + (0.5*rightBoundary*(x[n - 1] - x[n - 2]));
                    break;
                default:
                    throw new NotSupportedException("Invalid Right Boundary Condition.");
            }
 
            // Build Spline
            double[] dd = SolveTridiagonal(a1, a2, a3, b);
            return InterpolateHermiteSorted(x, y, dd);
        }
 
        /// <summary>
        /// Create a cubic spline interpolation from an unsorted set of (x,y) value pairs and custom boundary/termination conditions.
        /// WARNING: Works in-place and can thus causes the data array to be reordered.
        /// </summary>
        public static CubicSpline InterpolateBoundariesInplace(double[] x, double[] y,
            SplineBoundaryCondition leftBoundaryCondition, double leftBoundary,
            SplineBoundaryCondition rightBoundaryCondition, double rightBoundary)
        {
            if (x.Length != y.Length)
            {
                throw new ArgumentException("All vectors must have the same dimensionality.");
            }
 
            Sorting.Sort(x, y);
            return InterpolateBoundariesSorted(x, y, leftBoundaryCondition, leftBoundary, rightBoundaryCondition, rightBoundary);
        }
 
        /// <summary>
        /// Create a cubic spline interpolation from an unsorted set of (x,y) value pairs and custom boundary/termination conditions.
        /// </summary>
        public static CubicSpline InterpolateBoundaries(IEnumerable<double> x, IEnumerable<double> y,
            SplineBoundaryCondition leftBoundaryCondition, double leftBoundary,
            SplineBoundaryCondition rightBoundaryCondition, double rightBoundary)
        {
            // note: we must make a copy, even if the input was arrays already
            return InterpolateBoundariesInplace(x.ToArray(), y.ToArray(), leftBoundaryCondition, leftBoundary, rightBoundaryCondition, rightBoundary);
        }
 
        /// <summary>
        /// Create a natural cubic spline interpolation from a set of (x,y) value pairs
        /// and zero second derivatives at the two boundaries, sorted ascendingly by x.
        /// </summary>
        public static CubicSpline InterpolateNaturalSorted(double[] x, double[] y)
        {
            return InterpolateBoundariesSorted(x, y, SplineBoundaryCondition.SecondDerivative, 0.0, SplineBoundaryCondition.SecondDerivative, 0.0);
        }
 
        /// <summary>
        /// Create a natural cubic spline interpolation from an unsorted set of (x,y) value pairs
        /// and zero second derivatives at the two boundaries.
        /// WARNING: Works in-place and can thus causes the data array to be reordered.
        /// </summary>
        public static CubicSpline InterpolateNaturalInplace(double[] x, double[] y)
        {
            return InterpolateBoundariesInplace(x, y, SplineBoundaryCondition.SecondDerivative, 0.0, SplineBoundaryCondition.SecondDerivative, 0.0);
        }
 
        /// <summary>
        /// Create a natural cubic spline interpolation from an unsorted set of (x,y) value pairs
        /// and zero second derivatives at the two boundaries.
        /// </summary>
        public static CubicSpline InterpolateNatural(IEnumerable<double> x, IEnumerable<double> y)
        {
            return InterpolateBoundaries(x, y, SplineBoundaryCondition.SecondDerivative, 0.0, SplineBoundaryCondition.SecondDerivative, 0.0);
        }
 
        /// <summary>
        /// Three-Point Differentiation Helper.
        /// </summary>
        /// <param name="xx">Sample Points t.</param>
        /// <param name="yy">Sample Values x(t).</param>
        /// <param name="indexT">Index of the point of the differentiation.</param>
        /// <param name="index0">Index of the first sample.</param>
        /// <param name="index1">Index of the second sample.</param>
        /// <param name="index2">Index of the third sample.</param>
        /// <returns>The derivative approximation.</returns>
        static double DifferentiateThreePoint(double[] xx, double[] yy, int indexT, int index0, int index1, int index2)
        {
            double x0 = yy[index0];
            double x1 = yy[index1];
            double x2 = yy[index2];
 
            double t = xx[indexT] - xx[index0];
            double t1 = xx[index1] - xx[index0];
            double t2 = xx[index2] - xx[index0];
 
            double a = (x2 - x0 - (t2/t1*(x1 - x0)))/(t2*(t2 - t1));
            double b = (x1 - x0 - a*t1*t1)/t1;
            return (2*a*t) + b;
        }
 
        /// <summary>
        /// Tridiagonal Solve Helper.
        /// </summary>
        /// <param name="a">The a-vector[n].</param>
        /// <param name="b">The b-vector[n], will be modified by this function.</param>
        /// <param name="c">The c-vector[n].</param>
        /// <param name="d">The d-vector[n], will be modified by this function.</param>
        /// <returns>The x-vector[n]</returns>
        static double[] SolveTridiagonal(double[] a, double[] b, double[] c, double[] d)
        {
            for (int k = 1; k < a.Length; k++)
            {
                double t = a[k]/b[k - 1];
                b[k] = b[k] - (t*c[k - 1]);
                d[k] = d[k] - (t*d[k - 1]);
            }
 
            var x = new double[a.Length];
            x[x.Length - 1] = d[d.Length - 1]/b[b.Length - 1];
            for (int k = x.Length - 2; k >= 0; k--)
            {
                x[k] = (d[k] - (c[k]*x[k + 1]))/b[k];
            }
 
            return x;
        }
 
        /// <summary>
        /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
        /// </summary>
        bool IInterpolation.SupportsDifferentiation => true;
 
        /// <summary>
        /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
        /// </summary>
        bool IInterpolation.SupportsIntegration => true;
 
        /// <summary>
        /// Interpolate at point t.
        /// </summary>
        /// <param name="t">Point t to interpolate at.</param>
        /// <returns>Interpolated value x(t).</returns>
        public double Interpolate(double t)
        {
            int k = LeftSegmentIndex(t);
            var x = t - _x[k];
            return _c0[k] + x*(_c1[k] + x*(_c2[k] + x*_c3[k]));
        }
 
        /// <summary>
        /// Differentiate at point t.
        /// </summary>
        /// <param name="t">Point t to interpolate at.</param>
        /// <returns>Interpolated first derivative at point t.</returns>
        public double Differentiate(double t)
        {
            int k = LeftSegmentIndex(t);
            var x = t - _x[k];
            return _c1[k] + x*(2*_c2[k] + x*3*_c3[k]);
        }
 
        /// <summary>
        /// Differentiate twice at point t.
        /// </summary>
        /// <param name="t">Point t to interpolate at.</param>
        /// <returns>Interpolated second derivative at point t.</returns>
        public double Differentiate2(double t)
        {
            int k = LeftSegmentIndex(t);
            var x = t - _x[k];
            return 2*_c2[k] + x*6*_c3[k];
        }
 
        /// <summary>
        /// Indefinite integral at point t.
        /// </summary>
        /// <param name="t">Point t to integrate at.</param>
        public double Integrate(double t)
        {
            int k = LeftSegmentIndex(t);
            var x = t - _x[k];
            return _indefiniteIntegral.Value[k] + x*(_c0[k] + x*(_c1[k]/2 + x*(_c2[k]/3 + x*_c3[k]/4)));
        }
 
        /// <summary>
        /// Definite integral between points a and b.
        /// </summary>
        /// <param name="a">Left bound of the integration interval [a,b].</param>
        /// <param name="b">Right bound of the integration interval [a,b].</param>
        public double Integrate(double a, double b)
        {
            return Integrate(b) - Integrate(a);
        }
 
        double[] ComputeIndefiniteIntegral()
        {
            var integral = new double[_c1.Length];
            for (int i = 0; i < integral.Length - 1; i++)
            {
                double w = _x[i + 1] - _x[i];
                integral[i + 1] = integral[i] + w*(_c0[i] + w*(_c1[i]/2 + w*(_c2[i]/3 + w*_c3[i]/4)));
            }
 
            return integral;
        }
 
        /// <summary>
        /// Find the index of the greatest sample point smaller than t,
        /// or the left index of the closest segment for extrapolation.
        /// </summary>
        int LeftSegmentIndex(double t)
        {
            int index = Array.BinarySearch(_x, t);
            if (index < 0)
            {
                index = ~index - 1;
            }
 
            return Math.Min(Math.Max(index, 0), _x.Length - 2);
        }
    }
}