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