1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.analysis.interpolation;
23
24 import org.hipparchus.CalculusFieldElement;
25 import org.hipparchus.analysis.BivariateFunction;
26 import org.hipparchus.analysis.CalculusFieldBivariateFunction;
27 import org.hipparchus.analysis.FieldBivariateFunction;
28 import org.hipparchus.exception.MathIllegalArgumentException;
29 import org.hipparchus.random.RandomDataGenerator;
30 import org.hipparchus.util.Binary64;
31 import org.hipparchus.util.Binary64Field;
32 import org.junit.jupiter.api.Test;
33
34 import static org.junit.jupiter.api.Assertions.assertEquals;
35 import static org.junit.jupiter.api.Assertions.fail;
36
37
38
39
40 final class BicubicInterpolatorTest {
41
42
43
44 @Test
45 void testPreconditions() {
46 double[] xval = new double[] {3, 4, 5, 6.5};
47 double[] yval = new double[] {-4, -3, -1, 2.5};
48 double[][] zval = new double[xval.length][yval.length];
49
50 BivariateGridInterpolator interpolator = new BicubicInterpolator();
51
52 @SuppressWarnings("unused")
53 BivariateFunction p = interpolator.interpolate(xval, yval, zval);
54
55 double[] wxval = new double[] {3, 2, 5, 6.5};
56 try {
57 p = interpolator.interpolate(wxval, yval, zval);
58 fail("an exception should have been thrown");
59 } catch (MathIllegalArgumentException e) {
60
61 }
62
63 double[] wyval = new double[] {-4, -3, -1, -1};
64 try {
65 p = interpolator.interpolate(xval, wyval, zval);
66 fail("an exception should have been thrown");
67 } catch (MathIllegalArgumentException e) {
68
69 }
70
71 double[][] wzval = new double[xval.length][yval.length + 1];
72 try {
73 p = interpolator.interpolate(xval, yval, wzval);
74 fail("an exception should have been thrown");
75 } catch (MathIllegalArgumentException e) {
76
77 }
78 wzval = new double[xval.length - 1][yval.length];
79 try {
80 p = interpolator.interpolate(xval, yval, wzval);
81 fail("an exception should have been thrown");
82 } catch (MathIllegalArgumentException e) {
83
84 }
85 }
86
87
88
89
90
91
92 @Test
93 void testPlane() {
94 BivariateFunction f = (x, y) -> 2 * x - 3 * y + 5;
95 CalculusFieldBivariateFunction<Binary64> fT = new FieldBivariateFunction() {
96 @Override
97 public <T extends CalculusFieldElement<T>> T value(T x, T y) {
98 return x.twice().add(y.multiply(-3)).add(5);
99 }
100 }.toCalculusFieldBivariateFunction(Binary64Field.getInstance());
101
102 testInterpolation(3000,
103 1e-13,
104 f,
105 fT,
106 false);
107 }
108
109
110
111
112
113
114 @Test
115 void testParaboloid() {
116 BivariateFunction f = (x, y) -> 2 * x * x - 3 * y * y + 4 * x * y - 5;
117 CalculusFieldBivariateFunction<Binary64> fT = (x, y) -> x.multiply(x).twice()
118 .add(y.multiply(y).multiply(-3))
119 .add(x.multiply(y).multiply(4))
120 .add(-5);
121
122 testInterpolation(3000,
123 1e-12,
124 f,
125 fT,
126 false);
127 }
128
129
130
131
132
133
134
135 private void testInterpolation(int numSamples,
136 double tolerance,
137 BivariateFunction f,
138 CalculusFieldBivariateFunction<Binary64> fT,
139 boolean print) {
140 final int sz = 21;
141 final double[] xval = new double[sz];
142 final double[] yval = new double[sz];
143
144 final double delta = 1d / (sz - 1);
145 for (int i = 0; i < sz; i++) {
146 xval[i] = -1 + 15 * i * delta;
147 yval[i] = -20 + 30 * i * delta;
148 }
149
150 final double[][] zval = new double[xval.length][yval.length];
151 for (int i = 0; i < xval.length; i++) {
152 for (int j = 0; j < yval.length; j++) {
153 zval[i][j] = f.value(xval[i], yval[j]);
154 }
155 }
156
157 final BicubicInterpolator interpolator = new BicubicInterpolator();
158 final BicubicInterpolatingFunction p = interpolator.interpolate(xval, yval, zval);
159 double x, y;
160
161 final RandomDataGenerator gen = new RandomDataGenerator(1234567L);
162 int count = 0;
163 while (true) {
164 x = gen.nextUniform(xval[0], xval[xval.length - 1]);
165 y = gen.nextUniform(yval[0], yval[yval.length - 1]);
166 if (!p.isValidPoint(x, y)) {
167 if (print) {
168 System.out.println("# " + x + " " + y);
169 }
170 continue;
171 }
172
173 if (count++ > numSamples) {
174 break;
175 }
176 final double expected = f.value(x, y);
177 final double actual = p.value(x, y);
178
179 if (print) {
180 System.out.println(x + " " + y + " " + expected + " " + actual);
181 }
182
183 assertEquals(expected, actual, tolerance);
184
185 final Binary64 x64 = new Binary64(x);
186 final Binary64 y64 = new Binary64(y);
187 final Binary64 expectedT = fT.value(x64, y64);
188 final Binary64 actualT = p.value(x64, y64);
189
190 assertEquals(expectedT.getReal(), actualT.getReal(), tolerance);
191 }
192 }
193 }