1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  package org.apache.commons.math4.legacy.linear;
18  
19  import java.lang.reflect.InvocationTargetException;
20  import java.lang.reflect.Method;
21  import java.util.Set;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.Random;
25  
26  import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
27  import org.apache.commons.math4.legacy.analysis.function.Sin;
28  import org.apache.commons.math4.legacy.exception.MathUnsupportedOperationException;
29  import org.apache.commons.math4.legacy.linear.RealVector.Entry;
30  import org.apache.commons.math4.core.jdkmath.JdkMath;
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<>();
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 (JdkMath.abs(x) <= EPS) {
89              return JdkMath.abs(y) <= EPS;
90          } else if (JdkMath.abs(y) <= EPS) {
91              return JdkMath.abs(x) <= EPS;
92          } else {
93              return JdkMath.abs(x - y) <= EPS * JdkMath.min(JdkMath.abs(x), JdkMath.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         } else {
318             boolean flag = false;
319             try {
320                 m.invoke(v, args);
321             } catch (InvocationTargetException e) {
322                 if (e.getCause() instanceof MathUnsupportedOperationException) {
323                     flag = true;
324                 }
325             }
326             Assert.assertTrue(m.toGenericString()+", exception should have been thrown", flag);
327         }
328     }
329 
330     
331 
332 
333 
334 
335 
336     @Test
337     public void testAllButExcluded()
338         throws IllegalAccessException,
339                IllegalArgumentException,
340                InvocationTargetException {
341         Method[] method = RealVector.class.getMethods();
342         for (int i = 0; i < method.length; i++) {
343             Method m = method[i];
344             if (!EXCLUDE.contains(m.getName())) {
345                 RealVector u = (RealVector) createParameter(RealVector.class);
346                 Class<?>[] paramType = m.getParameterTypes();
347                 Object[] param = new Object[paramType.length];
348                 for (int j = 0; j < paramType.length; j++) {
349                     param[j] = createParameter(paramType[j]);
350                 }
351                 callMethod(m, u, param);
352             }
353         }
354     }
355 
356     @Test
357     public void testGetEntry() {
358         RealVector u = createVector();
359         RealVector v = RealVector.unmodifiableRealVector(u);
360         for (int i = 0; i < DIM; i++) {
361             Assert.assertTrue(equals(u.getEntry(i), v.getEntry(i)));
362         }
363     }
364 
365     @Test(expected = MathUnsupportedOperationException.class)
366     public void testSetEntry() {
367         RealVector u = createVector();
368         RealVector v = RealVector.unmodifiableRealVector(u);
369         for (int i = 0; i < DIM; i++) {
370             v.setEntry(i, 0d);
371         }
372     }
373 
374     @Test(expected = MathUnsupportedOperationException.class)
375     public void testAddToEntry() {
376         RealVector u = createVector();
377         RealVector v = RealVector.unmodifiableRealVector(u);
378         for (int i = 0; i < DIM; i++) {
379             v.addToEntry(i, 0d);
380         }
381     }
382 
383     @Test
384     public void testGetSubVector() {
385         RealVector u = createVector();
386         RealVector v = RealVector.unmodifiableRealVector(u);
387         for (int i = 0; i < DIM; i++) {
388             for (int n = 1; n < DIM - i; n++) {
389                 RealVector exp = u.getSubVector(i, n);
390                 RealVector act = v.getSubVector(i, n);
391                 Assert.assertTrue(equals(exp, act));
392             }
393         }
394     }
395 
396     @Test(expected = MathUnsupportedOperationException.class)
397     public void testSetSubVector() {
398         RealVector u = createVector();
399         RealVector v = RealVector.unmodifiableRealVector(u);
400         v.setSubVector(0, new ArrayRealVector());
401     }
402 
403     @Test
404     public void testIterator() {
405         RealVector u = createVector();
406         Iterator<Entry> i = u.iterator();
407         RealVector v = RealVector.unmodifiableRealVector(u.copy());
408         Iterator<Entry> j = v.iterator();
409         boolean flag;
410         while (i.hasNext()) {
411             Assert.assertTrue(j.hasNext());
412             Entry exp = i.next();
413             Entry act = j.next();
414             Assert.assertTrue(equals(exp.getIndex(), act.getIndex()));
415             Assert.assertTrue(equals(exp.getValue(), act.getValue()));
416             exp.setIndex(RANDOM.nextInt(DIM));
417             act.setIndex(RANDOM.nextInt(DIM));
418             flag = false;
419             try {
420                 act.setValue(RANDOM.nextDouble());
421             } catch (MathUnsupportedOperationException e) {
422                 flag = true;
423             }
424             Assert.assertTrue("exception should have been thrown", flag);
425         }
426         Assert.assertFalse(j.hasNext());
427     }
428 
429     @Test
430     public void testSparseIterator() {
431         RealVector u = createVector();
432         Iterator<Entry> i = u.sparseIterator();
433         RealVector v = RealVector.unmodifiableRealVector(u.copy());
434         Iterator<Entry> j = v.sparseIterator();
435         boolean flag;
436         while (i.hasNext()) {
437             Assert.assertTrue(j.hasNext());
438             Entry exp = i.next();
439             Entry act = j.next();
440             Assert.assertTrue(equals(exp.getIndex(), act.getIndex()));
441             Assert.assertTrue(equals(exp.getValue(), act.getValue()));
442             exp.setIndex(RANDOM.nextInt(DIM));
443             act.setIndex(RANDOM.nextInt(DIM));
444             flag = false;
445             try {
446                 act.setValue(RANDOM.nextDouble());
447             } catch (MathUnsupportedOperationException e) {
448                 flag = true;
449             }
450             Assert.assertTrue("exception should have been thrown", flag);
451         }
452         Assert.assertFalse(j.hasNext());
453     }
454 }