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.transform;
23
24 import org.hipparchus.analysis.UnivariateFunction;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.hipparchus.util.FastMath;
27 import org.junit.jupiter.params.ParameterizedTest;
28 import org.junit.jupiter.params.provider.MethodSource;
29
30 import java.util.Random;
31
32 import static org.junit.jupiter.api.Assertions.assertEquals;
33 import static org.junit.jupiter.api.Assertions.fail;
34
35
36
37
38
39
40
41
42
43
44
45 public abstract class RealTransformerAbstractTest <T> {
46
47
48 private final static long SEED = 20110119L;
49
50
51
52
53
54
55 abstract RealTransformer createRealTransformer();
56
57
58
59
60
61
62
63
64
65 abstract int getInvalidDataSize(int i);
66
67
68
69
70
71
72
73
74
75 abstract int getNumberOfInvalidDataSizes();
76
77
78
79
80
81
82 abstract int getNumberOfValidDataSizes();
83
84
85
86
87
88
89
90
91 abstract double getRelativeTolerance(int i);
92
93
94
95
96
97
98
99
100
101
102 abstract int getValidDataSize(int i);
103
104
105
106
107
108
109
110
111
112 abstract UnivariateFunction getValidFunction();
113
114
115
116
117
118
119
120
121
122 abstract double getValidLowerBound();
123
124
125
126
127
128
129
130
131
132 abstract double getValidUpperBound();
133
134
135
136
137
138
139
140
141 abstract double[] transform(double[] x, TransformType type);
142
143
144
145
146
147 abstract void init(T normalization);
148
149
150
151
152
153
154
155
156
157 @ParameterizedTest
158 @MethodSource("data")
159 public void testTransformRealInvalidDataSize(T normalization) {
160 init(normalization);
161 final TransformType[] type = TransformType.values();
162 final RealTransformer transformer = createRealTransformer();
163 for (int i = 0; i < getNumberOfInvalidDataSizes(); i++) {
164 final int n = getInvalidDataSize(i);
165 for (int j = 0; j < type.length; j++) {
166 try {
167 transformer.transform(createRealData(n), type[j]);
168 fail(type[j] + ", " + n);
169 } catch (MathIllegalArgumentException e) {
170
171 }
172 }
173 }
174 }
175
176
177
178
179
180
181 @ParameterizedTest
182 @MethodSource("data")
183 public void testTransformFunctionInvalidDataSize(T normalization) {
184 init(normalization);
185 final TransformType[] type = TransformType.values();
186 final RealTransformer transformer = createRealTransformer();
187 final UnivariateFunction f = getValidFunction();
188 final double a = getValidLowerBound();
189 final double b = getValidUpperBound();
190 for (int i = 0; i < getNumberOfInvalidDataSizes(); i++) {
191 final int n = getInvalidDataSize(i);
192 for (int j = 0; j < type.length; j++) {
193 try {
194 transformer.transform(f, a, b, n, type[j]);
195 fail(type[j] + ", " + n);
196 } catch (MathIllegalArgumentException e) {
197
198 }
199 }
200 }
201 }
202
203
204
205
206
207
208 @ParameterizedTest
209 @MethodSource("data")
210 public void testTransformFunctionNotStrictlyPositiveNumberOfSamples(T normalization) {
211 init(normalization);
212 final TransformType[] type = TransformType.values();
213 final RealTransformer transformer = createRealTransformer();
214 final UnivariateFunction f = getValidFunction();
215 final double a = getValidLowerBound();
216 final double b = getValidUpperBound();
217 for (int i = 0; i < getNumberOfValidDataSizes(); i++) {
218 final int n = getValidDataSize(i);
219 for (int j = 0; j < type.length; j++) {
220 try {
221 transformer.transform(f, a, b, -n, type[j]);
222 fail(type[j] + ", " + (-n));
223 } catch (MathIllegalArgumentException e) {
224
225 }
226 }
227 }
228 }
229
230
231
232
233
234
235 @ParameterizedTest
236 @MethodSource("data")
237 public void testTransformFunctionInvalidBounds(T normalization) {
238 init(normalization);
239 final TransformType[] type = TransformType.values();
240 final RealTransformer transformer = createRealTransformer();
241 final UnivariateFunction f = getValidFunction();
242 final double a = getValidLowerBound();
243 final double b = getValidUpperBound();
244 for (int i = 0; i < getNumberOfValidDataSizes(); i++) {
245 final int n = getValidDataSize(i);
246 for (int j = 0; j < type.length; j++) {
247 try {
248 transformer.transform(f, b, a, n, type[j]);
249 fail(type[j] + ", " + b + ", " + a);
250 } catch (MathIllegalArgumentException e) {
251
252 }
253 }
254 }
255 }
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272 @ParameterizedTest
273 @MethodSource("data")
274 public void testTransformReal(T normalization) {
275 init(normalization);
276 final TransformType[] type = TransformType.values();
277 for (int i = 0; i < getNumberOfValidDataSizes(); i++) {
278 final int n = getValidDataSize(i);
279 final double tol = getRelativeTolerance(i);
280 for (int j = 0; j < type.length; j++) {
281 doTestTransformReal(n, tol, type[j]);
282 }
283 }
284 }
285
286
287
288
289
290
291
292
293
294
295
296
297 @ParameterizedTest
298 @MethodSource("data")
299 public void testTransformFunction(T normalization) {
300 init(normalization);
301 final TransformType[] type = TransformType.values();
302 for (int i = 0; i < getNumberOfValidDataSizes(); i++) {
303 final int n = getValidDataSize(i);
304 final double tol = getRelativeTolerance(i);
305 for (int j = 0; j < type.length; j++) {
306 doTestTransformFunction(n, tol, type[j]);
307 }
308 }
309 }
310
311
312
313
314
315
316
317
318
319
320
321
322 double[] createRealData(final int n) {
323 final Random random = new Random(SEED);
324 final double[] data = new double[n];
325 for (int i = 0; i < n; i++) {
326 data[i] = 2.0 * random.nextDouble() - 1.0;
327 }
328 return data;
329 }
330
331
332
333
334
335 private void doTestTransformReal(final int n, final double tol,
336 final TransformType type) {
337 final RealTransformer transformer = createRealTransformer();
338 final double[] x = createRealData(n);
339 final double[] expected = transform(x, type);
340 final double[] actual = transformer.transform(x, type);
341 for (int i = 0; i < n; i++) {
342 final String msg = String.format("%d, %d", n, i);
343 final double delta = tol * FastMath.abs(expected[i]);
344 assertEquals(expected[i], actual[i], delta, msg);
345 }
346 }
347
348 private void doTestTransformFunction(final int n, final double tol,
349 final TransformType type) {
350 final RealTransformer transformer = createRealTransformer();
351 final UnivariateFunction f = getValidFunction();
352 final double a = getValidLowerBound();
353 final double b = getValidUpperBound();
354 final double[] x = createRealData(n);
355 for (int i = 0; i < n; i++) {
356 final double t = a + i * (b - a) / n;
357 x[i] = f.value(t);
358 }
359 final double[] expected = transform(x, type);
360 final double[] actual = transformer.transform(f, a, b, n, type);
361 for (int i = 0; i < n; i++) {
362 final String msg = String.format("%d, %d", n, i);
363 final double delta = tol * FastMath.abs(expected[i]);
364 assertEquals(expected[i], actual[i], delta, msg);
365 }
366 }
367 }