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