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.linear;
23
24 import java.io.Serializable;
25 import java.lang.reflect.Array;
26 import java.util.Arrays;
27
28 import org.hipparchus.Field;
29 import org.hipparchus.FieldElement;
30 import org.hipparchus.UnitTestUtils;
31 import org.hipparchus.exception.MathIllegalArgumentException;
32 import org.hipparchus.fraction.Fraction;
33 import org.hipparchus.fraction.FractionField;
34 import org.junit.Assert;
35 import org.junit.Test;
36
37
38
39
40
41 public class ArrayFieldVectorTest {
42
43
44 protected Fraction[][] ma1 = {
45 {new Fraction(1), new Fraction(2), new Fraction(3)},
46 {new Fraction(4), new Fraction(5), new Fraction(6)},
47 {new Fraction(7), new Fraction(8), new Fraction(9)}
48 };
49 protected Fraction[] vec1 = {new Fraction(1), new Fraction(2), new Fraction(3)};
50 protected Fraction[] vec2 = {new Fraction(4), new Fraction(5), new Fraction(6)};
51 protected Fraction[] vec3 = {new Fraction(7), new Fraction(8), new Fraction(9)};
52 protected Fraction[] vec4 = { new Fraction(1), new Fraction(2), new Fraction(3),
53 new Fraction(4), new Fraction(5), new Fraction(6),
54 new Fraction(7), new Fraction(8), new Fraction(9)};
55 protected Fraction[] vec_null = {Fraction.ZERO, Fraction.ZERO, Fraction.ZERO};
56 protected Fraction[] dvec1 = {new Fraction(1), new Fraction(2), new Fraction(3),
57 new Fraction(4), new Fraction(5), new Fraction(6),
58 new Fraction(7), new Fraction(8), new Fraction(9)};
59 protected Fraction[][] mat1 = {
60 {new Fraction(1), new Fraction(2), new Fraction(3)},
61 {new Fraction(4), new Fraction(5), new Fraction(6)},
62 {new Fraction(7), new Fraction(8), new Fraction(9)}
63 };
64
65
66
67 public static class FieldVectorTestImpl<T extends FieldElement<T>>
68 implements FieldVector<T>, Serializable {
69
70 private static final long serialVersionUID = 3970959016014158539L;
71
72 private final Field<T> field;
73
74
75 protected T[] data;
76
77
78
79
80
81 @SuppressWarnings("unchecked")
82 private T[] buildArray(final int length) {
83 return (T[]) Array.newInstance(field.getRuntimeClass(), length);
84 }
85
86 public FieldVectorTestImpl(T[] d) {
87 field = d[0].getField();
88 data = d.clone();
89 }
90
91 public Field<T> getField() {
92 return field;
93 }
94
95 private UnsupportedOperationException unsupported() {
96 return new UnsupportedOperationException("Not supported, unneeded for test purposes");
97 }
98
99 public FieldVector<T> copy() {
100 throw unsupported();
101 }
102
103 public FieldVector<T> add(FieldVector<T> v) {
104 throw unsupported();
105 }
106
107 public FieldVector<T> add(T[] v) {
108 throw unsupported();
109 }
110
111 public FieldVector<T> subtract(FieldVector<T> v) {
112 throw unsupported();
113 }
114
115 public FieldVector<T> subtract(T[] v) {
116 throw unsupported();
117 }
118
119 public FieldVector<T> mapAdd(T d) {
120 throw unsupported();
121 }
122
123 public FieldVector<T> mapAddToSelf(T d) {
124 throw unsupported();
125 }
126
127 public FieldVector<T> mapSubtract(T d) {
128 throw unsupported();
129 }
130
131 public FieldVector<T> mapSubtractToSelf(T d) {
132 throw unsupported();
133 }
134
135 public FieldVector<T> mapMultiply(T d) {
136 T[] out = buildArray(data.length);
137 for (int i = 0; i < data.length; i++) {
138 out[i] = data[i].multiply(d);
139 }
140 return new FieldVectorTestImpl<T>(out);
141 }
142
143 public FieldVector<T> mapMultiplyToSelf(T d) {
144 throw unsupported();
145 }
146
147 public FieldVector<T> mapDivide(T d) {
148 throw unsupported();
149 }
150
151 public FieldVector<T> mapDivideToSelf(T d) {
152 throw unsupported();
153 }
154
155 public FieldVector<T> mapInv() {
156 throw unsupported();
157 }
158
159 public FieldVector<T> mapInvToSelf() {
160 throw unsupported();
161 }
162
163 public FieldVector<T> ebeMultiply(FieldVector<T> v) {
164 throw unsupported();
165 }
166
167 public FieldVector<T> ebeMultiply(T[] v) {
168 throw unsupported();
169 }
170
171 public FieldVector<T> ebeDivide(FieldVector<T> v) {
172 throw unsupported();
173 }
174
175 public FieldVector<T> ebeDivide(T[] v) {
176 throw unsupported();
177 }
178
179 public T[] getData() {
180 return data.clone();
181 }
182
183 public T dotProduct(FieldVector<T> v) {
184 T dot = field.getZero();
185 for (int i = 0; i < data.length; i++) {
186 dot = dot.add(data[i].multiply(v.getEntry(i)));
187 }
188 return dot;
189 }
190
191 public T dotProduct(T[] v) {
192 T dot = field.getZero();
193 for (int i = 0; i < data.length; i++) {
194 dot = dot.add(data[i].multiply(v[i]));
195 }
196 return dot;
197 }
198
199 public FieldVector<T> projection(FieldVector<T> v) {
200 throw unsupported();
201 }
202
203 public FieldVector<T> projection(T[] v) {
204 throw unsupported();
205 }
206
207 public FieldMatrix<T> outerProduct(FieldVector<T> v) {
208 throw unsupported();
209 }
210
211 public FieldMatrix<T> outerProduct(T[] v) {
212 throw unsupported();
213 }
214
215 public T getEntry(int index) {
216 return data[index];
217 }
218
219 public int getDimension() {
220 return data.length;
221 }
222
223 public FieldVector<T> append(FieldVector<T> v) {
224 throw unsupported();
225 }
226
227 public FieldVector<T> append(T d) {
228 throw unsupported();
229 }
230
231 public FieldVector<T> append(T[] a) {
232 throw unsupported();
233 }
234
235 public FieldVector<T> getSubVector(int index, int n) {
236 throw unsupported();
237 }
238
239 public void setEntry(int index, T value) {
240 throw unsupported();
241 }
242
243 public void setSubVector(int index, FieldVector<T> v) {
244 throw unsupported();
245 }
246
247 public void setSubVector(int index, T[] v) {
248 throw unsupported();
249 }
250
251 public void set(T value) {
252 throw unsupported();
253 }
254
255 public T[] toArray() {
256 return data.clone();
257 }
258
259 }
260
261 @Test
262 public void testConstructors() {
263
264 ArrayFieldVector<Fraction> v0 = new ArrayFieldVector<Fraction>(FractionField.getInstance());
265 Assert.assertEquals(0, v0.getDimension());
266
267 ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<Fraction>(FractionField.getInstance(), 7);
268 Assert.assertEquals(7, v1.getDimension());
269 Assert.assertEquals(Fraction.ZERO, v1.getEntry(6));
270
271 ArrayFieldVector<Fraction> v2 = new ArrayFieldVector<Fraction>(5, new Fraction(123, 100));
272 Assert.assertEquals(5, v2.getDimension());
273 Assert.assertEquals(new Fraction(123, 100), v2.getEntry(4));
274
275 ArrayFieldVector<Fraction> v3 = new ArrayFieldVector<Fraction>(FractionField.getInstance(), vec1);
276 Assert.assertEquals(3, v3.getDimension());
277 Assert.assertEquals(new Fraction(2), v3.getEntry(1));
278
279 ArrayFieldVector<Fraction> v4 = new ArrayFieldVector<Fraction>(FractionField.getInstance(), vec4, 3, 2);
280 Assert.assertEquals(2, v4.getDimension());
281 Assert.assertEquals(new Fraction(4), v4.getEntry(0));
282 try {
283 new ArrayFieldVector<Fraction>(vec4, 8, 3);
284 Assert.fail("MathIllegalArgumentException expected");
285 } catch (MathIllegalArgumentException ex) {
286
287 }
288
289 FieldVector<Fraction> v5_i = new ArrayFieldVector<Fraction>(dvec1);
290 Assert.assertEquals(9, v5_i.getDimension());
291 Assert.assertEquals(new Fraction(9), v5_i.getEntry(8));
292
293 ArrayFieldVector<Fraction> v5 = new ArrayFieldVector<Fraction>(dvec1);
294 Assert.assertEquals(9, v5.getDimension());
295 Assert.assertEquals(new Fraction(9), v5.getEntry(8));
296
297 ArrayFieldVector<Fraction> v6 = new ArrayFieldVector<Fraction>(dvec1, 3, 2);
298 Assert.assertEquals(2, v6.getDimension());
299 Assert.assertEquals(new Fraction(4), v6.getEntry(0));
300 try {
301 new ArrayFieldVector<Fraction>(dvec1, 8, 3);
302 Assert.fail("MathIllegalArgumentException expected");
303 } catch (MathIllegalArgumentException ex) {
304
305 }
306
307 ArrayFieldVector<Fraction> v7 = new ArrayFieldVector<Fraction>(v1);
308 Assert.assertEquals(7, v7.getDimension());
309 Assert.assertEquals(Fraction.ZERO, v7.getEntry(6));
310
311 FieldVectorTestImpl<Fraction> v7_i = new FieldVectorTestImpl<Fraction>(vec1);
312
313 ArrayFieldVector<Fraction> v7_2 = new ArrayFieldVector<Fraction>(v7_i);
314 Assert.assertEquals(3, v7_2.getDimension());
315 Assert.assertEquals(new Fraction(2), v7_2.getEntry(1));
316
317 ArrayFieldVector<Fraction> v8 = new ArrayFieldVector<Fraction>(v1, true);
318 Assert.assertEquals(7, v8.getDimension());
319 Assert.assertEquals(Fraction.ZERO, v8.getEntry(6));
320 Assert.assertNotSame("testData not same object ", v1.getDataRef(), v8.getDataRef());
321
322 ArrayFieldVector<Fraction> v8_2 = new ArrayFieldVector<Fraction>(v1, false);
323 Assert.assertEquals(7, v8_2.getDimension());
324 Assert.assertEquals(Fraction.ZERO, v8_2.getEntry(6));
325 Assert.assertArrayEquals(v1.getDataRef(), v8_2.getDataRef());
326
327 ArrayFieldVector<Fraction> v9 = new ArrayFieldVector<Fraction>((FieldVector<Fraction>) v1, (FieldVector<Fraction>) v3);
328 Assert.assertEquals(10, v9.getDimension());
329 Assert.assertEquals(new Fraction(1), v9.getEntry(7));
330
331 }
332
333 @Test
334 public void testDataInOut() {
335
336 ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<Fraction>(vec1);
337 ArrayFieldVector<Fraction> v2 = new ArrayFieldVector<Fraction>(vec2);
338 ArrayFieldVector<Fraction> v4 = new ArrayFieldVector<Fraction>(vec4);
339 FieldVectorTestImpl<Fraction> v2_t = new FieldVectorTestImpl<Fraction>(vec2);
340
341 FieldVector<Fraction> v_append_1 = v1.append(v2);
342 Assert.assertEquals(6, v_append_1.getDimension());
343 Assert.assertEquals(new Fraction(4), v_append_1.getEntry(3));
344
345 FieldVector<Fraction> v_append_2 = v1.append(new Fraction(2));
346 Assert.assertEquals(4, v_append_2.getDimension());
347 Assert.assertEquals(new Fraction(2), v_append_2.getEntry(3));
348
349 FieldVector<Fraction> v_append_4 = v1.append(v2_t);
350 Assert.assertEquals(6, v_append_4.getDimension());
351 Assert.assertEquals(new Fraction(4), v_append_4.getEntry(3));
352
353 FieldVector<Fraction> v_copy = v1.copy();
354 Assert.assertEquals(3, v_copy.getDimension());
355 Assert.assertNotSame("testData not same object ", v1.getDataRef(), v_copy.toArray());
356
357 Fraction[] a_frac = v1.toArray();
358 Assert.assertEquals(3, a_frac.length);
359 Assert.assertNotSame("testData not same object ", v1.getDataRef(), a_frac);
360
361
362
363
364
365
366
367 FieldVector<Fraction> vout5 = v4.getSubVector(3, 3);
368 Assert.assertEquals(3, vout5.getDimension());
369 Assert.assertEquals(new Fraction(5), vout5.getEntry(1));
370 try {
371 v4.getSubVector(3, 7);
372 Assert.fail("MathIllegalArgumentException expected");
373 } catch (MathIllegalArgumentException ex) {
374
375 }
376
377 ArrayFieldVector<Fraction> v_set1 = (ArrayFieldVector<Fraction>) v1.copy();
378 v_set1.setEntry(1, new Fraction(11));
379 Assert.assertEquals(new Fraction(11), v_set1.getEntry(1));
380 try {
381 v_set1.setEntry(3, new Fraction(11));
382 Assert.fail("MathIllegalArgumentException expected");
383 } catch (MathIllegalArgumentException ex) {
384
385 }
386
387 ArrayFieldVector<Fraction> v_set2 = (ArrayFieldVector<Fraction>) v4.copy();
388 v_set2.set(3, v1);
389 Assert.assertEquals(new Fraction(1), v_set2.getEntry(3));
390 Assert.assertEquals(new Fraction(7), v_set2.getEntry(6));
391 try {
392 v_set2.set(7, v1);
393 Assert.fail("MathIllegalArgumentException expected");
394 } catch (MathIllegalArgumentException ex) {
395
396 }
397
398 ArrayFieldVector<Fraction> v_set3 = (ArrayFieldVector<Fraction>) v1.copy();
399 v_set3.set(new Fraction(13));
400 Assert.assertEquals(new Fraction(13), v_set3.getEntry(2));
401
402 try {
403 v_set3.getEntry(23);
404 Assert.fail("ArrayIndexOutOfBoundsException expected");
405 } catch (ArrayIndexOutOfBoundsException ex) {
406
407 }
408
409 ArrayFieldVector<Fraction> v_set4 = (ArrayFieldVector<Fraction>) v4.copy();
410 v_set4.setSubVector(3, v2_t);
411 Assert.assertEquals(new Fraction(4), v_set4.getEntry(3));
412 Assert.assertEquals(new Fraction(7), v_set4.getEntry(6));
413 try {
414 v_set4.setSubVector(7, v2_t);
415 Assert.fail("MathIllegalArgumentException expected");
416 } catch (MathIllegalArgumentException ex) {
417
418 }
419
420
421 ArrayFieldVector<Fraction> vout10 = (ArrayFieldVector<Fraction>) v1.copy();
422 ArrayFieldVector<Fraction> vout10_2 = (ArrayFieldVector<Fraction>) v1.copy();
423 Assert.assertEquals(vout10, vout10_2);
424 vout10_2.setEntry(0, new Fraction(11, 10));
425 Assert.assertNotSame(vout10, vout10_2);
426
427 }
428
429 @Test
430 public void testMapFunctions() {
431 ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<Fraction>(vec1);
432
433
434 FieldVector<Fraction> v_mapAdd = v1.mapAdd(new Fraction(2));
435 Fraction[] result_mapAdd = {new Fraction(3), new Fraction(4), new Fraction(5)};
436 checkArray("compare vectors" ,result_mapAdd,v_mapAdd.toArray());
437
438
439 FieldVector<Fraction> v_mapAddToSelf = v1.copy();
440 v_mapAddToSelf.mapAddToSelf(new Fraction(2));
441 Fraction[] result_mapAddToSelf = {new Fraction(3), new Fraction(4), new Fraction(5)};
442 checkArray("compare vectors" ,result_mapAddToSelf,v_mapAddToSelf.toArray());
443
444
445 FieldVector<Fraction> v_mapSubtract = v1.mapSubtract(new Fraction(2));
446 Fraction[] result_mapSubtract = {new Fraction(-1), Fraction.ZERO, new Fraction(1)};
447 checkArray("compare vectors" ,result_mapSubtract,v_mapSubtract.toArray());
448
449
450 FieldVector<Fraction> v_mapSubtractToSelf = v1.copy();
451 v_mapSubtractToSelf.mapSubtractToSelf(new Fraction(2));
452 Fraction[] result_mapSubtractToSelf = {new Fraction(-1), Fraction.ZERO, new Fraction(1)};
453 checkArray("compare vectors" ,result_mapSubtractToSelf,v_mapSubtractToSelf.toArray());
454
455
456 FieldVector<Fraction> v_mapMultiply = v1.mapMultiply(new Fraction(2));
457 Fraction[] result_mapMultiply = {new Fraction(2), new Fraction(4), new Fraction(6)};
458 checkArray("compare vectors" ,result_mapMultiply,v_mapMultiply.toArray());
459
460
461 FieldVector<Fraction> v_mapMultiplyToSelf = v1.copy();
462 v_mapMultiplyToSelf.mapMultiplyToSelf(new Fraction(2));
463 Fraction[] result_mapMultiplyToSelf = {new Fraction(2), new Fraction(4), new Fraction(6)};
464 checkArray("compare vectors" ,result_mapMultiplyToSelf,v_mapMultiplyToSelf.toArray());
465
466
467 FieldVector<Fraction> v_mapDivide = v1.mapDivide(new Fraction(2));
468 Fraction[] result_mapDivide = {new Fraction(1, 2), new Fraction(1), new Fraction(3, 2)};
469 checkArray("compare vectors" ,result_mapDivide,v_mapDivide.toArray());
470
471
472 FieldVector<Fraction> v_mapDivideToSelf = v1.copy();
473 v_mapDivideToSelf.mapDivideToSelf(new Fraction(2));
474 Fraction[] result_mapDivideToSelf = {new Fraction(1, 2), new Fraction(1), new Fraction(3, 2)};
475 checkArray("compare vectors" ,result_mapDivideToSelf,v_mapDivideToSelf.toArray());
476
477
478 FieldVector<Fraction> v_mapInv = v1.mapInv();
479 Fraction[] result_mapInv = {new Fraction(1),new Fraction(1, 2),new Fraction(1, 3)};
480 checkArray("compare vectors" ,result_mapInv,v_mapInv.toArray());
481
482
483 FieldVector<Fraction> v_mapInvToSelf = v1.copy();
484 v_mapInvToSelf.mapInvToSelf();
485 Fraction[] result_mapInvToSelf = {new Fraction(1),new Fraction(1, 2),new Fraction(1, 3)};
486 checkArray("compare vectors" ,result_mapInvToSelf,v_mapInvToSelf.toArray());
487
488 }
489
490 @Test
491 public void testBasicFunctions() {
492 ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<Fraction>(vec1);
493 ArrayFieldVector<Fraction> v2 = new ArrayFieldVector<Fraction>(vec2);
494 new ArrayFieldVector<Fraction>(vec_null);
495
496 FieldVectorTestImpl<Fraction> v2_t = new FieldVectorTestImpl<Fraction>(vec2);
497
498
499 ArrayFieldVector<Fraction> v_add = v1.add(v2);
500 Fraction[] result_add = {new Fraction(5), new Fraction(7), new Fraction(9)};
501 checkArray("compare vect" ,v_add.toArray(),result_add);
502
503 FieldVectorTestImpl<Fraction> vt2 = new FieldVectorTestImpl<Fraction>(vec2);
504 FieldVector<Fraction> v_add_i = v1.add(vt2);
505 Fraction[] result_add_i = {new Fraction(5), new Fraction(7), new Fraction(9)};
506 checkArray("compare vect" ,v_add_i.toArray(),result_add_i);
507
508
509 ArrayFieldVector<Fraction> v_subtract = v1.subtract(v2);
510 Fraction[] result_subtract = {new Fraction(-3), new Fraction(-3), new Fraction(-3)};
511 checkArray("compare vect" ,v_subtract.toArray(),result_subtract);
512
513 FieldVector<Fraction> v_subtract_i = v1.subtract(vt2);
514 Fraction[] result_subtract_i = {new Fraction(-3), new Fraction(-3), new Fraction(-3)};
515 checkArray("compare vect" ,v_subtract_i.toArray(),result_subtract_i);
516
517
518 ArrayFieldVector<Fraction> v_ebeMultiply = v1.ebeMultiply(v2);
519 Fraction[] result_ebeMultiply = {new Fraction(4), new Fraction(10), new Fraction(18)};
520 checkArray("compare vect" ,v_ebeMultiply.toArray(),result_ebeMultiply);
521
522 FieldVector<Fraction> v_ebeMultiply_2 = v1.ebeMultiply(v2_t);
523 Fraction[] result_ebeMultiply_2 = {new Fraction(4), new Fraction(10), new Fraction(18)};
524 checkArray("compare vect" ,v_ebeMultiply_2.toArray(),result_ebeMultiply_2);
525
526
527 ArrayFieldVector<Fraction> v_ebeDivide = v1.ebeDivide(v2);
528 Fraction[] result_ebeDivide = {new Fraction(1, 4), new Fraction(2, 5), new Fraction(1, 2)};
529 checkArray("compare vect" ,v_ebeDivide.toArray(),result_ebeDivide);
530
531 FieldVector<Fraction> v_ebeDivide_2 = v1.ebeDivide(v2_t);
532 Fraction[] result_ebeDivide_2 = {new Fraction(1, 4), new Fraction(2, 5), new Fraction(1, 2)};
533 checkArray("compare vect" ,v_ebeDivide_2.toArray(),result_ebeDivide_2);
534
535
536 Fraction dot = v1.dotProduct(v2);
537 Assert.assertEquals("compare val ",new Fraction(32), dot);
538
539
540 Fraction dot_2 = v1.dotProduct(v2_t);
541 Assert.assertEquals("compare val ",new Fraction(32), dot_2);
542
543 FieldMatrix<Fraction> m_outerProduct = v1.outerProduct(v2);
544 Assert.assertEquals("compare val ",new Fraction(4), m_outerProduct.getEntry(0,0));
545
546 FieldMatrix<Fraction> m_outerProduct_2 = v1.outerProduct(v2_t);
547 Assert.assertEquals("compare val ",new Fraction(4), m_outerProduct_2.getEntry(0,0));
548
549 ArrayFieldVector<Fraction> v_projection = v1.projection(v2);
550 Fraction[] result_projection = {new Fraction(128, 77), new Fraction(160, 77), new Fraction(192, 77)};
551 checkArray("compare vect", v_projection.toArray(), result_projection);
552
553 FieldVector<Fraction> v_projection_2 = v1.projection(v2_t);
554 Fraction[] result_projection_2 = {new Fraction(128, 77), new Fraction(160, 77), new Fraction(192, 77)};
555 checkArray("compare vect", v_projection_2.toArray(), result_projection_2);
556
557 }
558
559 @Test
560 public void testMisc() {
561 ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<Fraction>(vec1);
562 ArrayFieldVector<Fraction> v4 = new ArrayFieldVector<Fraction>(vec4);
563 FieldVector<Fraction> v4_2 = new ArrayFieldVector<Fraction>(vec4);
564
565 Assert.assertEquals("{1; 2; 3}", v1.toString());
566 Assert.assertEquals("{1; 2; 3; 4; 5; 6; 7; 8; 9}", v4.toString());
567
568
569
570
571
572
573 try {
574 v1.checkVectorDimensions(2);
575 Assert.fail("MathIllegalArgumentException expected");
576 } catch (MathIllegalArgumentException ex) {
577
578 }
579
580 try {
581 v1.checkVectorDimensions(v4);
582 Assert.fail("MathIllegalArgumentException expected");
583 } catch (MathIllegalArgumentException ex) {
584
585 }
586
587 try {
588 v1.checkVectorDimensions(v4_2);
589 Assert.fail("MathIllegalArgumentException expected");
590 } catch (MathIllegalArgumentException ex) {
591
592 }
593
594 }
595
596 @Test
597 public void testSerial() {
598 ArrayFieldVector<Fraction> v = new ArrayFieldVector<Fraction>(vec1);
599 Assert.assertEquals(v,UnitTestUtils.serializeAndRecover(v));
600 }
601
602 @Test
603 public void testZeroVectors() {
604
605
606 try {
607 new ArrayFieldVector<Fraction>(new Fraction[0]);
608 Assert.fail("MathIllegalArgumentException expected");
609 } catch (MathIllegalArgumentException ex) {
610
611 }
612 try {
613 new ArrayFieldVector<Fraction>(new Fraction[0], true);
614 Assert.fail("MathIllegalArgumentException expected");
615 } catch (MathIllegalArgumentException ex) {
616
617 }
618 try {
619 new ArrayFieldVector<Fraction>(new Fraction[0], false);
620 Assert.fail("MathIllegalArgumentException expected");
621 } catch (MathIllegalArgumentException ex) {
622
623 }
624
625
626 Assert.assertEquals(0, new ArrayFieldVector<Fraction>(FractionField.getInstance(), new Fraction[0]).getDimension());
627 Assert.assertEquals(0, new ArrayFieldVector<Fraction>(FractionField.getInstance(), new Fraction[0], true).getDimension());
628 Assert.assertEquals(0, new ArrayFieldVector<Fraction>(FractionField.getInstance(), new Fraction[0], false).getDimension());
629
630 }
631
632 @Test
633 public void testOuterProduct() {
634 final ArrayFieldVector<Fraction> u
635 = new ArrayFieldVector<Fraction>(FractionField.getInstance(),
636 new Fraction[] {new Fraction(1),
637 new Fraction(2),
638 new Fraction(-3)});
639 final ArrayFieldVector<Fraction> v
640 = new ArrayFieldVector<Fraction>(FractionField.getInstance(),
641 new Fraction[] {new Fraction(4),
642 new Fraction(-2)});
643
644 final FieldMatrix<Fraction> uv = u.outerProduct(v);
645
646 final double tol = Math.ulp(1d);
647 Assert.assertEquals(new Fraction(4).doubleValue(), uv.getEntry(0, 0).doubleValue(), tol);
648 Assert.assertEquals(new Fraction(-2).doubleValue(), uv.getEntry(0, 1).doubleValue(), tol);
649 Assert.assertEquals(new Fraction(8).doubleValue(), uv.getEntry(1, 0).doubleValue(), tol);
650 Assert.assertEquals(new Fraction(-4).doubleValue(), uv.getEntry(1, 1).doubleValue(), tol);
651 Assert.assertEquals(new Fraction(-12).doubleValue(), uv.getEntry(2, 0).doubleValue(), tol);
652 Assert.assertEquals(new Fraction(6).doubleValue(), uv.getEntry(2, 1).doubleValue(), tol);
653 }
654
655
656 protected void checkArray(String msg, Fraction[] m, Fraction[] n) {
657 if (m.length != n.length) {
658 Assert.fail("vectors have different lengths");
659 }
660 for (int i = 0; i < m.length; i++) {
661 Assert.assertEquals(msg + " " + i + " elements differ", m[i],n[i]);
662 }
663 }
664
665
666
667
668
669
670 @Test
671 public void testWalkInDefaultOrderPreservingVisitor1() {
672 final Fraction[] data = new Fraction[] {
673 Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
674 Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
675 Fraction.ZERO, Fraction.ZERO, new Fraction(3)
676 };
677 final ArrayFieldVector<Fraction> v = new ArrayFieldVector<Fraction>(data);
678 final FieldVectorPreservingVisitor<Fraction> visitor;
679 visitor = new FieldVectorPreservingVisitor<Fraction>() {
680
681 private int expectedIndex;
682
683 public void visit(final int actualIndex, final Fraction actualValue) {
684 Assert.assertEquals(expectedIndex, actualIndex);
685 Assert.assertEquals(Integer.toString(actualIndex),
686 data[actualIndex], actualValue);
687 ++expectedIndex;
688 }
689
690 public void start(final int actualSize, final int actualStart,
691 final int actualEnd) {
692 Assert.assertEquals(data.length, actualSize);
693 Assert.assertEquals(0, actualStart);
694 Assert.assertEquals(data.length - 1, actualEnd);
695 expectedIndex = 0;
696 }
697
698 public Fraction end() {
699 return Fraction.ZERO;
700 }
701 };
702 v.walkInDefaultOrder(visitor);
703 }
704
705
706 @Test
707 public void testWalkInDefaultOrderPreservingVisitor2() {
708 final ArrayFieldVector<Fraction> v = create(5);
709 final FieldVectorPreservingVisitor<Fraction> visitor;
710 visitor = new FieldVectorPreservingVisitor<Fraction>() {
711
712 public void visit(int index, Fraction value) {
713
714 }
715
716 public void start(int dimension, int start, int end) {
717
718 }
719
720 public Fraction end() {
721 return Fraction.ZERO;
722 }
723 };
724 try {
725 v.walkInDefaultOrder(visitor, -1, 4);
726 Assert.fail();
727 } catch (MathIllegalArgumentException e) {
728
729 }
730 try {
731 v.walkInDefaultOrder(visitor, 5, 4);
732 Assert.fail();
733 } catch (MathIllegalArgumentException e) {
734
735 }
736 try {
737 v.walkInDefaultOrder(visitor, 0, -1);
738 Assert.fail();
739 } catch (MathIllegalArgumentException e) {
740
741 }
742 try {
743 v.walkInDefaultOrder(visitor, 0, 5);
744 Assert.fail();
745 } catch (MathIllegalArgumentException e) {
746
747 }
748 try {
749 v.walkInDefaultOrder(visitor, 4, 0);
750 Assert.fail();
751 } catch (MathIllegalArgumentException e) {
752
753 }
754 }
755
756
757 @Test
758 public void testWalkInDefaultOrderPreservingVisitor3() {
759 final Fraction[] data = new Fraction[] {
760 Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
761 Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
762 Fraction.ZERO, Fraction.ZERO, new Fraction(3)
763 };
764 final ArrayFieldVector<Fraction> v = new ArrayFieldVector<Fraction>(data);
765 final int expectedStart = 2;
766 final int expectedEnd = 7;
767 final FieldVectorPreservingVisitor<Fraction> visitor;
768 visitor = new FieldVectorPreservingVisitor<Fraction>() {
769
770 private int expectedIndex;
771
772 public void visit(final int actualIndex, final Fraction actualValue) {
773 Assert.assertEquals(expectedIndex, actualIndex);
774 Assert.assertEquals(Integer.toString(actualIndex),
775 data[actualIndex], actualValue);
776 ++expectedIndex;
777 }
778
779 public void start(final int actualSize, final int actualStart,
780 final int actualEnd) {
781 Assert.assertEquals(data.length, actualSize);
782 Assert.assertEquals(expectedStart, actualStart);
783 Assert.assertEquals(expectedEnd, actualEnd);
784 expectedIndex = expectedStart;
785 }
786
787 public Fraction end() {
788 return Fraction.ZERO;
789 }
790 };
791 v.walkInDefaultOrder(visitor, expectedStart, expectedEnd);
792 }
793
794
795 @Test
796 public void testWalkInOptimizedOrderPreservingVisitor1() {
797 final Fraction[] data = new Fraction[] {
798 Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
799 Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
800 Fraction.ZERO, Fraction.ZERO, new Fraction(3)
801 };
802 final ArrayFieldVector<Fraction> v = new ArrayFieldVector<Fraction>(data);
803 final FieldVectorPreservingVisitor<Fraction> visitor;
804 visitor = new FieldVectorPreservingVisitor<Fraction>() {
805 private final boolean[] visited = new boolean[data.length];
806
807 public void visit(final int actualIndex, final Fraction actualValue) {
808 visited[actualIndex] = true;
809 Assert.assertEquals(Integer.toString(actualIndex),
810 data[actualIndex], actualValue);
811 }
812
813 public void start(final int actualSize, final int actualStart,
814 final int actualEnd) {
815 Assert.assertEquals(data.length, actualSize);
816 Assert.assertEquals(0, actualStart);
817 Assert.assertEquals(data.length - 1, actualEnd);
818 Arrays.fill(visited, false);
819 }
820
821 public Fraction end() {
822 for (int i = 0; i < data.length; i++) {
823 Assert.assertTrue("entry " + i + "has not been visited",
824 visited[i]);
825 }
826 return Fraction.ZERO;
827 }
828 };
829 v.walkInOptimizedOrder(visitor);
830 }
831
832
833 @Test
834 public void testWalkInOptimizedOrderPreservingVisitor2() {
835 final ArrayFieldVector<Fraction> v = create(5);
836 final FieldVectorPreservingVisitor<Fraction> visitor;
837 visitor = new FieldVectorPreservingVisitor<Fraction>() {
838
839 public void visit(int index, Fraction value) {
840
841 }
842
843 public void start(int dimension, int start, int end) {
844
845 }
846
847 public Fraction end() {
848 return Fraction.ZERO;
849 }
850 };
851 try {
852 v.walkInOptimizedOrder(visitor, -1, 4);
853 Assert.fail();
854 } catch (MathIllegalArgumentException e) {
855
856 }
857 try {
858 v.walkInOptimizedOrder(visitor, 5, 4);
859 Assert.fail();
860 } catch (MathIllegalArgumentException e) {
861
862 }
863 try {
864 v.walkInOptimizedOrder(visitor, 0, -1);
865 Assert.fail();
866 } catch (MathIllegalArgumentException e) {
867
868 }
869 try {
870 v.walkInOptimizedOrder(visitor, 0, 5);
871 Assert.fail();
872 } catch (MathIllegalArgumentException e) {
873
874 }
875 try {
876 v.walkInOptimizedOrder(visitor, 4, 0);
877 Assert.fail();
878 } catch (MathIllegalArgumentException e) {
879
880 }
881 }
882
883
884 @Test
885 public void testWalkInOptimizedOrderPreservingVisitor3() {
886 final Fraction[] data = new Fraction[] {
887 Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
888 Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
889 Fraction.ZERO, Fraction.ZERO, new Fraction(3)
890 };
891 final ArrayFieldVector<Fraction> v = new ArrayFieldVector<Fraction>(data);
892 final int expectedStart = 2;
893 final int expectedEnd = 7;
894 final FieldVectorPreservingVisitor<Fraction> visitor;
895 visitor = new FieldVectorPreservingVisitor<Fraction>() {
896 private final boolean[] visited = new boolean[data.length];
897
898 public void visit(final int actualIndex, final Fraction actualValue) {
899 Assert.assertEquals(Integer.toString(actualIndex),
900 data[actualIndex], actualValue);
901 visited[actualIndex] = true;
902 }
903
904 public void start(final int actualSize, final int actualStart,
905 final int actualEnd) {
906 Assert.assertEquals(data.length, actualSize);
907 Assert.assertEquals(expectedStart, actualStart);
908 Assert.assertEquals(expectedEnd, actualEnd);
909 Arrays.fill(visited, true);
910 }
911
912 public Fraction end() {
913 for (int i = expectedStart; i <= expectedEnd; i++) {
914 Assert.assertTrue("entry " + i + "has not been visited",
915 visited[i]);
916 }
917 return Fraction.ZERO;
918 }
919 };
920 v.walkInOptimizedOrder(visitor, expectedStart, expectedEnd);
921 }
922
923
924 @Test
925 public void testWalkInDefaultOrderChangingVisitor1() {
926 final Fraction[] data = new Fraction[] {
927 Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
928 Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
929 Fraction.ZERO, Fraction.ZERO, new Fraction(3)
930 };
931 final ArrayFieldVector<Fraction> v = new ArrayFieldVector<Fraction>(data);
932 final FieldVectorChangingVisitor<Fraction> visitor;
933 visitor = new FieldVectorChangingVisitor<Fraction>() {
934
935 private int expectedIndex;
936
937 public Fraction visit(final int actualIndex, final Fraction actualValue) {
938 Assert.assertEquals(expectedIndex, actualIndex);
939 Assert.assertEquals(Integer.toString(actualIndex),
940 data[actualIndex], actualValue);
941 ++expectedIndex;
942 return actualValue.add(actualIndex);
943 }
944
945 public void start(final int actualSize, final int actualStart,
946 final int actualEnd) {
947 Assert.assertEquals(data.length, actualSize);
948 Assert.assertEquals(0, actualStart);
949 Assert.assertEquals(data.length - 1, actualEnd);
950 expectedIndex = 0;
951 }
952
953 public Fraction end() {
954 return Fraction.ZERO;
955 }
956 };
957 v.walkInDefaultOrder(visitor);
958 for (int i = 0; i < data.length; i++) {
959 Assert.assertEquals("entry " + i, data[i].add(i), v.getEntry(i));
960 }
961 }
962
963
964 @Test
965 public void testWalkInDefaultOrderChangingVisitor2() {
966 final ArrayFieldVector<Fraction> v = create(5);
967 final FieldVectorChangingVisitor<Fraction> visitor;
968 visitor = new FieldVectorChangingVisitor<Fraction>() {
969
970 public Fraction visit(int index, Fraction value) {
971 return Fraction.ZERO;
972 }
973
974 public void start(int dimension, int start, int end) {
975
976 }
977
978 public Fraction end() {
979 return Fraction.ZERO;
980 }
981 };
982 try {
983 v.walkInDefaultOrder(visitor, -1, 4);
984 Assert.fail();
985 } catch (MathIllegalArgumentException e) {
986
987 }
988 try {
989 v.walkInDefaultOrder(visitor, 5, 4);
990 Assert.fail();
991 } catch (MathIllegalArgumentException e) {
992
993 }
994 try {
995 v.walkInDefaultOrder(visitor, 0, -1);
996 Assert.fail();
997 } catch (MathIllegalArgumentException e) {
998
999 }
1000 try {
1001 v.walkInDefaultOrder(visitor, 0, 5);
1002 Assert.fail();
1003 } catch (MathIllegalArgumentException e) {
1004
1005 }
1006 try {
1007 v.walkInDefaultOrder(visitor, 4, 0);
1008 Assert.fail();
1009 } catch (MathIllegalArgumentException e) {
1010
1011 }
1012 }
1013
1014
1015 @Test
1016 public void testWalkInDefaultOrderChangingVisitor3() {
1017 final Fraction[] data = new Fraction[] {
1018 Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
1019 Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
1020 Fraction.ZERO, Fraction.ZERO, new Fraction(3)
1021 };
1022 final ArrayFieldVector<Fraction> v = new ArrayFieldVector<Fraction>(data);
1023 final int expectedStart = 2;
1024 final int expectedEnd = 7;
1025 final FieldVectorChangingVisitor<Fraction> visitor;
1026 visitor = new FieldVectorChangingVisitor<Fraction>() {
1027
1028 private int expectedIndex;
1029
1030 public Fraction visit(final int actualIndex, final Fraction actualValue) {
1031 Assert.assertEquals(expectedIndex, actualIndex);
1032 Assert.assertEquals(Integer.toString(actualIndex),
1033 data[actualIndex], actualValue);
1034 ++expectedIndex;
1035 return actualValue.add(actualIndex);
1036 }
1037
1038 public void start(final int actualSize, final int actualStart,
1039 final int actualEnd) {
1040 Assert.assertEquals(data.length, actualSize);
1041 Assert.assertEquals(expectedStart, actualStart);
1042 Assert.assertEquals(expectedEnd, actualEnd);
1043 expectedIndex = expectedStart;
1044 }
1045
1046 public Fraction end() {
1047 return Fraction.ZERO;
1048 }
1049 };
1050 v.walkInDefaultOrder(visitor, expectedStart, expectedEnd);
1051 for (int i = expectedStart; i <= expectedEnd; i++) {
1052 Assert.assertEquals("entry " + i, data[i].add(i), v.getEntry(i));
1053 }
1054 }
1055
1056
1057 @Test
1058 public void testWalkInOptimizedOrderChangingVisitor1() {
1059 final Fraction[] data = new Fraction[] {
1060 Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
1061 Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
1062 Fraction.ZERO, Fraction.ZERO, new Fraction(3)
1063 };
1064 final ArrayFieldVector<Fraction> v = new ArrayFieldVector<Fraction>(data);
1065 final FieldVectorChangingVisitor<Fraction> visitor;
1066 visitor = new FieldVectorChangingVisitor<Fraction>() {
1067 private final boolean[] visited = new boolean[data.length];
1068
1069 public Fraction visit(final int actualIndex, final Fraction actualValue) {
1070 visited[actualIndex] = true;
1071 Assert.assertEquals(Integer.toString(actualIndex),
1072 data[actualIndex], actualValue);
1073 return actualValue.add(actualIndex);
1074 }
1075
1076 public void start(final int actualSize, final int actualStart,
1077 final int actualEnd) {
1078 Assert.assertEquals(data.length, actualSize);
1079 Assert.assertEquals(0, actualStart);
1080 Assert.assertEquals(data.length - 1, actualEnd);
1081 Arrays.fill(visited, false);
1082 }
1083
1084 public Fraction end() {
1085 for (int i = 0; i < data.length; i++) {
1086 Assert.assertTrue("entry " + i + "has not been visited",
1087 visited[i]);
1088 }
1089 return Fraction.ZERO;
1090 }
1091 };
1092 v.walkInOptimizedOrder(visitor);
1093 for (int i = 0; i < data.length; i++) {
1094 Assert.assertEquals("entry " + i, data[i].add(i), v.getEntry(i));
1095 }
1096 }
1097
1098
1099 @Test
1100 public void testWalkInOptimizedOrderChangingVisitor2() {
1101 final ArrayFieldVector<Fraction> v = create(5);
1102 final FieldVectorChangingVisitor<Fraction> visitor;
1103 visitor = new FieldVectorChangingVisitor<Fraction>() {
1104
1105 public Fraction visit(int index, Fraction value) {
1106 return Fraction.ZERO;
1107 }
1108
1109 public void start(int dimension, int start, int end) {
1110
1111 }
1112
1113 public Fraction end() {
1114 return Fraction.ZERO;
1115 }
1116 };
1117 try {
1118 v.walkInOptimizedOrder(visitor, -1, 4);
1119 Assert.fail();
1120 } catch (MathIllegalArgumentException e) {
1121
1122 }
1123 try {
1124 v.walkInOptimizedOrder(visitor, 5, 4);
1125 Assert.fail();
1126 } catch (MathIllegalArgumentException e) {
1127
1128 }
1129 try {
1130 v.walkInOptimizedOrder(visitor, 0, -1);
1131 Assert.fail();
1132 } catch (MathIllegalArgumentException e) {
1133
1134 }
1135 try {
1136 v.walkInOptimizedOrder(visitor, 0, 5);
1137 Assert.fail();
1138 } catch (MathIllegalArgumentException e) {
1139
1140 }
1141 try {
1142 v.walkInOptimizedOrder(visitor, 4, 0);
1143 Assert.fail();
1144 } catch (MathIllegalArgumentException e) {
1145
1146 }
1147 }
1148
1149
1150 @Test
1151 public void testWalkInOptimizedOrderChangingVisitor3() {
1152 final Fraction[] data = new Fraction[] {
1153 Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
1154 Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
1155 Fraction.ZERO, Fraction.ZERO, new Fraction(3)
1156 };
1157 final ArrayFieldVector<Fraction> v = new ArrayFieldVector<Fraction>(data);
1158 final int expectedStart = 2;
1159 final int expectedEnd = 7;
1160 final FieldVectorChangingVisitor<Fraction> visitor;
1161 visitor = new FieldVectorChangingVisitor<Fraction>() {
1162 private final boolean[] visited = new boolean[data.length];
1163
1164 public Fraction visit(final int actualIndex, final Fraction actualValue) {
1165 Assert.assertEquals(Integer.toString(actualIndex),
1166 data[actualIndex], actualValue);
1167 visited[actualIndex] = true;
1168 return actualValue.add(actualIndex);
1169 }
1170
1171 public void start(final int actualSize, final int actualStart,
1172 final int actualEnd) {
1173 Assert.assertEquals(data.length, actualSize);
1174 Assert.assertEquals(expectedStart, actualStart);
1175 Assert.assertEquals(expectedEnd, actualEnd);
1176 Arrays.fill(visited, true);
1177 }
1178
1179 public Fraction end() {
1180 for (int i = expectedStart; i <= expectedEnd; i++) {
1181 Assert.assertTrue("entry " + i + "has not been visited",
1182 visited[i]);
1183 }
1184 return Fraction.ZERO;
1185 }
1186 };
1187 v.walkInOptimizedOrder(visitor, expectedStart, expectedEnd);
1188 for (int i = expectedStart; i <= expectedEnd; i++) {
1189 Assert.assertEquals("entry " + i, data[i].add(i), v.getEntry(i));
1190 }
1191 }
1192
1193 private ArrayFieldVector<Fraction> create(int n) {
1194 Fraction[] t = new Fraction[n];
1195 for (int i = 0; i < n; ++i) {
1196 t[i] = Fraction.ZERO;
1197 }
1198 return new ArrayFieldVector<Fraction>(t);
1199 }
1200 }