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.math3.analysis.interpolation; 18 19 import org.apache.commons.math3.exception.DimensionMismatchException; 20 import org.apache.commons.math3.exception.MathIllegalArgumentException; 21 import org.apache.commons.math3.analysis.BivariateFunction; 22 import org.apache.commons.math3.distribution.UniformRealDistribution; 23 import org.apache.commons.math3.random.RandomGenerator; 24 import org.apache.commons.math3.random.Well19937c; 25 import org.junit.Assert; 26 import org.junit.Test; 27 28 /** 29 * Test case for the bicubic interpolator. 30 * 31 * @deprecated as of 3.4 replaced by {@link org.apache.commons.math3.analysis.interpolation.PiecewiseBicubicSplineInterpolator} 32 */ 33 @Deprecated 34 public final class BicubicSplineInterpolatorTest { 35 /** 36 * Test preconditions. 37 */ 38 @Test testPreconditions()39 public void testPreconditions() { 40 double[] xval = new double[] {3, 4, 5, 6.5}; 41 double[] yval = new double[] {-4, -3, -1, 2.5}; 42 double[][] zval = new double[xval.length][yval.length]; 43 44 BivariateGridInterpolator interpolator = new BicubicSplineInterpolator(); 45 46 @SuppressWarnings("unused") 47 BivariateFunction p = interpolator.interpolate(xval, yval, zval); 48 49 double[] wxval = new double[] {3, 2, 5, 6.5}; 50 try { 51 p = interpolator.interpolate(wxval, yval, zval); 52 Assert.fail("an exception should have been thrown"); 53 } catch (MathIllegalArgumentException e) { 54 // Expected 55 } 56 57 double[] wyval = new double[] {-4, -3, -1, -1}; 58 try { 59 p = interpolator.interpolate(xval, wyval, zval); 60 Assert.fail("an exception should have been thrown"); 61 } catch (MathIllegalArgumentException e) { 62 // Expected 63 } 64 65 double[][] wzval = new double[xval.length][yval.length + 1]; 66 try { 67 p = interpolator.interpolate(xval, yval, wzval); 68 Assert.fail("an exception should have been thrown"); 69 } catch (DimensionMismatchException e) { 70 // Expected 71 } 72 wzval = new double[xval.length - 1][yval.length]; 73 try { 74 p = interpolator.interpolate(xval, yval, wzval); 75 Assert.fail("an exception should have been thrown"); 76 } catch (DimensionMismatchException e) { 77 // Expected 78 } 79 } 80 81 /** 82 * Interpolating a plane. 83 * <p> 84 * z = 2 x - 3 y + 5 85 */ 86 @Test testInterpolation1()87 public void testInterpolation1() { 88 final int sz = 21; 89 double[] xval = new double[sz]; 90 double[] yval = new double[sz]; 91 // Coordinate values 92 final double delta = 1d / (sz - 1); 93 for (int i = 0; i < sz; i++) { 94 xval[i] = -1 + 15 * i * delta; 95 yval[i] = -20 + 30 * i * delta; 96 } 97 98 // Function values 99 BivariateFunction f = new BivariateFunction() { 100 public double value(double x, double y) { 101 return 2 * x - 3 * y + 5; 102 } 103 }; 104 double[][] zval = new double[xval.length][yval.length]; 105 for (int i = 0; i < xval.length; i++) { 106 for (int j = 0; j < yval.length; j++) { 107 zval[i][j] = f.value(xval[i], yval[j]); 108 } 109 } 110 111 BivariateGridInterpolator interpolator = new BicubicSplineInterpolator(); 112 BivariateFunction p = interpolator.interpolate(xval, yval, zval); 113 double x, y; 114 115 final RandomGenerator rng = new Well19937c(1234567L); // "tol" depends on the seed. 116 final UniformRealDistribution distX 117 = new UniformRealDistribution(rng, xval[0], xval[xval.length - 1]); 118 final UniformRealDistribution distY 119 = new UniformRealDistribution(rng, yval[0], yval[yval.length - 1]); 120 121 final int numSamples = 50; 122 final double tol = 6; 123 for (int i = 0; i < numSamples; i++) { 124 x = distX.sample(); 125 for (int j = 0; j < numSamples; j++) { 126 y = distY.sample(); 127 // System.out.println(x + " " + y + " " + f.value(x, y) + " " + p.value(x, y)); 128 Assert.assertEquals(f.value(x, y), p.value(x, y), tol); 129 } 130 // System.out.println(); 131 } 132 } 133 134 /** 135 * Interpolating a paraboloid. 136 * <p> 137 * z = 2 x<sup>2</sup> - 3 y<sup>2</sup> + 4 x y - 5 138 */ 139 @Test testInterpolation2()140 public void testInterpolation2() { 141 final int sz = 21; 142 double[] xval = new double[sz]; 143 double[] yval = new double[sz]; 144 // Coordinate values 145 final double delta = 1d / (sz - 1); 146 for (int i = 0; i < sz; i++) { 147 xval[i] = -1 + 15 * i * delta; 148 yval[i] = -20 + 30 * i * delta; 149 } 150 151 // Function values 152 BivariateFunction f = new BivariateFunction() { 153 public double value(double x, double y) { 154 return 2 * x * x - 3 * y * y + 4 * x * y - 5; 155 } 156 }; 157 double[][] zval = new double[xval.length][yval.length]; 158 for (int i = 0; i < xval.length; i++) { 159 for (int j = 0; j < yval.length; j++) { 160 zval[i][j] = f.value(xval[i], yval[j]); 161 } 162 } 163 164 BivariateGridInterpolator interpolator = new BicubicSplineInterpolator(); 165 BivariateFunction p = interpolator.interpolate(xval, yval, zval); 166 double x, y; 167 168 final RandomGenerator rng = new Well19937c(1234567L); // "tol" depends on the seed. 169 final UniformRealDistribution distX 170 = new UniformRealDistribution(rng, xval[0], xval[xval.length - 1]); 171 final UniformRealDistribution distY 172 = new UniformRealDistribution(rng, yval[0], yval[yval.length - 1]); 173 174 final int numSamples = 50; 175 final double tol = 251; 176 for (int i = 0; i < numSamples; i++) { 177 x = distX.sample(); 178 for (int j = 0; j < numSamples; j++) { 179 y = distY.sample(); 180 // System.out.println(x + " " + y + " " + f.value(x, y) + " " + p.value(x, y)); 181 Assert.assertEquals(f.value(x, y), p.value(x, y), tol); 182 } 183 // System.out.println(); 184 } 185 } 186 } 187