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.analysis.MultivariateFunction;
25 import org.hipparchus.exception.LocalizedCoreFormats;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27 import org.hipparchus.exception.MathIllegalStateException;
28 import org.hipparchus.random.RandomGenerator;
29 import org.hipparchus.random.UnitSphereRandomVectorGenerator;
30 import org.hipparchus.random.Well1024a;
31 import org.hipparchus.random.Well19937a;
32 import org.hipparchus.util.FastMath;
33 import org.junit.jupiter.api.Test;
34
35 import java.lang.reflect.InvocationTargetException;
36 import java.lang.reflect.Method;
37
38 import static org.junit.jupiter.api.Assertions.assertEquals;
39 import static org.junit.jupiter.api.Assertions.assertFalse;
40 import static org.junit.jupiter.api.Assertions.assertNotSame;
41 import static org.junit.jupiter.api.Assertions.assertTrue;
42 import static org.junit.jupiter.api.Assertions.fail;
43
44
45
46
47
48 final class MicrosphereProjectionInterpolatorTest {
49
50
51
52
53
54 @Test
55 void testLinearFunction2D() {
56 MultivariateFunction f = x -> {
57 if (x.length != 2) {
58 throw new IllegalArgumentException();
59 }
60 return 2 * x[0] - 3 * x[1] + 5;
61 };
62
63 final double darkFraction = 0.5;
64 final double darkThreshold = 1e-2;
65 final double background = Double.NaN;
66 final double exponent = 1.1;
67 final boolean shareSphere = true;
68 final double noInterpolationTolerance = Math.ulp(1d);
69 final RandomGenerator random = new Well1024a(0x1c7a150c83a6d9dal);
70
71
72 final MultivariateInterpolator interpolator
73 = new MicrosphereProjectionInterpolator(new InterpolatingMicrosphere(2, 500,
74 darkFraction,
75 darkThreshold,
76 background,
77 new UnitSphereRandomVectorGenerator(2, random)),
78 exponent,
79 shareSphere,
80 noInterpolationTolerance);
81
82
83 final MultivariateInterpolator interpolator2D
84 = new MicrosphereProjectionInterpolator(new InterpolatingMicrosphere2D(16,
85 darkFraction,
86 darkThreshold,
87 background),
88 exponent,
89 shareSphere,
90 noInterpolationTolerance);
91
92 final double min = -1;
93 final double max = 1;
94 final double range = max - min;
95 final int res = 5;
96 final int n = res * res;
97 final int dim = 2;
98 double[][] x = new double[n][dim];
99 double[] y = new double[n];
100 int index = 0;
101 for (int i = 0; i < res; i++) {
102 final double x1Val = toCoordinate(min, range, res, i);
103 for (int j = 0; j < res; j++) {
104 final double x2Val = toCoordinate(min, range, res, j);
105 x[index][0] = x1Val;
106 x[index][1] = x2Val;
107 y[index] = f.value(x[index]);
108 ++index;
109 }
110 }
111
112 final MultivariateFunction p = interpolator.interpolate(x, y);
113 final MultivariateFunction p2D = interpolator2D.interpolate(x, y);
114
115 double[] c = new double[dim];
116 double expected, result, result2D;
117
118 final int sampleIndex = 2;
119 c[0] = x[sampleIndex][0];
120 c[1] = x[sampleIndex][1];
121 expected = f.value(c);
122 result = p.value(c);
123 result2D = p2D.value(c);
124 assertEquals(expected, result2D, FastMath.ulp(1d), "on sample point (exact)");
125 assertEquals(result2D, result, FastMath.ulp(1d), "on sample point (ND vs 2D)");
126
127
128 c[0] = 0.654321;
129 c[1] = -0.345678;
130 expected = f.value(c);
131 result = p.value(c);
132 result2D = p2D.value(c);
133 assertEquals(expected, result2D, 1e-1, "interpolation (exact)");
134 assertEquals(result2D, result, 1e-1, "interpolation (ND vs 2D)");
135
136
137 c[0] = 0 - 1e-2;
138 c[1] = 1 + 1e-2;
139 expected = f.value(c);
140 result = p.value(c);
141 result2D = p2D.value(c);
142 assertFalse(Double.isNaN(result));
143 assertFalse(Double.isNaN(result2D));
144 assertEquals(expected, result2D, 1e-1, "extrapolation (exact)");
145 assertEquals(result2D, result, 1e-2, "extrapolation (ND vs 2D)");
146
147
148 c[0] = 20;
149 c[1] = -30;
150 result = p.value(c);
151 assertTrue(Double.isNaN(result), result + " should be NaN");
152 result2D = p2D.value(c);
153 assertTrue(Double.isNaN(result2D), result2D + " should be NaN");
154 }
155
156 @Test
157 void testWrongDimensions() {
158 checkWrongArguments(0, 1, 0.5, 0.0, 0.0,
159 LocalizedCoreFormats.NUMBER_TOO_SMALL_BOUND_EXCLUDED);
160 checkWrongArguments(1, 0, 0.5, 0.0, 0.0,
161 LocalizedCoreFormats.NUMBER_TOO_SMALL_BOUND_EXCLUDED);
162 checkWrongArguments(1, 1, 0.5, -1.0, 0.0,
163 LocalizedCoreFormats.NUMBER_TOO_SMALL);
164 }
165
166 private void checkWrongArguments(int dimension,
167 int size,
168 double maxDarkFraction,
169 double darkThreshold,
170 double background,
171 LocalizedCoreFormats expected) {
172 try {
173 new InterpolatingMicrosphere(dimension, size, maxDarkFraction, darkThreshold, background, null);
174 fail("an exception should have been thrown");
175 } catch (MathIllegalArgumentException miae) {
176 assertEquals(expected, miae.getSpecifier());
177 }
178 }
179
180 @Test
181 void testCopy() {
182 UnitSphereRandomVectorGenerator random =
183 new UnitSphereRandomVectorGenerator(3, new Well19937a(0x265318ael));
184 InterpolatingMicrosphere original = new InterpolatingMicrosphere(3, 30, 0.5, 0.2, 0.0, random);
185 InterpolatingMicrosphere copy = original.copy();
186 assertNotSame(original, copy);
187 assertEquals(original.getDimension(), copy.getDimension());
188 assertEquals(original.getSize(), copy.getSize());
189 }
190
191 @Test
192 void testSizeLimit() {
193 UnitSphereRandomVectorGenerator random =
194 new UnitSphereRandomVectorGenerator(3, new Well19937a(0x453l));
195 InterpolatingMicrosphere ims = new InterpolatingMicrosphere(3, 30, 0.5, 0.2, 0.0, random);
196 try {
197 Method add = InterpolatingMicrosphere.class.getDeclaredMethod("add", double[].class, Boolean.TYPE);
198 add.setAccessible(true);
199 try {
200 add.invoke(ims, random.nextVector(), true);
201 fail("an exception should have been thrown");
202 } catch (InvocationTargetException ite) {
203 MathIllegalStateException miae = (MathIllegalStateException) ite.getCause();
204 assertEquals(LocalizedCoreFormats.MAX_COUNT_EXCEEDED, miae.getSpecifier());
205 }
206 } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException e) {
207 fail(e.getLocalizedMessage());
208 }
209 }
210
211 @Test
212 void testInconsistentDimensions() {
213 final int d1 = 5;
214 final int d2 = 3;
215 try {
216 UnitSphereRandomVectorGenerator random =
217 new UnitSphereRandomVectorGenerator(d1, new Well19937a(0x1l));
218 new InterpolatingMicrosphere(d2, 30, 0.5, 0.2, 0.0, random);
219 fail("an exception should have been thrown");
220 } catch (MathIllegalArgumentException miae) {
221 assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
222 assertEquals(d1, ((Integer) miae.getParts()[0]).intValue());
223 assertEquals(d2, ((Integer) miae.getParts()[1]).intValue());
224 }
225 }
226
227
228
229
230
231
232
233 private static double toCoordinate(double min,
234 double range,
235 int res,
236 int pixel) {
237 return pixel * range / (res - 1) + min;
238 }
239 }