View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
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   * Test case for the bicubic function.
38   */
39  final class BicubicInterpolatingFunctionTest {
40      /**
41       * Test preconditions.
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              // Expected
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              // Expected
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              // Expected
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              // Expected
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              // Expected
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              // Expected
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             // Expected
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             // Expected
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             // Expected
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             // Expected
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             // Expected
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             // Expected
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             // Expected
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             // Expected
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         // Ensure that no exception is thrown.
173         bcf.value(x, y);
174 
175         x = xMax;
176         y = yMax;
177         assertTrue(bcf.isValidPoint(x, y));
178         // Ensure that no exception is thrown.
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         // Ensure that no exception is thrown.
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         // Ensure that an exception would have been thrown.
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         // Ensure that an exception would have been thrown.
203         try {
204             bcf.value(x, y);
205             fail("MathIllegalArgumentException expected");
206         } catch (MathIllegalArgumentException expected) {}
207     }
208 
209     /**
210      * Interpolating a plane.
211      * <p>
212      * z = 2 x - 3 y + 5
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         // Function values
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      * Interpolating a paraboloid.
249      * <p>
250      * z = 2 x<sup>2</sup> - 3 y<sup>2</sup> + 4 x y - 5
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         // Function values
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      * @param minimumX Lower bound of interpolation range along the x-coordinate.
287      * @param maximumX Higher bound of interpolation range along the x-coordinate.
288      * @param minimumY Lower bound of interpolation range along the y-coordinate.
289      * @param maximumY Higher bound of interpolation range along the y-coordinate.
290      * @param numberOfElements Number of data points (along each dimension).
291      * @param numberOfSamples Number of test points.
292      * @param f Function to test.
293      * @param dfdx Partial derivative w.r.t. x of the function to test.
294      * @param dfdy Partial derivative w.r.t. y of the function to test.
295      * @param d2fdxdy Second partial cross-derivative of the function to test.
296      * @param meanTolerance Allowed average error (mean error on all interpolated values).
297      * @param maxTolerance Allowed error on each interpolated value.
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 }