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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.math.analysis.interpolation;
19
import org.apache.commons.math.MathException;
20
import org.apache.commons.math.DimensionMismatchException;
21
import org.apache.commons.math.analysis.BivariateRealFunction;
22
import org.junit.Assert;
23
import org.junit.Test;
26
* Testcase for the bicubic interpolator.
28
* @version $Revision: 821626 $ $Date: 2009-10-04 23:57:30 +0200 (Sun, 04 Oct 2009) $
30
public final class BicubicSplineInterpolatorTest {
35
public void testPreconditions() throws MathException {
36
double[] xval = new double[] {3, 4, 5, 6.5};
37
double[] yval = new double[] {-4, -3, -1, 2.5};
38
double[][] zval = new double[xval.length][yval.length];
40
BivariateRealGridInterpolator interpolator = new BicubicSplineInterpolator();
42
@SuppressWarnings("unused")
43
BivariateRealFunction p = interpolator.interpolate(xval, yval, zval);
45
double[] wxval = new double[] {3, 2, 5, 6.5};
47
p = interpolator.interpolate(wxval, yval, zval);
48
Assert.fail("an exception should have been thrown");
49
} catch (IllegalArgumentException e) {
53
double[] wyval = new double[] {-4, -3, -1, -1};
55
p = interpolator.interpolate(xval, wyval, zval);
56
Assert.fail("an exception should have been thrown");
57
} catch (IllegalArgumentException e) {
61
double[][] wzval = new double[xval.length][yval.length + 1];
63
p = interpolator.interpolate(xval, yval, wzval);
64
Assert.fail("an exception should have been thrown");
65
} catch (DimensionMismatchException e) {
68
wzval = new double[xval.length - 1][yval.length];
70
p = interpolator.interpolate(xval, yval, wzval);
71
Assert.fail("an exception should have been thrown");
72
} catch (DimensionMismatchException e) {
78
* Test of interpolator for a plane.
83
public void testPlane() throws MathException {
84
BivariateRealFunction f = new BivariateRealFunction() {
85
public double value(double x, double y) {
86
return 2 * x - 3 * y + 5;
90
BivariateRealGridInterpolator interpolator = new BicubicSplineInterpolator();
92
double[] xval = new double[] {3, 4, 5, 6.5};
93
double[] yval = new double[] {-4, -3, -1, 2, 2.5};
94
double[][] zval = new double[xval.length][yval.length];
95
for (int i = 0; i < xval.length; i++) {
96
for (int j = 0; j < yval.length; j++) {
97
zval[i][j] = f.value(xval[i], yval[j]);
101
BivariateRealFunction p = interpolator.interpolate(xval, yval, zval);
103
double expected, result;
107
expected = f.value(x, y);
108
result = p.value(x, y);
109
Assert.assertEquals("On sample point", expected, result, 1e-15);
113
expected = f.value(x, y);
114
result = p.value(x, y);
115
Assert.assertEquals("half-way between sample points (middle of the patch)", expected, result, 0.3);
119
expected = f.value(x, y);
120
result = p.value(x, y);
121
Assert.assertEquals("half-way between sample points (border of the patch)", expected, result, 0.3);
125
* Test of interpolator for a paraboloid.
127
* z = 2 x<sup>2</sup> - 3 y<sup>2</sup> + 4 x y - 5
130
public void testParaboloid() throws MathException {
131
BivariateRealFunction f = new BivariateRealFunction() {
132
public double value(double x, double y) {
133
return 2 * x * x - 3 * y * y + 4 * x * y - 5;
137
BivariateRealGridInterpolator interpolator = new BicubicSplineInterpolator();
139
double[] xval = new double[] {3, 4, 5, 6.5};
140
double[] yval = new double[] {-4, -3, -2, -1, 0.5, 2.5};
141
double[][] zval = new double[xval.length][yval.length];
142
for (int i = 0; i < xval.length; i++) {
143
for (int j = 0; j < yval.length; j++) {
144
zval[i][j] = f.value(xval[i], yval[j]);
148
BivariateRealFunction p = interpolator.interpolate(xval, yval, zval);
150
double expected, result;
154
expected = f.value(x, y);
155
result = p.value(x, y);
156
Assert.assertEquals("On sample point", expected, result, 1e-13);
160
expected = f.value(x, y);
161
result = p.value(x, y);
162
Assert.assertEquals("half-way between sample points (middle of the patch)", expected, result, 0.2);
166
expected = f.value(x, y);
167
result = p.value(x, y);
168
Assert.assertEquals("half-way between sample points (border of the patch)", expected, result, 0.2);