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