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.analysis.BivariateFunction;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.hipparchus.random.RandomDataGenerator;
27 import org.hipparchus.util.FastMath;
28 import org.hipparchus.util.Precision;
29 import org.junit.jupiter.api.Test;
30
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32 import static org.junit.jupiter.api.Assertions.assertFalse;
33 import static org.junit.jupiter.api.Assertions.assertTrue;
34 import static org.junit.jupiter.api.Assertions.fail;
35
36
37
38
39 final class BicubicInterpolatingFunctionTest {
40
41
42
43 @Test
44 void testPreconditions() {
45 double[] xval = new double[] {3, 4, 5, 6.5};
46 double[] yval = new double[] {-4, -3, -1, 2.5};
47 double[][] zval = new double[xval.length][yval.length];
48
49 @SuppressWarnings("unused")
50 BivariateFunction bcf = new BicubicInterpolatingFunction(xval, yval, zval,
51 zval, zval, zval);
52
53 try {
54 bcf = new BicubicInterpolatingFunction(new double[0], yval,
55 zval, zval, zval, zval);
56 fail("an exception should have been thrown");
57 } catch (MathIllegalArgumentException e) {
58
59 }
60 try {
61 bcf = new BicubicInterpolatingFunction(xval, new double[0],
62 zval, zval, zval, zval);
63 fail("an exception should have been thrown");
64 } catch (MathIllegalArgumentException e) {
65
66 }
67 try {
68 bcf = new BicubicInterpolatingFunction(xval, yval,
69 new double[0][], zval, zval, zval);
70 fail("an exception should have been thrown");
71 } catch (MathIllegalArgumentException e) {
72
73 }
74 try {
75 bcf = new BicubicInterpolatingFunction(xval, yval,
76 new double[1][0], zval, zval, zval);
77 fail("an exception should have been thrown");
78 } catch (MathIllegalArgumentException e) {
79
80 }
81
82 double[] wxval = new double[] {3, 2, 5, 6.5};
83 try {
84 bcf = new BicubicInterpolatingFunction(wxval, yval, zval, zval, zval, zval);
85 fail("an exception should have been thrown");
86 } catch (MathIllegalArgumentException e) {
87
88 }
89 double[] wyval = new double[] {-4, -1, -1, 2.5};
90 try {
91 bcf = new BicubicInterpolatingFunction(xval, wyval, zval, zval, zval, zval);
92 fail("an exception should have been thrown");
93 } catch (MathIllegalArgumentException e) {
94
95 }
96 double[][] wzval = new double[xval.length][yval.length - 1];
97 try {
98 bcf = new BicubicInterpolatingFunction(xval, yval, wzval, zval, zval, zval);
99 fail("an exception should have been thrown");
100 } catch (MathIllegalArgumentException e) {
101
102 }
103 try {
104 bcf = new BicubicInterpolatingFunction(xval, yval, zval, wzval, zval, zval);
105 fail("an exception should have been thrown");
106 } catch (MathIllegalArgumentException e) {
107
108 }
109 try {
110 bcf = new BicubicInterpolatingFunction(xval, yval, zval, zval, wzval, zval);
111 fail("an exception should have been thrown");
112 } catch (MathIllegalArgumentException e) {
113
114 }
115 try {
116 bcf = new BicubicInterpolatingFunction(xval, yval, zval, zval, zval, wzval);
117 fail("an exception should have been thrown");
118 } catch (MathIllegalArgumentException e) {
119
120 }
121
122 wzval = new double[xval.length - 1][yval.length];
123 try {
124 bcf = new BicubicInterpolatingFunction(xval, yval, wzval, zval, zval, zval);
125 fail("an exception should have been thrown");
126 } catch (MathIllegalArgumentException e) {
127
128 }
129 try {
130 bcf = new BicubicInterpolatingFunction(xval, yval, zval, wzval, zval, zval);
131 fail("an exception should have been thrown");
132 } catch (MathIllegalArgumentException e) {
133
134 }
135 try {
136 bcf = new BicubicInterpolatingFunction(xval, yval, zval, zval, wzval, zval);
137 fail("an exception should have been thrown");
138 } catch (MathIllegalArgumentException e) {
139
140 }
141 try {
142 bcf = new BicubicInterpolatingFunction(xval, yval, zval, zval, zval, wzval);
143 fail("an exception should have been thrown");
144 } catch (MathIllegalArgumentException e) {
145
146 }
147 }
148
149 @Test
150 void testIsValidPoint() {
151 final double xMin = -12;
152 final double xMax = 34;
153 final double yMin = 5;
154 final double yMax = 67;
155 final double[] xval = new double[] { xMin, xMax };
156 final double[] yval = new double[] { yMin, yMax };
157 final double[][] f = new double[][] { { 1, 2 },
158 { 3, 4 } };
159 final double[][] dFdX = f;
160 final double[][] dFdY = f;
161 final double[][] dFdXdY = f;
162
163 final BicubicInterpolatingFunction bcf
164 = new BicubicInterpolatingFunction(xval, yval, f,
165 dFdX, dFdY, dFdXdY);
166
167 double x, y;
168
169 x = xMin;
170 y = yMin;
171 assertTrue(bcf.isValidPoint(x, y));
172
173 bcf.value(x, y);
174
175 x = xMax;
176 y = yMax;
177 assertTrue(bcf.isValidPoint(x, y));
178
179 bcf.value(x, y);
180
181 final double xRange = xMax - xMin;
182 final double yRange = yMax - yMin;
183 x = xMin + xRange / 3.4;
184 y = yMin + yRange / 1.2;
185 assertTrue(bcf.isValidPoint(x, y));
186
187 bcf.value(x, y);
188
189 final double small = 1e-8;
190 x = xMin - small;
191 y = yMax;
192 assertFalse(bcf.isValidPoint(x, y));
193
194 try {
195 bcf.value(x, y);
196 fail("MathIllegalArgumentException expected");
197 } catch (MathIllegalArgumentException expected) {}
198
199 x = xMin;
200 y = yMax + small;
201 assertFalse(bcf.isValidPoint(x, y));
202
203 try {
204 bcf.value(x, y);
205 fail("MathIllegalArgumentException expected");
206 } catch (MathIllegalArgumentException expected) {}
207 }
208
209
210
211
212
213
214 @Test
215 void testPlane() {
216 final int numberOfElements = 10;
217 final double minimumX = -10;
218 final double maximumX = 10;
219 final double minimumY = -10;
220 final double maximumY = 10;
221 final int numberOfSamples = 1000;
222
223 final double interpolationTolerance = 1e-15;
224 final double maxTolerance = 1e-14;
225
226
227 BivariateFunction f = (x, y) -> 2 * x - 3 * y + 5;
228 BivariateFunction dfdx = (x, y) -> 2;
229 BivariateFunction dfdy = (x, y) -> -3;
230 BivariateFunction d2fdxdy = (x, y) -> 0;
231
232 testInterpolation(minimumX,
233 maximumX,
234 minimumY,
235 maximumY,
236 numberOfElements,
237 numberOfSamples,
238 f,
239 dfdx,
240 dfdy,
241 d2fdxdy,
242 interpolationTolerance,
243 maxTolerance,
244 false);
245 }
246
247
248
249
250
251
252 @Test
253 void testParaboloid() {
254 final int numberOfElements = 10;
255 final double minimumX = -10;
256 final double maximumX = 10;
257 final double minimumY = -10;
258 final double maximumY = 10;
259 final int numberOfSamples = 1000;
260
261 final double interpolationTolerance = 2e-14;
262 final double maxTolerance = 1e-12;
263
264
265 BivariateFunction f = (x, y) -> 2 * x * x - 3 * y * y + 4 * x * y - 5;
266 BivariateFunction dfdx = (x, y) -> 4 * (x + y);
267 BivariateFunction dfdy = (x, y) -> 4 * x - 6 * y;
268 BivariateFunction d2fdxdy = (x, y) -> 4;
269
270 testInterpolation(minimumX,
271 maximumX,
272 minimumY,
273 maximumY,
274 numberOfElements,
275 numberOfSamples,
276 f,
277 dfdx,
278 dfdy,
279 d2fdxdy,
280 interpolationTolerance,
281 maxTolerance,
282 false);
283 }
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299 private void testInterpolation(double minimumX,
300 double maximumX,
301 double minimumY,
302 double maximumY,
303 int numberOfElements,
304 int numberOfSamples,
305 BivariateFunction f,
306 BivariateFunction dfdx,
307 BivariateFunction dfdy,
308 BivariateFunction d2fdxdy,
309 double meanTolerance,
310 double maxTolerance,
311 boolean print) {
312 double expected;
313 double actual;
314 double currentX;
315 double currentY;
316 final double deltaX = (maximumX - minimumX) / numberOfElements;
317 final double deltaY = (maximumY - minimumY) / numberOfElements;
318 final double[] xValues = new double[numberOfElements];
319 final double[] yValues = new double[numberOfElements];
320 final double[][] zValues = new double[numberOfElements][numberOfElements];
321 final double[][] dzdx = new double[numberOfElements][numberOfElements];
322 final double[][] dzdy = new double[numberOfElements][numberOfElements];
323 final double[][] d2zdxdy = new double[numberOfElements][numberOfElements];
324
325 for (int i = 0; i < numberOfElements; i++) {
326 xValues[i] = minimumX + deltaX * i;
327 final double x = xValues[i];
328 for (int j = 0; j < numberOfElements; j++) {
329 yValues[j] = minimumY + deltaY * j;
330 final double y = yValues[j];
331 zValues[i][j] = f.value(x, y);
332 dzdx[i][j] = dfdx.value(x, y);
333 dzdy[i][j] = dfdy.value(x, y);
334 d2zdxdy[i][j] = d2fdxdy.value(x, y);
335 }
336 }
337
338 final BivariateFunction interpolation
339 = new BicubicInterpolatingFunction(xValues,
340 yValues,
341 zValues,
342 dzdx,
343 dzdy,
344 d2zdxdy);
345
346 for (int i = 0; i < numberOfElements; i++) {
347 currentX = xValues[i];
348 for (int j = 0; j < numberOfElements; j++) {
349 currentY = yValues[j];
350 expected = f.value(currentX, currentY);
351 actual = interpolation.value(currentX, currentY);
352 assertTrue(Precision.equals(expected, actual),
353 "On data point: " + expected + " != " + actual);
354 }
355 }
356
357 final RandomDataGenerator randomDataGenerator = new RandomDataGenerator(100);
358
359 double sumError = 0;
360 for (int i = 0; i < numberOfSamples; i++) {
361 currentX = randomDataGenerator.nextUniform(xValues[0], xValues[xValues.length - 1]);
362 currentY = randomDataGenerator.nextUniform(yValues[0], yValues[yValues.length - 1]);
363 expected = f.value(currentX, currentY);
364
365 if (print) {
366 System.out.println(currentX + " " + currentY + " -> ");
367 }
368
369 actual = interpolation.value(currentX, currentY);
370 sumError += FastMath.abs(actual - expected);
371
372 if (print) {
373 System.out.println(actual + " (diff=" + (expected - actual) + ")");
374 }
375
376 assertEquals(expected, actual, maxTolerance);
377 }
378
379 final double meanError = sumError / numberOfSamples;
380 assertEquals(0, meanError, meanTolerance);
381 }
382 }