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.exception.NullArgumentException;
27  import org.hipparchus.random.RandomDataGenerator;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.fail;
32  
33  /**
34   * Test case for the piecewise bicubic interpolator.
35   */
36  final class PiecewiseBicubicSplineInterpolatorTest {
37      /**
38       * Test preconditions.
39       */
40      @Test
41      void testPreconditions() {
42          double[] xval = new double[] { 3, 4, 5, 6.5, 7.5 };
43          double[] yval = new double[] { -4, -3, -1, 2.5, 3.5 };
44          double[][] zval = new double[xval.length][yval.length];
45  
46          BivariateGridInterpolator interpolator = new PiecewiseBicubicSplineInterpolator();
47  
48          try {
49              interpolator.interpolate( null, yval, zval );
50              fail( "Failed to detect x null pointer" );
51          } catch ( NullArgumentException iae ) {
52              // Expected.
53          }
54  
55          try {
56              interpolator.interpolate( xval, null, zval );
57              fail( "Failed to detect y null pointer" );
58          } catch ( NullArgumentException iae ) {
59              // Expected.
60          }
61  
62          try {
63              interpolator.interpolate( xval, yval, null );
64              fail( "Failed to detect z null pointer" );
65          } catch ( NullArgumentException iae ) {
66              // Expected.
67          }
68  
69          try {
70              double[] xval1 = { 0.0, 1.0, 2.0, 3.0 };
71              interpolator.interpolate( xval1, yval, zval );
72              fail( "Failed to detect insufficient x data" );
73          } catch ( MathIllegalArgumentException iae ) {
74              // Expected.
75          }
76  
77          try  {
78              double[] yval1 = { 0.0, 1.0, 2.0, 3.0 };
79              interpolator.interpolate( xval, yval1, zval );
80              fail( "Failed to detect insufficient y data" );
81          } catch ( MathIllegalArgumentException iae ) {
82              // Expected.
83          }
84  
85          try {
86              double[][] zval1 = new double[4][4];
87              interpolator.interpolate( xval, yval, zval1 );
88              fail( "Failed to detect insufficient z data" );
89          } catch ( MathIllegalArgumentException iae ) {
90              // Expected.
91          }
92  
93          try {
94              double[] xval1 = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
95              interpolator.interpolate( xval1, yval, zval );
96              fail( "Failed to detect data set array with different sizes." );
97          } catch ( MathIllegalArgumentException iae ) {
98              // Expected.
99          }
100 
101         try {
102             double[] yval1 = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
103             interpolator.interpolate( xval, yval1, zval );
104             fail( "Failed to detect data set array with different sizes." );
105         } catch ( MathIllegalArgumentException iae ) {
106             // Expected.
107         }
108 
109         // X values not sorted.
110         try {
111             double[] xval1 = { 0.0, 1.0, 0.5, 7.0, 3.5 };
112             interpolator.interpolate( xval1, yval, zval );
113             fail( "Failed to detect unsorted x arguments." );
114         } catch ( MathIllegalArgumentException iae ) {
115             // Expected.
116         }
117 
118         // Y values not sorted.
119         try {
120             double[] yval1 = { 0.0, 1.0, 1.5, 0.0, 3.0 };
121             interpolator.interpolate( xval, yval1, zval );
122             fail( "Failed to detect unsorted y arguments." );
123         } catch ( MathIllegalArgumentException iae ) {
124             // Expected.
125         }
126     }
127 
128     /**
129      * Interpolating a plane.
130      * <p>
131      * z = 2 x - 3 y + 5
132      */
133     @Test
134     void testInterpolation1() {
135         final int sz = 21;
136         double[] xval = new double[sz];
137         double[] yval = new double[sz];
138         // Coordinate values
139         final double delta = 1d / (sz - 1);
140         for ( int i = 0; i < sz; i++ ){
141             xval[i] = -1 + 15 * i * delta;
142             yval[i] = -20 + 30 * i * delta;
143         }
144 
145         // Function values
146         BivariateFunction f = (x, y) -> 2 * x - 3 * y + 5;
147         double[][] zval = new double[xval.length][yval.length];
148         for ( int i = 0; i < xval.length; i++ ) {
149             for ( int j = 0; j < yval.length; j++ ) {
150                 zval[i][j] = f.value(xval[i], yval[j]);
151             }
152         }
153 
154         BivariateGridInterpolator interpolator = new PiecewiseBicubicSplineInterpolator();
155         BivariateFunction p = interpolator.interpolate(xval, yval, zval);
156         double x, y;
157 
158         final RandomDataGenerator gen = new RandomDataGenerator(1234567L);
159         final int numSamples = 50;
160         final double tol = 2e-14;
161         for ( int i = 0; i < numSamples; i++ ) {
162             x = gen.nextUniform(xval[0], xval[xval.length - 1]);
163             for ( int j = 0; j < numSamples; j++ ) {
164                 y = gen.nextUniform(yval[0], yval[yval.length - 1]);
165 //                 System.out.println(x + " " + y + " " + f.value(x, y) + " " + p.value(x, y));
166                 assertEquals(f.value(x, y),  p.value(x, y), tol);
167             }
168 //             System.out.println();
169         }
170     }
171 
172     /**
173      * Interpolating a paraboloid.
174      * <p>
175      * z = 2 x<sup>2</sup> - 3 y<sup>2</sup> + 4 x y - 5
176      */
177     @Test
178     void testInterpolation2() {
179         final int sz = 21;
180         double[] xval = new double[sz];
181         double[] yval = new double[sz];
182         // Coordinate values
183         final double delta = 1d / (sz - 1);
184         for ( int i = 0; i < sz; i++ ) {
185             xval[i] = -1 + 15 * i * delta;
186             yval[i] = -20 + 30 * i * delta;
187         }
188 
189         // Function values
190         BivariateFunction f = (x, y) -> 2 * x * x - 3 * y * y + 4 * x * y - 5;
191         double[][] zval = new double[xval.length][yval.length];
192         for ( int i = 0; i < xval.length; i++ ) {
193             for ( int j = 0; j < yval.length; j++ ) {
194                 zval[i][j] = f.value(xval[i], yval[j]);
195             }
196         }
197 
198         BivariateGridInterpolator interpolator = new PiecewiseBicubicSplineInterpolator();
199         BivariateFunction p = interpolator.interpolate(xval, yval, zval);
200         double x, y;
201 
202         final RandomDataGenerator gen = new RandomDataGenerator(1234567L);
203         final int numSamples = 50;
204         final double tol = 5e-13;
205         for ( int i = 0; i < numSamples; i++ ) {
206             x = gen.nextUniform(xval[0], xval[xval.length - 1]);
207             for ( int j = 0; j < numSamples; j++ ) {
208                 y = gen.nextUniform(yval[0], yval[yval.length - 1]);
209 //                 System.out.println(x + " " + y + " " + f.value(x, y) + " " + p.value(x, y));
210                 assertEquals(f.value(x, y),  p.value(x, y), tol);
211             }
212 //             System.out.println();
213         }
214     }
215 }