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.util.FastMath;
22 import org.apache.commons.math3.analysis.TrivariateFunction;
23 import org.junit.Assert;
24 import org.junit.Test;
25 import org.junit.Ignore;
26 
27 /**
28  * Test case for the tricubic interpolator.
29  *
30  * @deprecated To be removed in 4.0 (see MATH-1166).
31  */
32 @Deprecated
33 public final class TricubicSplineInterpolatorTest {
34     /**
35      * Test preconditions.
36      */
37     @Test
testPreconditions()38     public void testPreconditions() {
39         double[] xval = new double[] {3, 4, 5, 6.5};
40         double[] yval = new double[] {-4, -3, -1, 2.5};
41         double[] zval = new double[] {-12, -8, -5.5, -3, 0, 2.5};
42         double[][][] fval = new double[xval.length][yval.length][zval.length];
43 
44         TrivariateGridInterpolator interpolator = new TricubicSplineInterpolator();
45 
46         @SuppressWarnings("unused")
47         TrivariateFunction p = interpolator.interpolate(xval, yval, zval, fval);
48 
49         double[] wxval = new double[] {3, 2, 5, 6.5};
50         try {
51             p = interpolator.interpolate(wxval, yval, zval, fval);
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, fval);
60             Assert.fail("an exception should have been thrown");
61         } catch (MathIllegalArgumentException e) {
62             // Expected
63         }
64 
65         double[] wzval = new double[] {-12, -8, -5.5, -3, -4, 2.5};
66         try {
67             p = interpolator.interpolate(xval, yval, wzval, fval);
68             Assert.fail("an exception should have been thrown");
69         } catch (MathIllegalArgumentException e) {
70             // Expected
71         }
72 
73         double[][][] wfval = new double[xval.length][yval.length + 1][zval.length];
74         try {
75             p = interpolator.interpolate(xval, yval, zval, wfval);
76             Assert.fail("an exception should have been thrown");
77         } catch (DimensionMismatchException e) {
78             // Expected
79         }
80         wfval = new double[xval.length - 1][yval.length][zval.length];
81         try {
82             p = interpolator.interpolate(xval, yval, zval, wfval);
83             Assert.fail("an exception should have been thrown");
84         } catch (DimensionMismatchException e) {
85             // Expected
86         }
87         wfval = new double[xval.length][yval.length][zval.length - 1];
88         try {
89             p = interpolator.interpolate(xval, yval, zval, wfval);
90             Assert.fail("an exception should have been thrown");
91         } catch (DimensionMismatchException e) {
92             // Expected
93         }
94     }
95 
96     /**
97      * Test of interpolator for a plane.
98      * <p>
99      * f(x, y, z) = 2 x - 3 y - z + 5
100      */
101     @Ignore@Test
testPlane()102     public void testPlane() {
103         TrivariateFunction f = new TrivariateFunction() {
104                 public double value(double x, double y, double z) {
105                     return 2 * x - 3 * y - z + 5;
106                 }
107             };
108 
109         TrivariateGridInterpolator interpolator = new TricubicSplineInterpolator();
110 
111         double[] xval = new double[] {3, 4, 5, 6.5};
112         double[] yval = new double[] {-4, -3, -1, 2, 2.5};
113         double[] zval = new double[] {-12, -8, -5.5, -3, 0, 2.5};
114         double[][][] fval = new double[xval.length][yval.length][zval.length];
115         for (int i = 0; i < xval.length; i++) {
116             for (int j = 0; j < yval.length; j++) {
117                 for (int k = 0; k < zval.length; k++) {
118                     fval[i][j][k] = f.value(xval[i], yval[j], zval[k]);
119                 }
120             }
121         }
122 
123         TrivariateFunction p = interpolator.interpolate(xval, yval, zval, fval);
124         double x, y, z;
125         double expected, result;
126 
127         x = 4;
128         y = -3;
129         z = 0;
130         expected = f.value(x, y, z);
131         result = p.value(x, y, z);
132         Assert.assertEquals("On sample point", expected, result, 1e-15);
133 
134         x = 4.5;
135         y = -1.5;
136         z = -4.25;
137         expected = f.value(x, y, z);
138         result = p.value(x, y, z);
139         Assert.assertEquals("half-way between sample points (middle of the patch)", expected, result, 0.3);
140 
141         x = 3.5;
142         y = -3.5;
143         z = -10;
144         expected = f.value(x, y, z);
145         result = p.value(x, y, z);
146         Assert.assertEquals("half-way between sample points (border of the patch)", expected, result, 0.3);
147     }
148 
149     /**
150      * Test of interpolator for a sine wave.
151      * <p>
152      * <p>
153      *  f(x, y, z) = a cos [&omega; z - k<sub>y</sub> x - k<sub>y</sub> y]
154      * </p>
155      * with A = 0.2, &omega; = 0.5, k<sub>x</sub> = 2, k<sub>y</sub> = 1.
156      */
157     @Ignore@Test
testWave()158     public void testWave() {
159         double[] xval = new double[] {3, 4, 5, 6.5};
160         double[] yval = new double[] {-4, -3, -1, 2, 2.5};
161         double[] zval = new double[] {-12, -8, -5.5, -3, 0, 4};
162 
163         final double a = 0.2;
164         final double omega = 0.5;
165         final double kx = 2;
166         final double ky = 1;
167 
168         // Function values
169         TrivariateFunction f = new TrivariateFunction() {
170                 public double value(double x, double y, double z) {
171                     return a * FastMath.cos(omega * z - kx * x - ky * y);
172                 }
173             };
174 
175         double[][][] fval = new double[xval.length][yval.length][zval.length];
176         for (int i = 0; i < xval.length; i++) {
177             for (int j = 0; j < yval.length; j++) {
178                 for (int k = 0; k < zval.length; k++) {
179                     fval[i][j][k] = f.value(xval[i], yval[j], zval[k]);
180                 }
181             }
182         }
183 
184         TrivariateGridInterpolator interpolator = new TricubicSplineInterpolator();
185 
186         TrivariateFunction p = interpolator.interpolate(xval, yval, zval, fval);
187         double x, y, z;
188         double expected, result;
189 
190         x = 4;
191         y = -3;
192         z = 0;
193         expected = f.value(x, y, z);
194         result = p.value(x, y, z);
195         Assert.assertEquals("On sample point",
196                             expected, result, 1e-12);
197 
198         x = 4.5;
199         y = -1.5;
200         z = -4.25;
201         expected = f.value(x, y, z);
202         result = p.value(x, y, z);
203         Assert.assertEquals("Half-way between sample points (middle of the patch)",
204                             expected, result, 0.1);
205 
206         x = 3.5;
207         y = -3.5;
208         z = -10;
209         expected = f.value(x, y, z);
210         result = p.value(x, y, z);
211         Assert.assertEquals("Half-way between sample points (border of the patch)",
212                             expected, result, 0.1);
213     }
214 }
215