1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.linear;
18
19 import java.io.Serializable;
20
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 import org.apache.commons.math.TestUtils;
26
27
28
29
30
31
32 public class ArrayRealVectorTest extends TestCase {
33
34
35 protected double[][] ma1 = {{1d, 2d, 3d}, {4d, 5d, 6d}, {7d, 8d, 9d}};
36 protected double[] vec1 = {1d, 2d, 3d};
37 protected double[] vec2 = {4d, 5d, 6d};
38 protected double[] vec3 = {7d, 8d, 9d};
39 protected double[] vec4 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d};
40 protected double[] vec_null = {0d, 0d, 0d};
41 protected Double[] dvec1 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d};
42 protected double[][] mat1 = {{1d, 2d, 3d}, {4d, 5d, 6d},{ 7d, 8d, 9d}};
43
44
45 protected double entryTolerance = 10E-16;
46 protected double normTolerance = 10E-14;
47
48
49
50 public static class RealVectorTestImpl implements RealVector, Serializable {
51
52
53 private static final long serialVersionUID = 4715341047369582908L;
54
55
56 protected double data[];
57
58 public RealVectorTestImpl(double[] d) {
59 data = d.clone();
60 }
61
62 private UnsupportedOperationException unsupported() {
63 return new UnsupportedOperationException("Not supported, unneeded for test purposes");
64 }
65
66 public RealVector copy() {
67 throw unsupported();
68 }
69
70 public RealVector add(RealVector v) throws IllegalArgumentException {
71 throw unsupported();
72 }
73
74 public RealVector add(double[] v) throws IllegalArgumentException {
75 throw unsupported();
76 }
77
78 public RealVector subtract(RealVector v) throws IllegalArgumentException {
79 throw unsupported();
80 }
81
82 public RealVector subtract(double[] v) throws IllegalArgumentException {
83 throw unsupported();
84 }
85
86 public RealVector mapAdd(double d) {
87 throw unsupported();
88 }
89
90 public RealVector mapAddToSelf(double d) {
91 throw unsupported();
92 }
93
94 public RealVector mapSubtract(double d) {
95 throw unsupported();
96 }
97
98 public RealVector mapSubtractToSelf(double d) {
99 throw unsupported();
100 }
101
102 public RealVector mapMultiply(double d) {
103 double[] out = new double[data.length];
104 for (int i = 0; i < data.length; i++) {
105 out[i] = data[i] * d;
106 }
107 return new ArrayRealVector(out);
108 }
109
110 public RealVector mapMultiplyToSelf(double d) {
111 throw unsupported();
112 }
113
114 public RealVector mapDivide(double d) {
115 throw unsupported();
116 }
117
118 public RealVector mapDivideToSelf(double d) {
119 throw unsupported();
120 }
121
122 public RealVector mapPow(double d) {
123 throw unsupported();
124 }
125
126 public RealVector mapPowToSelf(double d) {
127 throw unsupported();
128 }
129
130 public RealVector mapExp() {
131 throw unsupported();
132 }
133
134 public RealVector mapExpToSelf() {
135 throw unsupported();
136 }
137
138 public RealVector mapExpm1() {
139 throw unsupported();
140 }
141
142 public RealVector mapExpm1ToSelf() {
143 throw unsupported();
144 }
145
146 public RealVector mapLog() {
147 throw unsupported();
148 }
149
150 public RealVector mapLogToSelf() {
151 throw unsupported();
152 }
153
154 public RealVector mapLog10() {
155 throw unsupported();
156 }
157
158 public RealVector mapLog10ToSelf() {
159 throw unsupported();
160 }
161
162 public RealVector mapLog1p() {
163 throw unsupported();
164 }
165
166 public RealVector mapLog1pToSelf() {
167 throw unsupported();
168 }
169
170 public RealVector mapCosh() {
171 throw unsupported();
172 }
173
174 public RealVector mapCoshToSelf() {
175 throw unsupported();
176 }
177
178 public RealVector mapSinh() {
179 throw unsupported();
180 }
181
182 public RealVector mapSinhToSelf() {
183 throw unsupported();
184 }
185
186 public RealVector mapTanh() {
187 throw unsupported();
188 }
189
190 public RealVector mapTanhToSelf() {
191 throw unsupported();
192 }
193
194 public RealVector mapCos() {
195 throw unsupported();
196 }
197
198 public RealVector mapCosToSelf() {
199 throw unsupported();
200 }
201
202 public RealVector mapSin() {
203 throw unsupported();
204 }
205
206 public RealVector mapSinToSelf() {
207 throw unsupported();
208 }
209
210 public RealVector mapTan() {
211 throw unsupported();
212 }
213
214 public RealVector mapTanToSelf() {
215 throw unsupported();
216 }
217
218 public RealVector mapAcos() {
219 throw unsupported();
220 }
221
222 public RealVector mapAcosToSelf() {
223 throw unsupported();
224 }
225
226 public RealVector mapAsin() {
227 throw unsupported();
228 }
229
230 public RealVector mapAsinToSelf() {
231 throw unsupported();
232 }
233
234 public RealVector mapAtan() {
235 throw unsupported();
236 }
237
238 public RealVector mapAtanToSelf() {
239 throw unsupported();
240 }
241
242 public RealVector mapInv() {
243 throw unsupported();
244 }
245
246 public RealVector mapInvToSelf() {
247 throw unsupported();
248 }
249
250 public RealVector mapAbs() {
251 throw unsupported();
252 }
253
254 public RealVector mapAbsToSelf() {
255 throw unsupported();
256 }
257
258 public RealVector mapSqrt() {
259 throw unsupported();
260 }
261
262 public RealVector mapSqrtToSelf() {
263 throw unsupported();
264 }
265
266 public RealVector mapCbrt() {
267 throw unsupported();
268 }
269
270 public RealVector mapCbrtToSelf() {
271 throw unsupported();
272 }
273
274 public RealVector mapCeil() {
275 throw unsupported();
276 }
277
278 public RealVector mapCeilToSelf() {
279 throw unsupported();
280 }
281
282 public RealVector mapFloor() {
283 throw unsupported();
284 }
285
286 public RealVector mapFloorToSelf() {
287 throw unsupported();
288 }
289
290 public RealVector mapRint() {
291 throw unsupported();
292 }
293
294 public RealVector mapRintToSelf() {
295 throw unsupported();
296 }
297
298 public RealVector mapSignum() {
299 throw unsupported();
300 }
301
302 public RealVector mapSignumToSelf() {
303 throw unsupported();
304 }
305
306 public RealVector mapUlp() {
307 throw unsupported();
308 }
309
310 public RealVector mapUlpToSelf() {
311 throw unsupported();
312 }
313
314 public RealVector ebeMultiply(RealVector v) throws IllegalArgumentException {
315 throw unsupported();
316 }
317
318 public RealVector ebeMultiply(double[] v) throws IllegalArgumentException {
319 throw unsupported();
320 }
321
322 public RealVector ebeDivide(RealVector v) throws IllegalArgumentException {
323 throw unsupported();
324 }
325
326 public RealVector ebeDivide(double[] v) throws IllegalArgumentException {
327 throw unsupported();
328 }
329
330 public double[] getData() {
331 return data.clone();
332 }
333
334 public double dotProduct(RealVector v) throws IllegalArgumentException {
335 double dot = 0;
336 for (int i = 0; i < data.length; i++) {
337 dot += data[i] * v.getEntry(i);
338 }
339 return dot;
340 }
341
342 public double dotProduct(double[] v) throws IllegalArgumentException {
343 double dot = 0;
344 for (int i = 0; i < data.length; i++) {
345 dot += data[i] * v[i];
346 }
347 return dot;
348 }
349
350 public double getNorm() {
351 throw unsupported();
352 }
353
354 public double getL1Norm() {
355 throw unsupported();
356 }
357
358 public double getLInfNorm() {
359 throw unsupported();
360 }
361
362 public double getDistance(RealVector v) throws IllegalArgumentException {
363 throw unsupported();
364 }
365
366 public double getDistance(double[] v) throws IllegalArgumentException {
367 throw unsupported();
368 }
369
370 public double getL1Distance(RealVector v) throws IllegalArgumentException {
371 throw unsupported();
372 }
373
374 public double getL1Distance(double[] v) throws IllegalArgumentException {
375 throw unsupported();
376 }
377
378 public double getLInfDistance(RealVector v) throws IllegalArgumentException {
379 throw unsupported();
380 }
381
382 public double getLInfDistance(double[] v) throws IllegalArgumentException {
383 throw unsupported();
384 }
385
386 public RealVector unitVector() {
387 throw unsupported();
388 }
389
390 public void unitize() {
391 throw unsupported();
392 }
393
394 public RealVector projection(RealVector v) throws IllegalArgumentException {
395 throw unsupported();
396 }
397
398 public RealVector projection(double[] v) throws IllegalArgumentException {
399 throw unsupported();
400 }
401
402 public RealMatrix outerProduct(RealVector v) throws IllegalArgumentException {
403 throw unsupported();
404 }
405
406 public RealMatrix outerProduct(double[] v) throws IllegalArgumentException {
407 throw unsupported();
408 }
409
410 public double getEntry(int index) throws MatrixIndexException {
411 return data[index];
412 }
413
414 public int getDimension() {
415 return data.length;
416 }
417
418 public RealVector append(RealVector v) {
419 throw unsupported();
420 }
421
422 public RealVector append(double d) {
423 throw unsupported();
424 }
425
426 public RealVector append(double[] a) {
427 throw unsupported();
428 }
429
430 public RealVector getSubVector(int index, int n) throws MatrixIndexException {
431 throw unsupported();
432 }
433
434 public void setEntry(int index, double value) throws MatrixIndexException {
435 throw unsupported();
436 }
437
438 public void setSubVector(int index, RealVector v) throws MatrixIndexException {
439 throw unsupported();
440 }
441
442 public void setSubVector(int index, double[] v) throws MatrixIndexException {
443 throw unsupported();
444 }
445
446 public void set(double value) {
447 throw unsupported();
448 }
449
450 public double[] toArray() {
451 throw unsupported();
452 }
453
454 public boolean isNaN() {
455 throw unsupported();
456 }
457
458 public boolean isInfinite() {
459 throw unsupported();
460 }
461
462 }
463
464 public static Test suite() {
465 TestSuite suite = new TestSuite(ArrayRealVectorTest.class);
466 suite.setName("ArrayRealVector Tests");
467 return suite;
468 }
469
470 public void testConstructors() {
471
472 ArrayRealVector v0 = new ArrayRealVector();
473 assertEquals("testData len", 0, v0.getDimension());
474
475 ArrayRealVector v1 = new ArrayRealVector(7);
476 assertEquals("testData len", 7, v1.getDimension());
477 assertEquals("testData is 0.0 ", 0.0, v1.getEntry(6));
478
479 ArrayRealVector v2 = new ArrayRealVector(5, 1.23);
480 assertEquals("testData len", 5, v2.getDimension());
481 assertEquals("testData is 1.23 ", 1.23, v2.getEntry(4));
482
483 ArrayRealVector v3 = new ArrayRealVector(vec1);
484 assertEquals("testData len", 3, v3.getDimension());
485 assertEquals("testData is 2.0 ", 2.0, v3.getEntry(1));
486
487 ArrayRealVector v4 = new ArrayRealVector(vec4, 3, 2);
488 assertEquals("testData len", 2, v4.getDimension());
489 assertEquals("testData is 4.0 ", 4.0, v4.getEntry(0));
490 try {
491 new ArrayRealVector(vec4, 8, 3);
492 fail("IllegalArgumentException expected");
493 } catch (IllegalArgumentException ex) {
494
495 } catch (Exception e) {
496 fail("wrong exception caught");
497 }
498
499 RealVector v5_i = new ArrayRealVector(dvec1);
500 assertEquals("testData len", 9, v5_i.getDimension());
501 assertEquals("testData is 9.0 ", 9.0, v5_i.getEntry(8));
502
503 ArrayRealVector v5 = new ArrayRealVector(dvec1);
504 assertEquals("testData len", 9, v5.getDimension());
505 assertEquals("testData is 9.0 ", 9.0, v5.getEntry(8));
506
507 ArrayRealVector v6 = new ArrayRealVector(dvec1, 3, 2);
508 assertEquals("testData len", 2, v6.getDimension());
509 assertEquals("testData is 4.0 ", 4.0, v6.getEntry(0));
510 try {
511 new ArrayRealVector(dvec1, 8, 3);
512 fail("IllegalArgumentException expected");
513 } catch (IllegalArgumentException ex) {
514
515 } catch (Exception e) {
516 fail("wrong exception caught");
517 }
518
519 ArrayRealVector v7 = new ArrayRealVector(v1);
520 assertEquals("testData len", 7, v7.getDimension());
521 assertEquals("testData is 0.0 ", 0.0, v7.getEntry(6));
522
523 RealVectorTestImpl v7_i = new RealVectorTestImpl(vec1);
524
525 ArrayRealVector v7_2 = new ArrayRealVector(v7_i);
526 assertEquals("testData len", 3, v7_2.getDimension());
527 assertEquals("testData is 0.0 ", 2.0d, v7_2.getEntry(1));
528
529 ArrayRealVector v8 = new ArrayRealVector(v1, true);
530 assertEquals("testData len", 7, v8.getDimension());
531 assertEquals("testData is 0.0 ", 0.0, v8.getEntry(6));
532 assertNotSame("testData not same object ", v1.data, v8.data);
533
534 ArrayRealVector v8_2 = new ArrayRealVector(v1, false);
535 assertEquals("testData len", 7, v8_2.getDimension());
536 assertEquals("testData is 0.0 ", 0.0, v8_2.getEntry(6));
537 assertEquals("testData same object ", v1.data, v8_2.data);
538
539 ArrayRealVector v9 = new ArrayRealVector(v1, v3);
540 assertEquals("testData len", 10, v9.getDimension());
541 assertEquals("testData is 1.0 ", 1.0, v9.getEntry(7));
542
543 }
544
545 public void testDataInOut() {
546
547 ArrayRealVector v1 = new ArrayRealVector(vec1);
548 ArrayRealVector v2 = new ArrayRealVector(vec2);
549 ArrayRealVector v4 = new ArrayRealVector(vec4);
550 RealVectorTestImpl v2_t = new RealVectorTestImpl(vec2);
551
552 RealVector v_append_1 = v1.append(v2);
553 assertEquals("testData len", 6, v_append_1.getDimension());
554 assertEquals("testData is 4.0 ", 4.0, v_append_1.getEntry(3));
555
556 RealVector v_append_2 = v1.append(2.0);
557 assertEquals("testData len", 4, v_append_2.getDimension());
558 assertEquals("testData is 2.0 ", 2.0, v_append_2.getEntry(3));
559
560 RealVector v_append_3 = v1.append(vec2);
561 assertEquals("testData len", 6, v_append_3.getDimension());
562 assertEquals("testData is ", 4.0, v_append_3.getEntry(3));
563
564 RealVector v_append_4 = v1.append(v2_t);
565 assertEquals("testData len", 6, v_append_4.getDimension());
566 assertEquals("testData is 4.0 ", 4.0, v_append_4.getEntry(3));
567
568 RealVector v_copy = v1.copy();
569 assertEquals("testData len", 3, v_copy.getDimension());
570 assertNotSame("testData not same object ", v1.data, v_copy.getData());
571
572 double[] a_double = v1.toArray();
573 assertEquals("testData len", 3, a_double.length);
574 assertNotSame("testData not same object ", v1.data, a_double);
575
576
577
578
579
580
581
582 RealVector vout5 = v4.getSubVector(3, 3);
583 assertEquals("testData len", 3, vout5.getDimension());
584 assertEquals("testData is 4.0 ", 5.0, vout5.getEntry(1));
585 try {
586 v4.getSubVector(3, 7);
587 fail("MatrixIndexException expected");
588 } catch (MatrixIndexException ex) {
589
590 } catch (Exception e) {
591 fail("wrong exception caught");
592 }
593
594 ArrayRealVector v_set1 = (ArrayRealVector) v1.copy();
595 v_set1.setEntry(1, 11.0);
596 assertEquals("testData is 11.0 ", 11.0, v_set1.getEntry(1));
597 try {
598 v_set1.setEntry(3, 11.0);
599 fail("MatrixIndexException expected");
600 } catch (MatrixIndexException ex) {
601
602 } catch (Exception e) {
603 fail("wrong exception caught");
604 }
605
606 ArrayRealVector v_set2 = (ArrayRealVector) v4.copy();
607 v_set2.set(3, v1);
608 assertEquals("testData is 1.0 ", 1.0, v_set2.getEntry(3));
609 assertEquals("testData is 7.0 ", 7.0, v_set2.getEntry(6));
610 try {
611 v_set2.set(7, v1);
612 fail("MatrixIndexException expected");
613 } catch (MatrixIndexException ex) {
614
615 } catch (Exception e) {
616 fail("wrong exception caught");
617 }
618
619 ArrayRealVector v_set3 = (ArrayRealVector) v1.copy();
620 v_set3.set(13.0);
621 assertEquals("testData is 13.0 ", 13.0, v_set3.getEntry(2));
622
623 try {
624 v_set3.getEntry(23);
625 fail("ArrayIndexOutOfBoundsException expected");
626 } catch (ArrayIndexOutOfBoundsException ex) {
627
628 } catch (Exception e) {
629 fail("wrong exception caught");
630 }
631
632 ArrayRealVector v_set4 = (ArrayRealVector) v4.copy();
633 v_set4.setSubVector(3, v2_t);
634 assertEquals("testData is 1.0 ", 4.0, v_set4.getEntry(3));
635 assertEquals("testData is 7.0 ", 7.0, v_set4.getEntry(6));
636 try {
637 v_set4.setSubVector(7, v2_t);
638 fail("MatrixIndexException expected");
639 } catch (MatrixIndexException ex) {
640
641 } catch (Exception e) {
642 fail("wrong exception caught");
643 }
644
645
646 ArrayRealVector vout10 = (ArrayRealVector) v1.copy();
647 ArrayRealVector vout10_2 = (ArrayRealVector) v1.copy();
648 assertEquals(vout10, vout10_2);
649 vout10_2.setEntry(0, 1.1);
650 assertNotSame(vout10, vout10_2);
651
652 }
653
654 public void testMapFunctions() {
655 ArrayRealVector v1 = new ArrayRealVector(vec1);
656
657
658 RealVector v_mapAdd = v1.mapAdd(2.0d);
659 double[] result_mapAdd = {3d, 4d, 5d};
660 assertClose("compare vectors" ,result_mapAdd,v_mapAdd.getData(),normTolerance);
661
662
663 RealVector v_mapAddToSelf = v1.copy();
664 v_mapAddToSelf.mapAddToSelf(2.0d);
665 double[] result_mapAddToSelf = {3d, 4d, 5d};
666 assertClose("compare vectors" ,result_mapAddToSelf,v_mapAddToSelf.getData(),normTolerance);
667
668
669 RealVector v_mapSubtract = v1.mapSubtract(2.0d);
670 double[] result_mapSubtract = {-1d, 0d, 1d};
671 assertClose("compare vectors" ,result_mapSubtract,v_mapSubtract.getData(),normTolerance);
672
673
674 RealVector v_mapSubtractToSelf = v1.copy();
675 v_mapSubtractToSelf.mapSubtractToSelf(2.0d);
676 double[] result_mapSubtractToSelf = {-1d, 0d, 1d};
677 assertClose("compare vectors" ,result_mapSubtractToSelf,v_mapSubtractToSelf.getData(),normTolerance);
678
679
680 RealVector v_mapMultiply = v1.mapMultiply(2.0d);
681 double[] result_mapMultiply = {2d, 4d, 6d};
682 assertClose("compare vectors" ,result_mapMultiply,v_mapMultiply.getData(),normTolerance);
683
684
685 RealVector v_mapMultiplyToSelf = v1.copy();
686 v_mapMultiplyToSelf.mapMultiplyToSelf(2.0d);
687 double[] result_mapMultiplyToSelf = {2d, 4d, 6d};
688 assertClose("compare vectors" ,result_mapMultiplyToSelf,v_mapMultiplyToSelf.getData(),normTolerance);
689
690
691 RealVector v_mapDivide = v1.mapDivide(2.0d);
692 double[] result_mapDivide = {.5d, 1d, 1.5d};
693 assertClose("compare vectors" ,result_mapDivide,v_mapDivide.getData(),normTolerance);
694
695
696 RealVector v_mapDivideToSelf = v1.copy();
697 v_mapDivideToSelf.mapDivideToSelf(2.0d);
698 double[] result_mapDivideToSelf = {.5d, 1d, 1.5d};
699 assertClose("compare vectors" ,result_mapDivideToSelf,v_mapDivideToSelf.getData(),normTolerance);
700
701
702 RealVector v_mapPow = v1.mapPow(2.0d);
703 double[] result_mapPow = {1d, 4d, 9d};
704 assertClose("compare vectors" ,result_mapPow,v_mapPow.getData(),normTolerance);
705
706
707 RealVector v_mapPowToSelf = v1.copy();
708 v_mapPowToSelf.mapPowToSelf(2.0d);
709 double[] result_mapPowToSelf = {1d, 4d, 9d};
710 assertClose("compare vectors" ,result_mapPowToSelf,v_mapPowToSelf.getData(),normTolerance);
711
712
713 RealVector v_mapExp = v1.mapExp();
714 double[] result_mapExp = {2.718281828459045e+00d,7.389056098930650e+00d, 2.008553692318767e+01d};
715 assertClose("compare vectors" ,result_mapExp,v_mapExp.getData(),normTolerance);
716
717
718 RealVector v_mapExpToSelf = v1.copy();
719 v_mapExpToSelf.mapExpToSelf();
720 double[] result_mapExpToSelf = {2.718281828459045e+00d,7.389056098930650e+00d, 2.008553692318767e+01d};
721 assertClose("compare vectors" ,result_mapExpToSelf,v_mapExpToSelf.getData(),normTolerance);
722
723
724
725 RealVector v_mapExpm1 = v1.mapExpm1();
726 double[] result_mapExpm1 = {1.718281828459045d,6.38905609893065d, 19.085536923187668d};
727 assertClose("compare vectors" ,result_mapExpm1,v_mapExpm1.getData(),normTolerance);
728
729
730 RealVector v_mapExpm1ToSelf = v1.copy();
731 v_mapExpm1ToSelf.mapExpm1ToSelf();
732 double[] result_mapExpm1ToSelf = {1.718281828459045d,6.38905609893065d, 19.085536923187668d};
733 assertClose("compare vectors" ,result_mapExpm1ToSelf,v_mapExpm1ToSelf.getData(),normTolerance);
734
735
736 RealVector v_mapLog = v1.mapLog();
737 double[] result_mapLog = {0d,6.931471805599453e-01d, 1.098612288668110e+00d};
738 assertClose("compare vectors" ,result_mapLog,v_mapLog.getData(),normTolerance);
739
740
741 RealVector v_mapLogToSelf = v1.copy();
742 v_mapLogToSelf.mapLogToSelf();
743 double[] result_mapLogToSelf = {0d,6.931471805599453e-01d, 1.098612288668110e+00d};
744 assertClose("compare vectors" ,result_mapLogToSelf,v_mapLogToSelf.getData(),normTolerance);
745
746
747 RealVector v_mapLog10 = v1.mapLog10();
748 double[] result_mapLog10 = {0d,3.010299956639812e-01d, 4.771212547196624e-01d};
749 assertClose("compare vectors" ,result_mapLog10,v_mapLog10.getData(),normTolerance);
750
751
752 RealVector v_mapLog10ToSelf = v1.copy();
753 v_mapLog10ToSelf.mapLog10ToSelf();
754 double[] result_mapLog10ToSelf = {0d,3.010299956639812e-01d, 4.771212547196624e-01d};
755 assertClose("compare vectors" ,result_mapLog10ToSelf,v_mapLog10ToSelf.getData(),normTolerance);
756
757
758 RealVector v_mapLog1p = v1.mapLog1p();
759 double[] result_mapLog1p = {0.6931471805599453d,1.0986122886681096d,1.3862943611198906d};
760 assertClose("compare vectors" ,result_mapLog1p,v_mapLog1p.getData(),normTolerance);
761
762
763 RealVector v_mapLog1pToSelf = v1.copy();
764 v_mapLog1pToSelf.mapLog1pToSelf();
765 double[] result_mapLog1pToSelf = {0.6931471805599453d,1.0986122886681096d,1.3862943611198906d};
766 assertClose("compare vectors" ,result_mapLog1pToSelf,v_mapLog1pToSelf.getData(),normTolerance);
767
768
769 RealVector v_mapCosh = v1.mapCosh();
770 double[] result_mapCosh = {1.543080634815244e+00d,3.762195691083631e+00d, 1.006766199577777e+01d};
771 assertClose("compare vectors" ,result_mapCosh,v_mapCosh.getData(),normTolerance);
772
773
774 RealVector v_mapCoshToSelf = v1.copy();
775 v_mapCoshToSelf.mapCoshToSelf();
776 double[] result_mapCoshToSelf = {1.543080634815244e+00d,3.762195691083631e+00d, 1.006766199577777e+01d};
777 assertClose("compare vectors" ,result_mapCoshToSelf,v_mapCoshToSelf.getData(),normTolerance);
778
779
780 RealVector v_mapSinh = v1.mapSinh();
781 double[] result_mapSinh = {1.175201193643801e+00d,3.626860407847019e+00d, 1.001787492740990e+01d};
782 assertClose("compare vectors" ,result_mapSinh,v_mapSinh.getData(),normTolerance);
783
784
785 RealVector v_mapSinhToSelf = v1.copy();
786 v_mapSinhToSelf.mapSinhToSelf();
787 double[] result_mapSinhToSelf = {1.175201193643801e+00d,3.626860407847019e+00d, 1.001787492740990e+01d};
788 assertClose("compare vectors" ,result_mapSinhToSelf,v_mapSinhToSelf.getData(),normTolerance);
789
790
791 RealVector v_mapTanh = v1.mapTanh();
792 double[] result_mapTanh = {7.615941559557649e-01d,9.640275800758169e-01d,9.950547536867305e-01d};
793 assertClose("compare vectors" ,result_mapTanh,v_mapTanh.getData(),normTolerance);
794
795
796 RealVector v_mapTanhToSelf = v1.copy();
797 v_mapTanhToSelf.mapTanhToSelf();
798 double[] result_mapTanhToSelf = {7.615941559557649e-01d,9.640275800758169e-01d,9.950547536867305e-01d};
799 assertClose("compare vectors" ,result_mapTanhToSelf,v_mapTanhToSelf.getData(),normTolerance);
800
801
802 RealVector v_mapCos = v1.mapCos();
803 double[] result_mapCos = {5.403023058681398e-01d,-4.161468365471424e-01d, -9.899924966004454e-01d};
804 assertClose("compare vectors" ,result_mapCos,v_mapCos.getData(),normTolerance);
805
806
807 RealVector v_mapCosToSelf = v1.copy();
808 v_mapCosToSelf.mapCosToSelf();
809 double[] result_mapCosToSelf = {5.403023058681398e-01d,-4.161468365471424e-01d, -9.899924966004454e-01d};
810 assertClose("compare vectors" ,result_mapCosToSelf,v_mapCosToSelf.getData(),normTolerance);
811
812
813 RealVector v_mapSin = v1.mapSin();
814 double[] result_mapSin = {8.414709848078965e-01d,9.092974268256817e-01d,1.411200080598672e-01d};
815 assertClose("compare vectors" ,result_mapSin,v_mapSin.getData(),normTolerance);
816
817
818 RealVector v_mapSinToSelf = v1.copy();
819 v_mapSinToSelf.mapSinToSelf();
820 double[] result_mapSinToSelf = {8.414709848078965e-01d,9.092974268256817e-01d,1.411200080598672e-01d};
821 assertClose("compare vectors" ,result_mapSinToSelf,v_mapSinToSelf.getData(),normTolerance);
822
823
824 RealVector v_mapTan = v1.mapTan();
825 double[] result_mapTan = {1.557407724654902e+00d,-2.185039863261519e+00d,-1.425465430742778e-01d};
826 assertClose("compare vectors" ,result_mapTan,v_mapTan.getData(),normTolerance);
827
828
829 RealVector v_mapTanToSelf = v1.copy();
830 v_mapTanToSelf.mapTanToSelf();
831 double[] result_mapTanToSelf = {1.557407724654902e+00d,-2.185039863261519e+00d,-1.425465430742778e-01d};
832 assertClose("compare vectors" ,result_mapTanToSelf,v_mapTanToSelf.getData(),normTolerance);
833
834 double[] vat_a = {0d, 0.5d, 1.0d};
835 ArrayRealVector vat = new ArrayRealVector(vat_a);
836
837
838 RealVector v_mapAcos = vat.mapAcos();
839 double[] result_mapAcos = {1.570796326794897e+00d,1.047197551196598e+00d, 0.0d};
840 assertClose("compare vectors" ,result_mapAcos,v_mapAcos.getData(),normTolerance);
841
842
843 RealVector v_mapAcosToSelf = vat.copy();
844 v_mapAcosToSelf.mapAcosToSelf();
845 double[] result_mapAcosToSelf = {1.570796326794897e+00d,1.047197551196598e+00d, 0.0d};
846 assertClose("compare vectors" ,result_mapAcosToSelf,v_mapAcosToSelf.getData(),normTolerance);
847
848
849 RealVector v_mapAsin = vat.mapAsin();
850 double[] result_mapAsin = {0.0d,5.235987755982989e-01d,1.570796326794897e+00d};
851 assertClose("compare vectors" ,result_mapAsin,v_mapAsin.getData(),normTolerance);
852
853
854 RealVector v_mapAsinToSelf = vat.copy();
855 v_mapAsinToSelf.mapAsinToSelf();
856 double[] result_mapAsinToSelf = {0.0d,5.235987755982989e-01d,1.570796326794897e+00d};
857 assertClose("compare vectors" ,result_mapAsinToSelf,v_mapAsinToSelf.getData(),normTolerance);
858
859
860 RealVector v_mapAtan = vat.mapAtan();
861 double[] result_mapAtan = {0.0d,4.636476090008061e-01d,7.853981633974483e-01d};
862 assertClose("compare vectors" ,result_mapAtan,v_mapAtan.getData(),normTolerance);
863
864
865 RealVector v_mapAtanToSelf = vat.copy();
866 v_mapAtanToSelf.mapAtanToSelf();
867 double[] result_mapAtanToSelf = {0.0d,4.636476090008061e-01d,7.853981633974483e-01d};
868 assertClose("compare vectors" ,result_mapAtanToSelf,v_mapAtanToSelf.getData(),normTolerance);
869
870
871 RealVector v_mapInv = v1.mapInv();
872 double[] result_mapInv = {1d,0.5d,3.333333333333333e-01d};
873 assertClose("compare vectors" ,result_mapInv,v_mapInv.getData(),normTolerance);
874
875
876 RealVector v_mapInvToSelf = v1.copy();
877 v_mapInvToSelf.mapInvToSelf();
878 double[] result_mapInvToSelf = {1d,0.5d,3.333333333333333e-01d};
879 assertClose("compare vectors" ,result_mapInvToSelf,v_mapInvToSelf.getData(),normTolerance);
880
881 double[] abs_a = {-1.0d, 0.0d, 1.0d};
882 ArrayRealVector abs_v = new ArrayRealVector(abs_a);
883
884
885 RealVector v_mapAbs = abs_v.mapAbs();
886 double[] result_mapAbs = {1d,0d,1d};
887 assertClose("compare vectors" ,result_mapAbs,v_mapAbs.getData(),normTolerance);
888
889
890 RealVector v_mapAbsToSelf = abs_v.copy();
891 v_mapAbsToSelf.mapAbsToSelf();
892 double[] result_mapAbsToSelf = {1d,0d,1d};
893 assertClose("compare vectors" ,result_mapAbsToSelf,v_mapAbsToSelf.getData(),normTolerance);
894
895
896 RealVector v_mapSqrt = v1.mapSqrt();
897 double[] result_mapSqrt = {1d,1.414213562373095e+00d,1.732050807568877e+00d};
898 assertClose("compare vectors" ,result_mapSqrt,v_mapSqrt.getData(),normTolerance);
899
900
901 RealVector v_mapSqrtToSelf = v1.copy();
902 v_mapSqrtToSelf.mapSqrtToSelf();
903 double[] result_mapSqrtToSelf = {1d,1.414213562373095e+00d,1.732050807568877e+00d};
904 assertClose("compare vectors" ,result_mapSqrtToSelf,v_mapSqrtToSelf.getData(),normTolerance);
905
906 double[] cbrt_a = {-2.0d, 0.0d, 2.0d};
907 ArrayRealVector cbrt_v = new ArrayRealVector(cbrt_a);
908
909
910 RealVector v_mapCbrt = cbrt_v.mapCbrt();
911 double[] result_mapCbrt = {-1.2599210498948732d,0d,1.2599210498948732d};
912 assertClose("compare vectors" ,result_mapCbrt,v_mapCbrt.getData(),normTolerance);
913
914
915 RealVector v_mapCbrtToSelf = cbrt_v.copy();
916 v_mapCbrtToSelf.mapCbrtToSelf();
917 double[] result_mapCbrtToSelf = {-1.2599210498948732d,0d,1.2599210498948732d};
918 assertClose("compare vectors" ,result_mapCbrtToSelf,v_mapCbrtToSelf.getData(),normTolerance);
919
920 double[] ceil_a = {-1.1d, 0.9d, 1.1d};
921 ArrayRealVector ceil_v = new ArrayRealVector(ceil_a);
922
923
924 RealVector v_mapCeil = ceil_v.mapCeil();
925 double[] result_mapCeil = {-1d,1d,2d};
926 assertClose("compare vectors" ,result_mapCeil,v_mapCeil.getData(),normTolerance);
927
928
929 RealVector v_mapCeilToSelf = ceil_v.copy();
930 v_mapCeilToSelf.mapCeilToSelf();
931 double[] result_mapCeilToSelf = {-1d,1d,2d};
932 assertClose("compare vectors" ,result_mapCeilToSelf,v_mapCeilToSelf.getData(),normTolerance);
933
934
935 RealVector v_mapFloor = ceil_v.mapFloor();
936 double[] result_mapFloor = {-2d,0d,1d};
937 assertClose("compare vectors" ,result_mapFloor,v_mapFloor.getData(),normTolerance);
938
939
940 RealVector v_mapFloorToSelf = ceil_v.copy();
941 v_mapFloorToSelf.mapFloorToSelf();
942 double[] result_mapFloorToSelf = {-2d,0d,1d};
943 assertClose("compare vectors" ,result_mapFloorToSelf,v_mapFloorToSelf.getData(),normTolerance);
944
945
946 RealVector v_mapRint = ceil_v.mapRint();
947 double[] result_mapRint = {-1d,1d,1d};
948 assertClose("compare vectors" ,result_mapRint,v_mapRint.getData(),normTolerance);
949
950
951 RealVector v_mapRintToSelf = ceil_v.copy();
952 v_mapRintToSelf.mapRintToSelf();
953 double[] result_mapRintToSelf = {-1d,1d,1d};
954 assertClose("compare vectors" ,result_mapRintToSelf,v_mapRintToSelf.getData(),normTolerance);
955
956
957 RealVector v_mapSignum = ceil_v.mapSignum();
958 double[] result_mapSignum = {-1d,1d,1d};
959 assertClose("compare vectors" ,result_mapSignum,v_mapSignum.getData(),normTolerance);
960
961
962 RealVector v_mapSignumToSelf = ceil_v.copy();
963 v_mapSignumToSelf.mapSignumToSelf();
964 double[] result_mapSignumToSelf = {-1d,1d,1d};
965 assertClose("compare vectors" ,result_mapSignumToSelf,v_mapSignumToSelf.getData(),normTolerance);
966
967
968
969
970 RealVector v_mapUlp = ceil_v.mapUlp();
971 double[] result_mapUlp = {2.220446049250313E-16d,1.1102230246251565E-16d,2.220446049250313E-16d};
972 assertClose("compare vectors" ,result_mapUlp,v_mapUlp.getData(),normTolerance);
973
974
975 RealVector v_mapUlpToSelf = ceil_v.copy();
976 v_mapUlpToSelf.mapUlpToSelf();
977 double[] result_mapUlpToSelf = {2.220446049250313E-16d,1.1102230246251565E-16d,2.220446049250313E-16d};
978 assertClose("compare vectors" ,result_mapUlpToSelf,v_mapUlpToSelf.getData(),normTolerance);
979
980 }
981
982 public void testBasicFunctions() {
983 ArrayRealVector v1 = new ArrayRealVector(vec1);
984 ArrayRealVector v2 = new ArrayRealVector(vec2);
985 ArrayRealVector v_null = new ArrayRealVector(vec_null);
986
987 RealVectorTestImpl v2_t = new RealVectorTestImpl(vec2);
988
989
990 double d_getNorm = v1.getNorm();
991 assertEquals("compare values ", 3.7416573867739413,d_getNorm);
992
993 double d_getL1Norm = v1.getL1Norm();
994 assertEquals("compare values ",6.0, d_getL1Norm);
995
996 double d_getLInfNorm = v1.getLInfNorm();
997 assertEquals("compare values ",6.0, d_getLInfNorm);
998
999
1000 double dist = v1.getDistance(v2);
1001 assertEquals("compare values ",v1.subtract(v2).getNorm(), dist );
1002
1003
1004 double dist_2 = v1.getDistance(v2_t);
1005 assertEquals("compare values ", v1.subtract(v2).getNorm(),dist_2 );
1006
1007
1008 double d_getL1Distance = v1. getL1Distance(v2);
1009 assertEquals("compare values ",9d, d_getL1Distance );
1010
1011 double d_getL1Distance_2 = v1. getL1Distance(v2_t);
1012 assertEquals("compare values ",9d, d_getL1Distance_2 );
1013
1014
1015 double d_getLInfDistance = v1. getLInfDistance(v2);
1016 assertEquals("compare values ",3d, d_getLInfDistance );
1017
1018 double d_getLInfDistance_2 = v1. getLInfDistance(v2_t);
1019 assertEquals("compare values ",3d, d_getLInfDistance_2 );
1020
1021
1022 ArrayRealVector v_add = v1.add(v2);
1023 double[] result_add = {5d, 7d, 9d};
1024 assertClose("compare vect" ,v_add.getData(),result_add,normTolerance);
1025
1026 RealVectorTestImpl vt2 = new RealVectorTestImpl(vec2);
1027 RealVector v_add_i = v1.add(vt2);
1028 double[] result_add_i = {5d, 7d, 9d};
1029 assertClose("compare vect" ,v_add_i.getData(),result_add_i,normTolerance);
1030
1031
1032 ArrayRealVector v_subtract = v1.subtract(v2);
1033 double[] result_subtract = {-3d, -3d, -3d};
1034 assertClose("compare vect" ,v_subtract.getData(),result_subtract,normTolerance);
1035
1036 RealVector v_subtract_i = v1.subtract(vt2);
1037 double[] result_subtract_i = {-3d, -3d, -3d};
1038 assertClose("compare vect" ,v_subtract_i.getData(),result_subtract_i,normTolerance);
1039
1040
1041 ArrayRealVector v_ebeMultiply = v1.ebeMultiply(v2);
1042 double[] result_ebeMultiply = {4d, 10d, 18d};
1043 assertClose("compare vect" ,v_ebeMultiply.getData(),result_ebeMultiply,normTolerance);
1044
1045 RealVector v_ebeMultiply_2 = v1.ebeMultiply(v2_t);
1046 double[] result_ebeMultiply_2 = {4d, 10d, 18d};
1047 assertClose("compare vect" ,v_ebeMultiply_2.getData(),result_ebeMultiply_2,normTolerance);
1048
1049
1050 ArrayRealVector v_ebeDivide = v1.ebeDivide(v2);
1051 double[] result_ebeDivide = {0.25d, 0.4d, 0.5d};
1052 assertClose("compare vect" ,v_ebeDivide.getData(),result_ebeDivide,normTolerance);
1053
1054 RealVector v_ebeDivide_2 = v1.ebeDivide(v2_t);
1055 double[] result_ebeDivide_2 = {0.25d, 0.4d, 0.5d};
1056 assertClose("compare vect" ,v_ebeDivide_2.getData(),result_ebeDivide_2,normTolerance);
1057
1058
1059 double dot = v1.dotProduct(v2);
1060 assertEquals("compare val ",32d, dot);
1061
1062
1063 double dot_2 = v1.dotProduct(v2_t);
1064 assertEquals("compare val ",32d, dot_2);
1065
1066 RealMatrix m_outerProduct = v1.outerProduct(v2);
1067 assertEquals("compare val ",4d, m_outerProduct.getEntry(0,0));
1068
1069 RealMatrix m_outerProduct_2 = v1.outerProduct(v2_t);
1070 assertEquals("compare val ",4d, m_outerProduct_2.getEntry(0,0));
1071
1072 RealVector v_unitVector = v1.unitVector();
1073 RealVector v_unitVector_2 = v1.mapDivide(v1.getNorm());
1074 assertClose("compare vect" ,v_unitVector.getData(),v_unitVector_2.getData(),normTolerance);
1075
1076 try {
1077 v_null.unitVector();
1078 fail("Expecting ArithmeticException");
1079 } catch (ArithmeticException ex) {
1080
1081 } catch (Exception e) {
1082 fail("wrong exception caught");
1083 }
1084
1085 ArrayRealVector v_unitize = (ArrayRealVector)v1.copy();
1086 v_unitize.unitize();
1087 assertClose("compare vect" ,v_unitVector_2.getData(),v_unitize.getData(),normTolerance);
1088 try {
1089 v_null.unitize();
1090 fail("Expecting ArithmeticException");
1091 } catch (ArithmeticException ex) {
1092
1093 } catch (Exception e) {
1094 fail("wrong exception caught");
1095 }
1096
1097 ArrayRealVector v_projection = v1.projection(v2);
1098 double[] result_projection = {1.662337662337662, 2.0779220779220777, 2.493506493506493};
1099 assertClose("compare vect", v_projection.getData(), result_projection, normTolerance);
1100
1101 RealVector v_projection_2 = v1.projection(v2_t);
1102 double[] result_projection_2 = {1.662337662337662, 2.0779220779220777, 2.493506493506493};
1103 assertClose("compare vect", v_projection_2.getData(), result_projection_2, normTolerance);
1104
1105 }
1106
1107 public void testMisc() {
1108 ArrayRealVector v1 = new ArrayRealVector(vec1);
1109 ArrayRealVector v4 = new ArrayRealVector(vec4);
1110 RealVector v4_2 = new ArrayRealVector(vec4);
1111
1112 String out1 = v1.toString();
1113 assertTrue("some output ", out1.length()!=0);
1114
1115
1116
1117
1118
1119 try {
1120 v1.checkVectorDimensions(2);
1121 fail("IllegalArgumentException expected");
1122 } catch (IllegalArgumentException ex) {
1123
1124 } catch (Exception e) {
1125 fail("wrong exception caught");
1126 }
1127
1128 try {
1129 v1.checkVectorDimensions(v4);
1130 fail("IllegalArgumentException expected");
1131 } catch (IllegalArgumentException ex) {
1132
1133 } catch (Exception e) {
1134 fail("wrong exception caught");
1135 }
1136
1137 try {
1138 v1.checkVectorDimensions(v4_2);
1139 fail("IllegalArgumentException expected");
1140 } catch (IllegalArgumentException ex) {
1141
1142 } catch (Exception e) {
1143 fail("wrong exception caught");
1144 }
1145
1146 }
1147
1148 public void testPredicates() {
1149
1150 ArrayRealVector v = new ArrayRealVector(new double[] { 0, 1, 2 });
1151
1152 assertFalse(v.isNaN());
1153 v.setEntry(1, Double.NaN);
1154 assertTrue(v.isNaN());
1155
1156 assertFalse(v.isInfinite());
1157 v.setEntry(0, Double.POSITIVE_INFINITY);
1158 assertFalse(v.isInfinite());
1159 v.setEntry(1, 1);
1160 assertTrue(v.isInfinite());
1161
1162 v.setEntry(0, 0);
1163 assertEquals(v, new ArrayRealVector(new double[] { 0, 1, 2 }));
1164 assertNotSame(v, new ArrayRealVector(new double[] { 0, 1, 2 + Math.ulp(2)}));
1165 assertNotSame(v, new ArrayRealVector(new double[] { 0, 1, 2, 3 }));
1166
1167 assertEquals(new ArrayRealVector(new double[] { Double.NaN, 1, 2 }).hashCode(),
1168 new ArrayRealVector(new double[] { 0, Double.NaN, 2 }).hashCode());
1169
1170 assertTrue(new ArrayRealVector(new double[] { Double.NaN, 1, 2 }).hashCode() !=
1171 new ArrayRealVector(new double[] { 0, 1, 2 }).hashCode());
1172
1173 }
1174
1175 public void testSerial() {
1176 ArrayRealVector v = new ArrayRealVector(new double[] { 0, 1, 2 });
1177 assertEquals(v,TestUtils.serializeAndRecover(v));
1178 }
1179
1180
1181
1182 protected void assertClose(String msg, double[] m, double[] n,
1183 double tolerance) {
1184 if (m.length != n.length) {
1185 fail("vectors have different lengths");
1186 }
1187 for (int i = 0; i < m.length; i++) {
1188 assertEquals(msg + " " + i + " elements differ", m[i],n[i],tolerance);
1189 }
1190 }
1191
1192 }