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.CalculusFieldElement;
25 import org.hipparchus.UnitTestUtils;
26 import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
27 import org.hipparchus.analysis.UnivariateFunction;
28 import org.hipparchus.analysis.polynomials.FieldPolynomialFunction;
29 import org.hipparchus.analysis.polynomials.FieldPolynomialSplineFunction;
30 import org.hipparchus.analysis.polynomials.PolynomialFunction;
31 import org.hipparchus.analysis.polynomials.PolynomialSplineFunction;
32 import org.hipparchus.exception.MathIllegalArgumentException;
33 import org.hipparchus.exception.NullArgumentException;
34 import org.hipparchus.util.Binary64;
35 import org.junit.Assert;
36 import org.junit.Test;
37
38
39
40
41 public abstract class UnivariateInterpolatorAbstractTest {
42
43
44 protected double knotTolerance = 1E-12;
45
46
47 protected double coefficientTolerance = 1E-6;
48
49
50 protected double interpolationTolerance = 1E-12;
51
52 protected abstract UnivariateInterpolator buildDoubleInterpolator();
53
54 protected abstract FieldUnivariateInterpolator buildFieldInterpolator();
55
56 @Test
57 public void testInterpolateLinearDegenerateTwoSegment()
58 {
59 double[] x = { 0.0, 0.5, 1.0 };
60 double[] y = { 0.0, 0.5, 1.0 };
61 UnivariateInterpolator i = buildDoubleInterpolator();
62 UnivariateFunction f = i.interpolate(x, y);
63 verifyInterpolation(f, x, y);
64
65
66 PolynomialFunction[] polynomials = ((PolynomialSplineFunction) f).getPolynomials();
67 double[] target = {y[0], 1d};
68 UnitTestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
69 target = new double[]{y[1], 1d};
70 UnitTestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
71
72
73 Assert.assertEquals(0.0,f.value(0.0), interpolationTolerance);
74 Assert.assertEquals(0.4,f.value(0.4), interpolationTolerance);
75 Assert.assertEquals(1.0,f.value(1.0), interpolationTolerance);
76 }
77
78 @Test
79 public void testInterpolateLinearDegenerateTwoSegmentD64()
80 {
81 Binary64[] x = buildD64(0.0, 0.5, 1.0);
82 Binary64[] y = buildD64(0.0, 0.5, 1.0);
83 FieldUnivariateInterpolator i = buildFieldInterpolator();
84 CalculusFieldUnivariateFunction<Binary64> f = i.interpolate(x, y);
85 verifyInterpolation(f, x, y);
86
87
88 FieldPolynomialFunction<Binary64>[] polynomials = ((FieldPolynomialSplineFunction<Binary64>) f).getPolynomials();
89 checkCoeffs(coefficientTolerance, polynomials[0], y[0].getReal(), 1.0);
90 checkCoeffs(coefficientTolerance, polynomials[1], y[1].getReal(), 1.0);
91
92
93 Assert.assertEquals(0.0, f.value(new Binary64(0.0)).getReal(), interpolationTolerance);
94 Assert.assertEquals(0.4, f.value(new Binary64(0.4)).getReal(), interpolationTolerance);
95 Assert.assertEquals(1.0, f.value(new Binary64(1.0)).getReal(), interpolationTolerance);
96 }
97
98 @Test
99 public void testInterpolateLinearDegenerateThreeSegment()
100 {
101 double[] x = { 0.0, 0.5, 1.0, 1.5 };
102 double[] y = { 0.0, 0.5, 1.0, 1.5 };
103 UnivariateInterpolator i = buildDoubleInterpolator();
104 UnivariateFunction f = i.interpolate(x, y);
105 verifyInterpolation(f, x, y);
106
107
108 PolynomialFunction[] polynomials = ((PolynomialSplineFunction) f).getPolynomials();
109 double[] target = {y[0], 1d};
110 UnitTestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
111 target = new double[]{y[1], 1d};
112 UnitTestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
113 target = new double[]{y[2], 1d};
114 UnitTestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance);
115
116
117 Assert.assertEquals(0,f.value(0), interpolationTolerance);
118 Assert.assertEquals(1.4,f.value(1.4), interpolationTolerance);
119 Assert.assertEquals(1.5,f.value(1.5), interpolationTolerance);
120 }
121
122 @Test
123 public void testInterpolateLinearDegenerateThreeSegmentD64()
124 {
125 Binary64[] x = buildD64(0.0, 0.5, 1.0, 1.5);
126 Binary64[] y = buildD64(0.0, 0.5, 1.0, 1.5);
127 FieldUnivariateInterpolator i = buildFieldInterpolator();
128 CalculusFieldUnivariateFunction<Binary64> f = i.interpolate(x, y);
129 verifyInterpolation(f, x, y);
130
131
132 FieldPolynomialFunction<Binary64>[] polynomials = ((FieldPolynomialSplineFunction<Binary64>) f).getPolynomials();
133 checkCoeffs(coefficientTolerance, polynomials[0], y[0].getReal(), 1.0);
134 checkCoeffs(coefficientTolerance, polynomials[1], y[1].getReal(), 1.0);
135 checkCoeffs(coefficientTolerance, polynomials[2], y[2].getReal(), 1.0);
136
137
138 Assert.assertEquals(0, f.value(new Binary64(0)).getReal(), interpolationTolerance);
139 Assert.assertEquals(1.4, f.value(new Binary64(1.4)).getReal(), interpolationTolerance);
140 Assert.assertEquals(1.5, f.value(new Binary64(1.5)).getReal(), interpolationTolerance);
141 }
142
143 @Test
144 public void testIllegalArguments() {
145 UnivariateInterpolator i = buildDoubleInterpolator();
146 try
147 {
148 double[] yval = { 0.0, 1.0, 2.0, 3.0, 4.0 };
149 i.interpolate( null, yval );
150 Assert.fail( "Failed to detect x null pointer" );
151 }
152 catch ( NullArgumentException iae )
153 {
154
155 }
156
157 try
158 {
159 double[] xval = { 0.0, 1.0, 2.0, 3.0, 4.0 };
160 i.interpolate( xval, null );
161 Assert.fail( "Failed to detect y null pointer" );
162 }
163 catch ( NullArgumentException iae )
164 {
165
166 }
167
168
169 try {
170 double[] xval = { 0.0, 1.0 };
171 double[] yval = { 0.0, 1.0, 2.0 };
172 i.interpolate(xval, yval);
173 Assert.fail("Failed to detect data set array with different sizes.");
174 } catch (MathIllegalArgumentException iae) {
175
176 }
177
178 try {
179 double[] xval = { 0.0, 1.0, 0.5 };
180 double[] yval = { 0.0, 1.0, 2.0 };
181 i.interpolate(xval, yval);
182 Assert.fail("Failed to detect unsorted arguments.");
183 } catch (MathIllegalArgumentException iae) {
184
185 }
186
187 try {
188 double[] xval = { 0.0 };
189 double[] yval = { 0.0 };
190 i.interpolate(xval, yval);
191 Assert.fail("Failed to detect unsorted arguments.");
192 } catch (MathIllegalArgumentException iae) {
193
194 }
195 }
196
197 @Test
198 public void testIllegalArgumentsD64() {
199 FieldUnivariateInterpolator i = buildFieldInterpolator();
200 try
201 {
202 Binary64[] yval = buildD64(0.0, 1.0, 2.0, 3.0, 4.0);
203 i.interpolate( null, yval );
204 Assert.fail( "Failed to detect x null pointer" );
205 }
206 catch ( NullArgumentException iae )
207 {
208
209 }
210
211 try
212 {
213 Binary64[] xval = buildD64(0.0, 1.0, 2.0, 3.0, 4.0);
214 i.interpolate( xval, null );
215 Assert.fail( "Failed to detect y null pointer" );
216 }
217 catch ( NullArgumentException iae )
218 {
219
220 }
221
222
223 try {
224 Binary64[] xval = buildD64(0.0, 1.0);
225 Binary64[] yval = buildD64(0.0, 1.0, 2.0);
226 i.interpolate(xval, yval);
227 Assert.fail("Failed to detect data set array with different sizes.");
228 } catch (MathIllegalArgumentException iae) {
229
230 }
231
232 try {
233 Binary64[] xval = buildD64(0.0, 1.0, 0.5);
234 Binary64[] yval = buildD64(0.0, 1.0, 2.0);
235 i.interpolate(xval, yval);
236 Assert.fail("Failed to detect unsorted arguments.");
237 } catch (MathIllegalArgumentException iae) {
238
239 }
240
241 try {
242 Binary64[] xval = buildD64(0.0);
243 Binary64[] yval = buildD64(0.0);
244 i.interpolate(xval, yval);
245 Assert.fail("Failed to detect unsorted arguments.");
246 } catch (MathIllegalArgumentException iae) {
247
248 }
249 }
250
251
252
253
254 protected void verifyInterpolation(UnivariateFunction f, double[] x, double[] y) {
255 for (int i = 0; i < x.length; i++) {
256 Assert.assertEquals(y[i], f.value(x[i]), knotTolerance);
257 }
258 }
259
260
261
262
263 protected <T extends CalculusFieldElement<T>> void verifyInterpolation(CalculusFieldUnivariateFunction<T> f,
264 T[] x, T[] y) {
265 for (int i = 0; i < x.length; i++) {
266 Assert.assertEquals( y[i].getReal(), f.value(x[i]).getReal(), knotTolerance);
267 }
268 }
269
270 protected Binary64[] buildD64(double...c) {
271 Binary64[] array = new Binary64[c.length];
272 for (int i = 0; i < c.length; ++i) {
273 array[i] = new Binary64(c[i]);
274 }
275 return array;
276 }
277
278 protected <T extends CalculusFieldElement<T>> void checkCoeffs(final double tolerance, final FieldPolynomialFunction<T> p,
279 final double... ref) {
280 final T[] c = p.getCoefficients();
281 Assert.assertEquals(ref.length, c.length);
282 for (int i = 0; i < ref.length; ++i) {
283 Assert.assertEquals(ref[i], c[i].getReal(), tolerance);
284 }
285 }
286
287 }