1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.linear;
18
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.Random;
24 import java.util.Set;
25
26 import org.hipparchus.analysis.UnivariateFunction;
27 import org.hipparchus.analysis.function.Sin;
28 import org.hipparchus.exception.MathRuntimeException;
29 import org.hipparchus.linear.RealVector.Entry;
30 import org.hipparchus.util.FastMath;
31 import org.junit.Assert;
32 import org.junit.Test;
33
34
35
36
37
38
39
40
41
42
43
44 public abstract class UnmodifiableRealVectorAbstractTest {
45
46 protected static final int DIM = 100;
47
48 protected static final double EPS = 10 * Math.ulp(1d);
49
50
51
52
53 protected static final Set<String> EXCLUDE = new HashSet<String>();
54
55 protected static final Random RANDOM;
56
57 static {
58 EXCLUDE.add("getEntry");
59 EXCLUDE.add("setEntry");
60 EXCLUDE.add("addToEntry");
61 EXCLUDE.add("getSubVector");
62 EXCLUDE.add("setSubVector");
63 EXCLUDE.add("iterator");
64 EXCLUDE.add("sparseIterator");
65 EXCLUDE.add("walkInDefaultOrder");
66 EXCLUDE.add("walkInOptimizedOrder");
67 EXCLUDE.add("ebeDivide");
68 EXCLUDE.add("ebeMultiply");
69
70
71 for (Method m : Object.class.getMethods()) {
72 EXCLUDE.add(m.getName());
73 }
74 RANDOM = new Random(20110813);
75 }
76
77
78
79
80
81
82
83
84
85 public static boolean equals(final double x, final double y) {
86 if (x == y) {
87 return true;
88 } else if (FastMath.abs(x) <= EPS) {
89 return FastMath.abs(y) <= EPS;
90 } else if (FastMath.abs(y) <= EPS) {
91 return FastMath.abs(x) <= EPS;
92 } else {
93 return FastMath.abs(x - y) <= EPS * FastMath.min(FastMath.abs(x), FastMath.abs(y));
94 }
95 }
96
97
98
99
100
101
102
103
104
105 public static boolean equals(final double[] x, final double[] y) {
106 if (x.length != y.length) {
107 return false;
108 }
109 final int n = x.length;
110 for (int i = 0; i < n; i++) {
111 if (!equals(x[i], y[i])) {
112 return false;
113 }
114 }
115 return true;
116 }
117
118
119
120
121
122
123
124
125
126 public static boolean equals(final RealVector x, final RealVector y) {
127 if (x.getDimension() != y.getDimension()) {
128 return false;
129 }
130 final int n = x.getDimension();
131 for (int i = 0; i < n; i++) {
132 if (!equals(x.getEntry(i), y.getEntry(i))) {
133 return false;
134 }
135 }
136 return true;
137 }
138
139
140
141
142
143
144
145
146
147 public static boolean equals(final RealVector x, final double[] y) {
148 if (x.getDimension() != y.length) {
149 return false;
150 }
151 final int n = x.getDimension();
152 for (int i = 0; i < n; i++) {
153 if (!equals(x.getEntry(i), y[i])) {
154 return false;
155 }
156 }
157 return true;
158 }
159
160
161
162
163
164
165
166
167
168 public static boolean equals(final RealMatrix x, final RealMatrix y) {
169 if (x.getRowDimension() != y.getRowDimension()) {
170 return false;
171 }
172 if (x.getColumnDimension() != y.getColumnDimension()) {
173 return false;
174 }
175 final int rows = x.getRowDimension();
176 final int cols = x.getColumnDimension();
177 for (int i = 0; i < rows; i++) {
178 for (int j = 0; j < cols; j++) {
179 if (!equals(x.getEntry(i, j), y.getEntry(i, j))) {
180 return false;
181 }
182 }
183 }
184 return true;
185 }
186
187
188
189
190
191
192
193
194
195
196 public static boolean equals(final Object x, final Object y) {
197 if (x instanceof Boolean) {
198 if (y instanceof Boolean) {
199 return ((Boolean) x).booleanValue() == ((Boolean) y)
200 .booleanValue();
201 } else {
202 return false;
203 }
204 }
205 if (x instanceof Integer) {
206 if (y instanceof Integer) {
207 return ((Integer) x).intValue() == ((Integer) y).intValue();
208 } else {
209 return false;
210 }
211 } else if (x instanceof Double) {
212 if (y instanceof Double) {
213 return equals(((Double) x).doubleValue(),
214 ((Double) y).doubleValue());
215 } else {
216 return false;
217 }
218 } else if (x instanceof double[]) {
219 if (y instanceof double[]) {
220 return equals((double[]) x, (double[]) y);
221 } else if (y instanceof RealVector) {
222 return equals((RealVector) y, (double[]) x);
223 } else {
224 return false;
225 }
226 } else if (x instanceof RealVector) {
227 if (y instanceof double[]) {
228 return equals((RealVector) x, (double[]) y);
229 } else if (y instanceof RealVector) {
230 return equals((RealVector) x, (RealVector) y);
231 } else {
232 return false;
233 }
234 } else if (x instanceof RealMatrix) {
235 if (y instanceof RealMatrix) {
236 return equals((RealMatrix) x, (RealMatrix) y);
237 } else {
238 return false;
239 }
240 } else {
241 throw new IllegalArgumentException("could not compare " + x + ", "
242 + y);
243 }
244 }
245
246
247
248
249
250
251
252 public abstract RealVector createVector();
253
254
255
256
257
258
259
260
261
262 public Object createParameter(final Class<?> c) {
263 if (c == Integer.TYPE) {
264 return Integer.valueOf(RANDOM.nextInt());
265 } else if (c == Double.TYPE) {
266 return Double.valueOf(RANDOM.nextDouble());
267 } else if (c == double[].class) {
268 final double[] v = new double[DIM];
269 for (int i = 0; i < DIM; i++) {
270 v[i] = RANDOM.nextDouble();
271 }
272 return v;
273 } else if (c.isAssignableFrom(RealVector.class)) {
274 return createVector();
275 } else if (c.isAssignableFrom(UnivariateFunction.class)) {
276 return new Sin();
277 } else {
278 throw new IllegalArgumentException("could not create " + c);
279 }
280 }
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 private void callMethod(final Method m,
303 final RealVector u,
304 final Object... args)
305 throws IllegalAccessException,
306 IllegalArgumentException,
307 InvocationTargetException {
308 final RealVector uu = u.copy();
309 final RealVector v = RealVector.unmodifiableRealVector(u.copy());
310 Object exp = m.invoke(u, args);
311 if (equals(uu, u)) {
312 Object act = m.invoke(v, args);
313 Assert.assertTrue(m.toGenericString() + ", unmodifiable vector has changed",
314 equals(uu, v));
315 Assert.assertTrue(m.toGenericString() + ", wrong result",
316 equals(exp, act));
317
318 } else {
319 boolean flag = false;
320 try {
321 m.invoke(v, args);
322 } catch (InvocationTargetException e) {
323 if (e.getCause() instanceof MathRuntimeException) {
324 flag = true;
325 }
326 }
327 Assert.assertTrue(m.toGenericString()+", exception should have been thrown", flag);
328 }
329 }
330
331
332
333
334
335
336
337 @Test
338 public void testAllButExcluded()
339 throws IllegalAccessException,
340 IllegalArgumentException,
341 InvocationTargetException {
342 Method[] method = RealVector.class.getMethods();
343 for (int i = 0; i < method.length; i++) {
344 Method m = method[i];
345 if (!EXCLUDE.contains(m.getName())) {
346 RealVector u = (RealVector) createParameter(RealVector.class);
347 Class<?>[] paramType = m.getParameterTypes();
348 Object[] param = new Object[paramType.length];
349 for (int j = 0; j < paramType.length; j++) {
350 param[j] = createParameter(paramType[j]);
351 }
352 callMethod(m, u, param);
353 }
354 }
355 }
356
357 @Test
358 public void testGetEntry() {
359 RealVector u = createVector();
360 RealVector v = RealVector.unmodifiableRealVector(u);
361 for (int i = 0; i < DIM; i++) {
362 Assert.assertTrue(equals(u.getEntry(i), v.getEntry(i)));
363 }
364 }
365
366 @Test(expected = MathRuntimeException.class)
367 public void testSetEntry() {
368 RealVector u = createVector();
369 RealVector v = RealVector.unmodifiableRealVector(u);
370 for (int i = 0; i < DIM; i++) {
371 v.setEntry(i, 0d);
372 }
373 }
374
375 @Test(expected = MathRuntimeException.class)
376 public void testAddToEntry() {
377 RealVector u = createVector();
378 RealVector v = RealVector.unmodifiableRealVector(u);
379 for (int i = 0; i < DIM; i++) {
380 v.addToEntry(i, 0d);
381 }
382 }
383
384 @Test
385 public void testGetSubVector() {
386 RealVector u = createVector();
387 RealVector v = RealVector.unmodifiableRealVector(u);
388 for (int i = 0; i < DIM; i++) {
389 for (int n = 1; n < DIM - i; n++) {
390 RealVector exp = u.getSubVector(i, n);
391 RealVector act = v.getSubVector(i, n);
392 Assert.assertTrue(equals(exp, act));
393 }
394 }
395 }
396
397 @Test(expected = MathRuntimeException.class)
398 public void testSetSubVector() {
399 RealVector u = createVector();
400 RealVector v = RealVector.unmodifiableRealVector(u);
401 v.setSubVector(0, new ArrayRealVector());
402 }
403
404 @Test
405 public void testIterator() {
406 RealVector u = createVector();
407 Iterator<Entry> i = u.iterator();
408 RealVector v = RealVector.unmodifiableRealVector(u.copy());
409 Iterator<Entry> j = v.iterator();
410 boolean flag;
411 while (i.hasNext()) {
412 Assert.assertTrue(j.hasNext());
413 Entry exp = i.next();
414 Entry act = j.next();
415 Assert.assertTrue(equals(exp.getIndex(), act.getIndex()));
416 Assert.assertTrue(equals(exp.getValue(), act.getValue()));
417 exp.setIndex(RANDOM.nextInt(DIM));
418 act.setIndex(RANDOM.nextInt(DIM));
419 flag = false;
420 try {
421 act.setValue(RANDOM.nextDouble());
422 } catch (MathRuntimeException e) {
423 flag = true;
424 }
425 Assert.assertTrue("exception should have been thrown", flag);
426 }
427 Assert.assertFalse(j.hasNext());
428 }
429
430 @Test
431 public void testSparseIterator() {
432 RealVector u = createVector();
433 Iterator<Entry> i = u.sparseIterator();
434 RealVector v = RealVector.unmodifiableRealVector(u.copy());
435 Iterator<Entry> j = v.sparseIterator();
436 boolean flag;
437 while (i.hasNext()) {
438 Assert.assertTrue(j.hasNext());
439 Entry exp = i.next();
440 Entry act = j.next();
441 Assert.assertTrue(equals(exp.getIndex(), act.getIndex()));
442 Assert.assertTrue(equals(exp.getValue(), act.getValue()));
443 exp.setIndex(RANDOM.nextInt(DIM));
444 act.setIndex(RANDOM.nextInt(DIM));
445 flag = false;
446 try {
447 act.setValue(RANDOM.nextDouble());
448 } catch (MathRuntimeException e) {
449 flag = true;
450 }
451 Assert.assertTrue("exception should have been thrown", flag);
452 }
453 Assert.assertFalse(j.hasNext());
454 }
455 }