2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.math.linear;
19
import java.io.Serializable;
21
import junit.framework.Test;
22
import junit.framework.TestCase;
23
import junit.framework.TestSuite;
25
import org.apache.commons.math.TestUtils;
28
* Test cases for the {@link ArrayRealVector} class.
30
* @version $Revision: 783702 $ $Date: 2009-06-11 04:54:02 -0400 (Thu, 11 Jun 2009) $
32
public class ArrayRealVectorTest extends TestCase {
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}};
45
protected double entryTolerance = 10E-16;
46
protected double normTolerance = 10E-14;
48
// Testclass to test the RealVector interface
49
// only with enough content to support the test
50
public static class RealVectorTestImpl implements RealVector, Serializable {
52
/** Serializable version identifier. */
53
private static final long serialVersionUID = 4715341047369582908L;
55
/** Entries of the vector. */
56
protected double data[];
58
public RealVectorTestImpl(double[] d) {
62
private UnsupportedOperationException unsupported() {
63
return new UnsupportedOperationException("Not supported, unneeded for test purposes");
66
public RealVector copy() {
70
public RealVector add(RealVector v) throws IllegalArgumentException {
74
public RealVector add(double[] v) throws IllegalArgumentException {
78
public RealVector subtract(RealVector v) throws IllegalArgumentException {
82
public RealVector subtract(double[] v) throws IllegalArgumentException {
86
public RealVector mapAdd(double d) {
90
public RealVector mapAddToSelf(double d) {
94
public RealVector mapSubtract(double d) {
98
public RealVector mapSubtractToSelf(double d) {
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;
107
return new ArrayRealVector(out);
110
public RealVector mapMultiplyToSelf(double d) {
114
public RealVector mapDivide(double d) {
118
public RealVector mapDivideToSelf(double d) {
122
public RealVector mapPow(double d) {
126
public RealVector mapPowToSelf(double d) {
130
public RealVector mapExp() {
134
public RealVector mapExpToSelf() {
138
public RealVector mapExpm1() {
142
public RealVector mapExpm1ToSelf() {
146
public RealVector mapLog() {
150
public RealVector mapLogToSelf() {
154
public RealVector mapLog10() {
158
public RealVector mapLog10ToSelf() {
162
public RealVector mapLog1p() {
166
public RealVector mapLog1pToSelf() {
170
public RealVector mapCosh() {
174
public RealVector mapCoshToSelf() {
178
public RealVector mapSinh() {
182
public RealVector mapSinhToSelf() {
186
public RealVector mapTanh() {
190
public RealVector mapTanhToSelf() {
194
public RealVector mapCos() {
198
public RealVector mapCosToSelf() {
202
public RealVector mapSin() {
206
public RealVector mapSinToSelf() {
210
public RealVector mapTan() {
214
public RealVector mapTanToSelf() {
218
public RealVector mapAcos() {
222
public RealVector mapAcosToSelf() {
226
public RealVector mapAsin() {
230
public RealVector mapAsinToSelf() {
234
public RealVector mapAtan() {
238
public RealVector mapAtanToSelf() {
242
public RealVector mapInv() {
246
public RealVector mapInvToSelf() {
250
public RealVector mapAbs() {
254
public RealVector mapAbsToSelf() {
258
public RealVector mapSqrt() {
262
public RealVector mapSqrtToSelf() {
266
public RealVector mapCbrt() {
270
public RealVector mapCbrtToSelf() {
274
public RealVector mapCeil() {
278
public RealVector mapCeilToSelf() {
282
public RealVector mapFloor() {
286
public RealVector mapFloorToSelf() {
290
public RealVector mapRint() {
294
public RealVector mapRintToSelf() {
298
public RealVector mapSignum() {
302
public RealVector mapSignumToSelf() {
306
public RealVector mapUlp() {
310
public RealVector mapUlpToSelf() {
314
public RealVector ebeMultiply(RealVector v) throws IllegalArgumentException {
318
public RealVector ebeMultiply(double[] v) throws IllegalArgumentException {
322
public RealVector ebeDivide(RealVector v) throws IllegalArgumentException {
326
public RealVector ebeDivide(double[] v) throws IllegalArgumentException {
330
public double[] getData() {
334
public double dotProduct(RealVector v) throws IllegalArgumentException {
336
for (int i = 0; i < data.length; i++) {
337
dot += data[i] * v.getEntry(i);
342
public double dotProduct(double[] v) throws IllegalArgumentException {
344
for (int i = 0; i < data.length; i++) {
345
dot += data[i] * v[i];
350
public double getNorm() {
354
public double getL1Norm() {
358
public double getLInfNorm() {
362
public double getDistance(RealVector v) throws IllegalArgumentException {
366
public double getDistance(double[] v) throws IllegalArgumentException {
370
public double getL1Distance(RealVector v) throws IllegalArgumentException {
374
public double getL1Distance(double[] v) throws IllegalArgumentException {
378
public double getLInfDistance(RealVector v) throws IllegalArgumentException {
382
public double getLInfDistance(double[] v) throws IllegalArgumentException {
386
public RealVector unitVector() {
390
public void unitize() {
394
public RealVector projection(RealVector v) throws IllegalArgumentException {
398
public RealVector projection(double[] v) throws IllegalArgumentException {
402
public RealMatrix outerProduct(RealVector v) throws IllegalArgumentException {
406
public RealMatrix outerProduct(double[] v) throws IllegalArgumentException {
410
public double getEntry(int index) throws MatrixIndexException {
414
public int getDimension() {
418
public RealVector append(RealVector v) {
422
public RealVector append(double d) {
426
public RealVector append(double[] a) {
430
public RealVector getSubVector(int index, int n) throws MatrixIndexException {
434
public void setEntry(int index, double value) throws MatrixIndexException {
438
public void setSubVector(int index, RealVector v) throws MatrixIndexException {
442
public void setSubVector(int index, double[] v) throws MatrixIndexException {
446
public void set(double value) {
450
public double[] toArray() {
454
public boolean isNaN() {
458
public boolean isInfinite() {
464
public static Test suite() {
465
TestSuite suite = new TestSuite(ArrayRealVectorTest.class);
466
suite.setName("ArrayRealVector Tests");
470
public void testConstructors() {
472
ArrayRealVector v0 = new ArrayRealVector();
473
assertEquals("testData len", 0, v0.getDimension());
475
ArrayRealVector v1 = new ArrayRealVector(7);
476
assertEquals("testData len", 7, v1.getDimension());
477
assertEquals("testData is 0.0 ", 0.0, v1.getEntry(6));
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));
483
ArrayRealVector v3 = new ArrayRealVector(vec1);
484
assertEquals("testData len", 3, v3.getDimension());
485
assertEquals("testData is 2.0 ", 2.0, v3.getEntry(1));
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));
491
new ArrayRealVector(vec4, 8, 3);
492
fail("IllegalArgumentException expected");
493
} catch (IllegalArgumentException ex) {
495
} catch (Exception e) {
496
fail("wrong exception caught");
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));
503
ArrayRealVector v5 = new ArrayRealVector(dvec1);
504
assertEquals("testData len", 9, v5.getDimension());
505
assertEquals("testData is 9.0 ", 9.0, v5.getEntry(8));
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));
511
new ArrayRealVector(dvec1, 8, 3);
512
fail("IllegalArgumentException expected");
513
} catch (IllegalArgumentException ex) {
515
} catch (Exception e) {
516
fail("wrong exception caught");
519
ArrayRealVector v7 = new ArrayRealVector(v1);
520
assertEquals("testData len", 7, v7.getDimension());
521
assertEquals("testData is 0.0 ", 0.0, v7.getEntry(6));
523
RealVectorTestImpl v7_i = new RealVectorTestImpl(vec1);
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));
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);
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);
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));
545
public void testDataInOut() {
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);
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));
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));
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));
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));
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());
572
double[] a_double = v1.toArray();
573
assertEquals("testData len", 3, a_double.length);
574
assertNotSame("testData not same object ", v1.data, a_double);
577
// ArrayRealVector vout4 = (ArrayRealVector) v1.clone();
578
// assertEquals("testData len", 3, vout4.getDimension());
579
// assertEquals("testData not same object ", v1.data, vout4.data);
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));
586
v4.getSubVector(3, 7);
587
fail("MatrixIndexException expected");
588
} catch (MatrixIndexException ex) {
590
} catch (Exception e) {
591
fail("wrong exception caught");
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));
598
v_set1.setEntry(3, 11.0);
599
fail("MatrixIndexException expected");
600
} catch (MatrixIndexException ex) {
602
} catch (Exception e) {
603
fail("wrong exception caught");
606
ArrayRealVector v_set2 = (ArrayRealVector) v4.copy();
608
assertEquals("testData is 1.0 ", 1.0, v_set2.getEntry(3));
609
assertEquals("testData is 7.0 ", 7.0, v_set2.getEntry(6));
612
fail("MatrixIndexException expected");
613
} catch (MatrixIndexException ex) {
615
} catch (Exception e) {
616
fail("wrong exception caught");
619
ArrayRealVector v_set3 = (ArrayRealVector) v1.copy();
621
assertEquals("testData is 13.0 ", 13.0, v_set3.getEntry(2));
625
fail("ArrayIndexOutOfBoundsException expected");
626
} catch (ArrayIndexOutOfBoundsException ex) {
628
} catch (Exception e) {
629
fail("wrong exception caught");
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));
637
v_set4.setSubVector(7, v2_t);
638
fail("MatrixIndexException expected");
639
} catch (MatrixIndexException ex) {
641
} catch (Exception e) {
642
fail("wrong exception caught");
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);
654
public void testMapFunctions() {
655
ArrayRealVector v1 = new ArrayRealVector(vec1);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
834
double[] vat_a = {0d, 0.5d, 1.0d};
835
ArrayRealVector vat = new ArrayRealVector(vat_a);
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);
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);
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);
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);
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);
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);
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);
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);
881
double[] abs_a = {-1.0d, 0.0d, 1.0d};
882
ArrayRealVector abs_v = new ArrayRealVector(abs_a);
884
//octave = abs(abs_v)
885
RealVector v_mapAbs = abs_v.mapAbs();
886
double[] result_mapAbs = {1d,0d,1d};
887
assertClose("compare vectors" ,result_mapAbs,v_mapAbs.getData(),normTolerance);
889
//octave = abs(abs_v)
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);
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);
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);
906
double[] cbrt_a = {-2.0d, 0.0d, 2.0d};
907
ArrayRealVector cbrt_v = new ArrayRealVector(cbrt_a);
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);
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);
920
double[] ceil_a = {-1.1d, 0.9d, 1.1d};
921
ArrayRealVector ceil_v = new ArrayRealVector(ceil_a);
923
//octave = ceil(ceil_v)
924
RealVector v_mapCeil = ceil_v.mapCeil();
925
double[] result_mapCeil = {-1d,1d,2d};
926
assertClose("compare vectors" ,result_mapCeil,v_mapCeil.getData(),normTolerance);
928
//octave = ceil(ceil_v)
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);
934
//octave = floor(ceil_v)
935
RealVector v_mapFloor = ceil_v.mapFloor();
936
double[] result_mapFloor = {-2d,0d,1d};
937
assertClose("compare vectors" ,result_mapFloor,v_mapFloor.getData(),normTolerance);
939
//octave = floor(ceil_v)
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);
946
RealVector v_mapRint = ceil_v.mapRint();
947
double[] result_mapRint = {-1d,1d,1d};
948
assertClose("compare vectors" ,result_mapRint,v_mapRint.getData(),normTolerance);
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);
957
RealVector v_mapSignum = ceil_v.mapSignum();
958
double[] result_mapSignum = {-1d,1d,1d};
959
assertClose("compare vectors" ,result_mapSignum,v_mapSignum.getData(),normTolerance);
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);
968
// Is with the used resolutions of limited value as test
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);
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);
982
public void testBasicFunctions() {
983
ArrayRealVector v1 = new ArrayRealVector(vec1);
984
ArrayRealVector v2 = new ArrayRealVector(vec2);
985
ArrayRealVector v_null = new ArrayRealVector(vec_null);
987
RealVectorTestImpl v2_t = new RealVectorTestImpl(vec2);
989
//octave = sqrt(sumsq(v1))
990
double d_getNorm = v1.getNorm();
991
assertEquals("compare values ", 3.7416573867739413,d_getNorm);
993
double d_getL1Norm = v1.getL1Norm();
994
assertEquals("compare values ",6.0, d_getL1Norm);
996
double d_getLInfNorm = v1.getLInfNorm();
997
assertEquals("compare values ",6.0, d_getLInfNorm);
999
//octave = sqrt(sumsq(v1-v2))
1000
double dist = v1.getDistance(v2);
1001
assertEquals("compare values ",v1.subtract(v2).getNorm(), dist );
1003
//octave = sqrt(sumsq(v1-v2))
1004
double dist_2 = v1.getDistance(v2_t);
1005
assertEquals("compare values ", v1.subtract(v2).getNorm(),dist_2 );
1008
double d_getL1Distance = v1. getL1Distance(v2);
1009
assertEquals("compare values ",9d, d_getL1Distance );
1011
double d_getL1Distance_2 = v1. getL1Distance(v2_t);
1012
assertEquals("compare values ",9d, d_getL1Distance_2 );
1015
double d_getLInfDistance = v1. getLInfDistance(v2);
1016
assertEquals("compare values ",3d, d_getLInfDistance );
1018
double d_getLInfDistance_2 = v1. getLInfDistance(v2_t);
1019
assertEquals("compare values ",3d, d_getLInfDistance_2 );
1022
ArrayRealVector v_add = v1.add(v2);
1023
double[] result_add = {5d, 7d, 9d};
1024
assertClose("compare vect" ,v_add.getData(),result_add,normTolerance);
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);
1032
ArrayRealVector v_subtract = v1.subtract(v2);
1033
double[] result_subtract = {-3d, -3d, -3d};
1034
assertClose("compare vect" ,v_subtract.getData(),result_subtract,normTolerance);
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);
1041
ArrayRealVector v_ebeMultiply = v1.ebeMultiply(v2);
1042
double[] result_ebeMultiply = {4d, 10d, 18d};
1043
assertClose("compare vect" ,v_ebeMultiply.getData(),result_ebeMultiply,normTolerance);
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);
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);
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);
1058
// octave dot(v1,v2)
1059
double dot = v1.dotProduct(v2);
1060
assertEquals("compare val ",32d, dot);
1062
// octave dot(v1,v2_t)
1063
double dot_2 = v1.dotProduct(v2_t);
1064
assertEquals("compare val ",32d, dot_2);
1066
RealMatrix m_outerProduct = v1.outerProduct(v2);
1067
assertEquals("compare val ",4d, m_outerProduct.getEntry(0,0));
1069
RealMatrix m_outerProduct_2 = v1.outerProduct(v2_t);
1070
assertEquals("compare val ",4d, m_outerProduct_2.getEntry(0,0));
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);
1077
v_null.unitVector();
1078
fail("Expecting ArithmeticException");
1079
} catch (ArithmeticException ex) {
1080
// expected behavior
1081
} catch (Exception e) {
1082
fail("wrong exception caught");
1085
ArrayRealVector v_unitize = (ArrayRealVector)v1.copy();
1086
v_unitize.unitize();
1087
assertClose("compare vect" ,v_unitVector_2.getData(),v_unitize.getData(),normTolerance);
1090
fail("Expecting ArithmeticException");
1091
} catch (ArithmeticException ex) {
1092
// expected behavior
1093
} catch (Exception e) {
1094
fail("wrong exception caught");
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);
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);
1107
public void testMisc() {
1108
ArrayRealVector v1 = new ArrayRealVector(vec1);
1109
ArrayRealVector v4 = new ArrayRealVector(vec4);
1110
RealVector v4_2 = new ArrayRealVector(vec4);
1112
String out1 = v1.toString();
1113
assertTrue("some output ", out1.length()!=0);
1115
double[] dout1 = v1.copyOut();
1116
assertEquals("testData len", 3, dout1.length);
1117
assertNotSame("testData not same object ", v1.data, dout1);
1120
v1.checkVectorDimensions(2);
1121
fail("IllegalArgumentException expected");
1122
} catch (IllegalArgumentException ex) {
1123
// expected behavior
1124
} catch (Exception e) {
1125
fail("wrong exception caught");
1129
v1.checkVectorDimensions(v4);
1130
fail("IllegalArgumentException expected");
1131
} catch (IllegalArgumentException ex) {
1132
// expected behavior
1133
} catch (Exception e) {
1134
fail("wrong exception caught");
1138
v1.checkVectorDimensions(v4_2);
1139
fail("IllegalArgumentException expected");
1140
} catch (IllegalArgumentException ex) {
1141
// expected behavior
1142
} catch (Exception e) {
1143
fail("wrong exception caught");
1148
public void testPredicates() {
1150
ArrayRealVector v = new ArrayRealVector(new double[] { 0, 1, 2 });
1152
assertFalse(v.isNaN());
1153
v.setEntry(1, Double.NaN);
1154
assertTrue(v.isNaN());
1156
assertFalse(v.isInfinite());
1157
v.setEntry(0, Double.POSITIVE_INFINITY);
1158
assertFalse(v.isInfinite());
1160
assertTrue(v.isInfinite());
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 }));
1167
assertEquals(new ArrayRealVector(new double[] { Double.NaN, 1, 2 }).hashCode(),
1168
new ArrayRealVector(new double[] { 0, Double.NaN, 2 }).hashCode());
1170
assertTrue(new ArrayRealVector(new double[] { Double.NaN, 1, 2 }).hashCode() !=
1171
new ArrayRealVector(new double[] { 0, 1, 2 }).hashCode());
1175
public void testSerial() {
1176
ArrayRealVector v = new ArrayRealVector(new double[] { 0, 1, 2 });
1177
assertEquals(v,TestUtils.serializeAndRecover(v));
1181
/** verifies that two vectors are close (sup norm) */
1182
protected void assertClose(String msg, double[] m, double[] n,
1184
if (m.length != n.length) {
1185
fail("vectors have different lengths");
1187
for (int i = 0; i < m.length; i++) {
1188
assertEquals(msg + " " + i + " elements differ", m[i],n[i],tolerance);