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.CalculusFieldTrivariateFunction;
26 import org.hipparchus.analysis.FieldTrivariateFunction;
27 import org.hipparchus.analysis.TrivariateFunction;
28 import org.hipparchus.exception.MathIllegalArgumentException;
29 import org.hipparchus.util.Binary64;
30 import org.hipparchus.util.Binary64Field;
31 import org.hipparchus.util.FastMath;
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.assertFalse;
36 import static org.junit.jupiter.api.Assertions.assertTrue;
37 import static org.junit.jupiter.api.Assertions.fail;
38
39
40
41
42 public final class TricubicInterpolatorTest {
43
44
45
46 @Test
47 void testPreconditions() {
48 double[] xval = new double[] {3, 4, 5, 6.5};
49 double[] yval = new double[] {-4, -3, -1, 2.5};
50 double[] zval = new double[] {-12, -8, -5.5, -3, 0, 2.5};
51 double[][][] fval = new double[xval.length][yval.length][zval.length];
52
53 @SuppressWarnings("unused")
54 TrivariateFunction tcf = new TricubicInterpolator().interpolate(xval, yval, zval, fval);
55
56 double[] wxval = new double[] {3, 2, 5, 6.5};
57 try {
58 tcf = new TricubicInterpolator().interpolate(wxval, yval, zval, fval);
59 fail("an exception should have been thrown");
60 } catch (MathIllegalArgumentException e) {
61
62 }
63 double[] wyval = new double[] {-4, -1, -1, 2.5};
64 try {
65 tcf = new TricubicInterpolator().interpolate(xval, wyval, zval, fval);
66 fail("an exception should have been thrown");
67 } catch (MathIllegalArgumentException e) {
68
69 }
70 double[] wzval = new double[] {-12, -8, -9, -3, 0, 2.5};
71 try {
72 tcf = new TricubicInterpolator().interpolate(xval, yval, wzval, fval);
73 fail("an exception should have been thrown");
74 } catch (MathIllegalArgumentException e) {
75
76 }
77 double[][][] wfval = new double[xval.length - 1][yval.length][zval.length];
78 try {
79 tcf = new TricubicInterpolator().interpolate(xval, yval, zval, wfval);
80 fail("an exception should have been thrown");
81 } catch (MathIllegalArgumentException e) {
82
83 }
84 wfval = new double[xval.length][yval.length - 1][zval.length];
85 try {
86 tcf = new TricubicInterpolator().interpolate(xval, yval, zval, wfval);
87 fail("an exception should have been thrown");
88 } catch (MathIllegalArgumentException e) {
89
90 }
91 wfval = new double[xval.length][yval.length][zval.length - 1];
92 try {
93 tcf = new TricubicInterpolator().interpolate(xval, yval, zval, wfval);
94 fail("an exception should have been thrown");
95 } catch (MathIllegalArgumentException e) {
96
97 }
98 }
99
100 public void testIsValid() {
101 double[] xval = new double[] {3, 4, 5, 6.5};
102 double[] yval = new double[] {-4, -3, -1, 2.5};
103 double[] zval = new double[] {-12, -8, -5.5, -3, 0, 2.5};
104 double[][][] fval = new double[xval.length][yval.length][zval.length];
105
106 TricubicInterpolatingFunction tcf = new TricubicInterpolator().interpolate(xval, yval, zval, fval);
107
108
109 assertTrue(tcf.isValidPoint(4, -3, -8));
110 assertTrue(tcf.isValidPoint(5, -3, -8));
111 assertTrue(tcf.isValidPoint(4, -1, -8));
112 assertTrue(tcf.isValidPoint(5, -1, -8));
113 assertTrue(tcf.isValidPoint(4, -3, 0));
114 assertTrue(tcf.isValidPoint(5, -3, 0));
115 assertTrue(tcf.isValidPoint(4, -1, 0));
116 assertTrue(tcf.isValidPoint(5, -1, 0));
117
118
119 assertFalse(tcf.isValidPoint(3.5, -3, -8));
120 assertFalse(tcf.isValidPoint(4.5, -3.1, -8));
121 assertFalse(tcf.isValidPoint(4.5, -2, 0));
122 assertFalse(tcf.isValidPoint(4.5, 0, -3.5));
123 assertFalse(tcf.isValidPoint(-10, 4.1, -1));
124 }
125
126
127
128
129
130
131
132 @Test
133 void testPlane() {
134 double[] xval = new double[] {3, 4, 5, 6.5};
135 double[] yval = new double[] {-4, -3, -1, 2, 2.5};
136 double[] zval = new double[] {-12, -8, -5.5, -3, 0, 2.5};
137
138
139 TrivariateFunction f = (x, y, z) -> 2 * x - 3 * y - 4 * z + 5;
140 CalculusFieldTrivariateFunction<Binary64> fT = new FieldTrivariateFunction() {
141 @Override
142 public <T extends CalculusFieldElement<T>> T value(T x, T y, T z) {
143 return x.twice().add(y.multiply(-3)).add(z.multiply(-4)).add(5);
144 }
145 }.toCalculusFieldTrivariateFunction(Binary64Field.getInstance());
146
147 double[][][] fval = new double[xval.length][yval.length][zval.length];
148
149 for (int i = 0; i < xval.length; i++) {
150 for (int j = 0; j < yval.length; j++) {
151 for (int k = 0; k < zval.length; k++) {
152 fval[i][j][k] = f.value(xval[i], yval[j], zval[k]);
153 }
154 }
155 }
156
157 TricubicInterpolatingFunction tcf = new TricubicInterpolator().interpolate(xval,
158 yval,
159 zval,
160 fval);
161 double x, y, z;
162 double expected, result;
163 Binary64 x64, y64, z64;
164 Binary64 expectedT, resultT;
165
166 x = 4;
167 y = -3;
168 z = 0;
169 expected = f.value(x, y, z);
170 result = tcf.value(x, y, z);
171 assertEquals(expected, result, 1e-15, "On sample point");
172
173 x64 = new Binary64(x);
174 y64 = new Binary64(y);
175 z64 = new Binary64(z);
176 expectedT = fT.value(x64, y64, z64);
177 resultT = tcf.value(x64, y64, z64);
178 assertEquals(expectedT.getReal(), resultT.getReal(), 1e-15, "On sample point with CalculusFieldElement");
179
180 x = 4.5;
181 y = -1.5;
182 z = -4.25;
183 expected = f.value(x, y, z);
184 result = tcf.value(x, y, z);
185 assertEquals(expected, result, 1e-14, "Half-way between sample points (middle of the patch)");
186
187 x64 = new Binary64(x);
188 y64 = new Binary64(y);
189 z64 = new Binary64(z);
190 expectedT = fT.value(x64, y64, z64);
191 resultT = tcf.value(x64, y64, z64);
192 assertEquals(expectedT.getReal(), resultT.getReal(), 1e-14, "Half-way between sample points (middle of the patch) with CalculusFieldElement");
193 }
194
195
196
197
198
199
200
201
202 @Test
203 void testWave() {
204 double[] xval = new double[] {3, 4, 5, 6.5};
205 double[] yval = new double[] {-4, -3, -1, 2, 2.5};
206 double[] zval = new double[] {-12, -8, -5.5, -3, 0, 4};
207
208 final double a = 0.2;
209 final double omega = 0.5;
210 final double kx = 2;
211 final double ky = 1;
212
213
214 TrivariateFunction f = (x, y, z) -> a * FastMath.cos(omega * z - kx * x - ky * y);
215 CalculusFieldTrivariateFunction<Binary64> fT = (x, y, z) ->
216 z.multiply(omega).subtract(x.multiply(kx)).subtract(y.multiply(ky)).cos().multiply(a);
217
218 double[][][] fval = new double[xval.length][yval.length][zval.length];
219 for (int i = 0; i < xval.length; i++) {
220 for (int j = 0; j < yval.length; j++) {
221 for (int k = 0; k < zval.length; k++) {
222 fval[i][j][k] = f.value(xval[i], yval[j], zval[k]);
223 }
224 }
225 }
226
227 TricubicInterpolatingFunction tcf = new TricubicInterpolator().interpolate(xval,
228 yval,
229 zval,
230 fval);
231
232 double x, y, z;
233 double expected, result;
234 Binary64 x64, y64, z64;
235 Binary64 expectedT, resultT;
236
237 x = 4;
238 y = -3;
239 z = 0;
240 expected = f.value(x, y, z);
241 result = tcf.value(x, y, z);
242 assertEquals(expected, result, 1e-14, "On sample point");
243
244 x64 = new Binary64(x);
245 y64 = new Binary64(y);
246 z64 = new Binary64(z);
247 expectedT = fT.value(x64, y64, z64);
248 resultT = tcf.value(x64, y64, z64);
249 assertEquals(expectedT.getReal(), resultT.getReal(), 1e-14, "On sample point with CalculusFieldElement");
250
251 x = 4.5;
252 y = -1.5;
253 z = -4.25;
254 expected = f.value(x, y, z);
255 result = tcf.value(x, y, z);
256 assertEquals(expected, result, 1e-1, "Half-way between sample points (middle of the patch)");
257
258 x64 = new Binary64(x);
259 y64 = new Binary64(y);
260 z64 = new Binary64(z);
261 expectedT = fT.value(x64, y64, z64);
262 resultT = tcf.value(x64, y64, z64);
263 assertEquals(expectedT.getReal(), resultT.getReal(), 1e-1, "Half-way between sample points (middle of the patch) with CalculusFieldElement");
264 }
265 }