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