1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.analysis.interpolation;
23
24 import org.hipparchus.UnitTestUtils;
25 import org.hipparchus.analysis.UnivariateFunction;
26 import org.hipparchus.analysis.polynomials.PolynomialFunction;
27 import org.hipparchus.analysis.polynomials.PolynomialSplineFunction;
28 import org.hipparchus.util.FastMath;
29 import org.junit.Assert;
30 import org.junit.Test;
31
32
33
34
35
36 public class SplineInterpolatorTest extends UnivariateInterpolatorAbstractTest {
37
38 protected UnivariateInterpolator buildDoubleInterpolator() {
39 return new SplineInterpolator();
40 }
41
42 protected FieldUnivariateInterpolator buildFieldInterpolator() {
43 return new SplineInterpolator();
44 }
45
46 @Test
47 public void testInterpolateSin() {
48 double sineCoefficientTolerance = 1e-6;
49 double sineInterpolationTolerance = 0.0043;
50 double[] x =
51 {
52 0.0,
53 FastMath.PI / 6d,
54 FastMath.PI / 2d,
55 5d * FastMath.PI / 6d,
56 FastMath.PI,
57 7d * FastMath.PI / 6d,
58 3d * FastMath.PI / 2d,
59 11d * FastMath.PI / 6d,
60 2.d * FastMath.PI };
61 double[] y = { 0d, 0.5d, 1d, 0.5d, 0d, -0.5d, -1d, -0.5d, 0d };
62 UnivariateInterpolator i = buildDoubleInterpolator();
63 UnivariateFunction f = i.interpolate(x, y);
64 verifyInterpolation(f, x, y);
65 verifyConsistency((PolynomialSplineFunction) f, x);
66
67
68
69
70
71
72
73
74
75
76 PolynomialFunction[] polynomials = ((PolynomialSplineFunction) f).getPolynomials();
77 double[] target = {y[0], 1.002676d, 0d, -0.17415829d};
78 UnitTestUtils.assertEquals(polynomials[0].getCoefficients(), target, sineCoefficientTolerance);
79 target = new double[]{y[1], 8.594367e-01, -2.735672e-01, -0.08707914};
80 UnitTestUtils.assertEquals(polynomials[1].getCoefficients(), target, sineCoefficientTolerance);
81 target = new double[]{y[2], 1.471804e-17,-5.471344e-01, 0.08707914};
82 UnitTestUtils.assertEquals(polynomials[2].getCoefficients(), target, sineCoefficientTolerance);
83 target = new double[]{y[3], -8.594367e-01, -2.735672e-01, 0.17415829};
84 UnitTestUtils.assertEquals(polynomials[3].getCoefficients(), target, sineCoefficientTolerance);
85 target = new double[]{y[4], -1.002676, 6.548562e-17, 0.17415829};
86 UnitTestUtils.assertEquals(polynomials[4].getCoefficients(), target, sineCoefficientTolerance);
87 target = new double[]{y[5], -8.594367e-01, 2.735672e-01, 0.08707914};
88 UnitTestUtils.assertEquals(polynomials[5].getCoefficients(), target, sineCoefficientTolerance);
89 target = new double[]{y[6], 3.466465e-16, 5.471344e-01, -0.08707914};
90 UnitTestUtils.assertEquals(polynomials[6].getCoefficients(), target, sineCoefficientTolerance);
91 target = new double[]{y[7], 8.594367e-01, 2.735672e-01, -0.17415829};
92 UnitTestUtils.assertEquals(polynomials[7].getCoefficients(), target, sineCoefficientTolerance);
93
94
95 Assert.assertEquals(FastMath.sqrt(2d) / 2d,f.value(FastMath.PI/4d),sineInterpolationTolerance);
96 Assert.assertEquals(FastMath.sqrt(2d) / 2d,f.value(3d*FastMath.PI/4d),sineInterpolationTolerance);
97 }
98
99
100
101
102
103 protected void verifyConsistency(PolynomialSplineFunction f, double[] x)
104 {
105 PolynomialFunction[] polynomials = f.getPolynomials();
106 for (int i = 1; i < x.length - 2; i++) {
107
108 Assert.assertEquals(polynomials[i].value(x[i +1] - x[i]), polynomials[i + 1].value(0), 0.1);
109 Assert.assertEquals(polynomials[i].polynomialDerivative().value(x[i +1] - x[i]),
110 polynomials[i + 1].polynomialDerivative().value(0), 0.5);
111 Assert.assertEquals(polynomials[i].polynomialDerivative().polynomialDerivative().value(x[i +1] - x[i]),
112 polynomials[i + 1].polynomialDerivative().polynomialDerivative().value(0), 0.5);
113 }
114 }
115
116 }