1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
31
32 public final class BicubicInterpolatorTest {
33
34
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
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
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
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
76 }
77 }
78
79
80
81
82
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
101
102
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
121
122
123
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
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 }