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.junit.jupiter.api.Test;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  import static org.junit.jupiter.api.Assertions.fail;
31  
32  /**
33   * Test case for the bicubic interpolator.
34   */
35  final class BicubicInterpolatorTest {
36      /**
37       * Test preconditions.
38       */
39      @Test
40      void testPreconditions() {
41          double[] xval = new double[] {3, 4, 5, 6.5};
42          double[] yval = new double[] {-4, -3, -1, 2.5};
43          double[][] zval = new double[xval.length][yval.length];
44  
45          BivariateGridInterpolator interpolator = new BicubicInterpolator();
46  
47          @SuppressWarnings("unused")
48          BivariateFunction p = interpolator.interpolate(xval, yval, zval);
49  
50          double[] wxval = new double[] {3, 2, 5, 6.5};
51          try {
52              p = interpolator.interpolate(wxval, yval, zval);
53              fail("an exception should have been thrown");
54          } catch (MathIllegalArgumentException e) {
55              // Expected
56          }
57  
58          double[] wyval = new double[] {-4, -3, -1, -1};
59          try {
60              p = interpolator.interpolate(xval, wyval, zval);
61              fail("an exception should have been thrown");
62          } catch (MathIllegalArgumentException e) {
63              // Expected
64          }
65  
66          double[][] wzval = new double[xval.length][yval.length + 1];
67          try {
68              p = interpolator.interpolate(xval, yval, wzval);
69              fail("an exception should have been thrown");
70          } catch (MathIllegalArgumentException e) {
71              // Expected
72          }
73          wzval = new double[xval.length - 1][yval.length];
74          try {
75              p = interpolator.interpolate(xval, yval, wzval);
76              fail("an exception should have been thrown");
77          } catch (MathIllegalArgumentException e) {
78              // Expected
79          }
80      }
81  
82      /**
83       * Interpolating a plane.
84       * <p>
85       * z = 2 x - 3 y + 5
86       */
87      @Test
88      void testPlane() {
89          BivariateFunction f = new BivariateFunction() {
90                  @Override
91                  public double value(double x, double y) {
92                      return 2 * x - 3 * y + 5;
93                  }
94              };
95  
96          testInterpolation(3000,
97                            1e-13,
98                            f,
99                            false);
100     }
101 
102     /**
103      * Interpolating a paraboloid.
104      * <p>
105      * z = 2 x<sup>2</sup> - 3 y<sup>2</sup> + 4 x y - 5
106      */
107     @Test
108     void testParaboloid() {
109         BivariateFunction f = new BivariateFunction() {
110                 @Override
111                 public double value(double x, double y) {
112                     return 2 * x * x - 3 * y * y + 4 * x * y - 5;
113                 }
114             };
115 
116         testInterpolation(3000,
117                           1e-12,
118                           f,
119                           false);
120     }
121 
122     /**
123      * @param numSamples Number of test samples.
124      * @param tolerance Allowed tolerance on the interpolated value.
125      * @param f Test function.
126      * @param print Whether to print debugging output to the console.
127      */
128     private void testInterpolation(int numSamples,
129                                    double tolerance,
130                                    BivariateFunction f,
131                                    boolean print) {
132         final int sz = 21;
133         final double[] xval = new double[sz];
134         final double[] yval = new double[sz];
135         // Coordinate values
136         final double delta = 1d / (sz - 1);
137         for (int i = 0; i < sz; i++) {
138             xval[i] = -1 + 15 * i * delta;
139             yval[i] = -20 + 30 * i * delta;
140         }
141 
142         final double[][] zval = new double[xval.length][yval.length];
143         for (int i = 0; i < xval.length; i++) {
144             for (int j = 0; j < yval.length; j++) {
145                 zval[i][j] = f.value(xval[i], yval[j]);
146             }
147         }
148 
149         final BicubicInterpolator interpolator = new BicubicInterpolator();
150         final BicubicInterpolatingFunction p = interpolator.interpolate(xval, yval, zval);
151         double x, y;
152 
153         final RandomDataGenerator gen = new RandomDataGenerator(1234567L);
154         int count = 0;
155         while (true) {
156             x = gen.nextUniform(xval[0], xval[xval.length - 1]);
157             y = gen.nextUniform(yval[0], yval[yval.length - 1]);
158             if (!p.isValidPoint(x, y)) {
159                 if (print) {
160                     System.out.println("# " + x + " " + y);
161                 }
162                 continue;
163             }
164 
165             if (count++ > numSamples) {
166                 break;
167             }
168             final double expected = f.value(x, y);
169             final double actual = p.value(x, y);
170 
171             if (print) {
172                 System.out.println(x + " " + y + " " + expected + " " + actual);
173             }
174 
175             assertEquals(expected, actual, tolerance);
176         }
177     }
178 }