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