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 OpenMapRealVector} class.
30
* @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
32
public class SparseRealVectorTest 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 SparseRealVectorTestImpl 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 SparseRealVectorTestImpl(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 OpenMapRealVector(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(SparseRealVectorTest.class);
466
suite.setName("SparseRealVector Tests");
470
public void testConstructors() {
472
OpenMapRealVector v0 = new OpenMapRealVector();
473
assertEquals("testData len", 0, v0.getDimension());
475
OpenMapRealVector v1 = new OpenMapRealVector(7);
476
assertEquals("testData len", 7, v1.getDimension());
477
assertEquals("testData is 0.0 ", 0.0, v1.getEntry(6));
479
OpenMapRealVector v3 = new OpenMapRealVector(vec1);
480
assertEquals("testData len", 3, v3.getDimension());
481
assertEquals("testData is 2.0 ", 2.0, v3.getEntry(1));
483
//SparseRealVector v4 = new SparseRealVector(vec4, 3, 2);
484
//assertEquals("testData len", 2, v4.getDimension());
485
//assertEquals("testData is 4.0 ", 4.0, v4.getEntry(0));
487
// new SparseRealVector(vec4, 8, 3);
488
// fail("IllegalArgumentException expected");
489
//} catch (IllegalArgumentException ex) {
491
//} catch (Exception e) {
492
// fail("wrong exception caught");
495
RealVector v5_i = new OpenMapRealVector(dvec1);
496
assertEquals("testData len", 9, v5_i.getDimension());
497
assertEquals("testData is 9.0 ", 9.0, v5_i.getEntry(8));
499
OpenMapRealVector v5 = new OpenMapRealVector(dvec1);
500
assertEquals("testData len", 9, v5.getDimension());
501
assertEquals("testData is 9.0 ", 9.0, v5.getEntry(8));
503
OpenMapRealVector v7 = new OpenMapRealVector(v1);
504
assertEquals("testData len", 7, v7.getDimension());
505
assertEquals("testData is 0.0 ", 0.0, v7.getEntry(6));
507
SparseRealVectorTestImpl v7_i = new SparseRealVectorTestImpl(vec1);
509
OpenMapRealVector v7_2 = new OpenMapRealVector(v7_i);
510
assertEquals("testData len", 3, v7_2.getDimension());
511
assertEquals("testData is 0.0 ", 2.0d, v7_2.getEntry(1));
513
OpenMapRealVector v8 = new OpenMapRealVector(v1);
514
assertEquals("testData len", 7, v8.getDimension());
515
assertEquals("testData is 0.0 ", 0.0, v8.getEntry(6));
519
public void testDataInOut() {
521
OpenMapRealVector v1 = new OpenMapRealVector(vec1);
522
OpenMapRealVector v2 = new OpenMapRealVector(vec2);
523
OpenMapRealVector v4 = new OpenMapRealVector(vec4);
524
SparseRealVectorTestImpl v2_t = new SparseRealVectorTestImpl(vec2);
526
RealVector v_append_1 = v1.append(v2);
527
assertEquals("testData len", 6, v_append_1.getDimension());
528
assertEquals("testData is 4.0 ", 4.0, v_append_1.getEntry(3));
530
RealVector v_append_2 = v1.append(2.0);
531
assertEquals("testData len", 4, v_append_2.getDimension());
532
assertEquals("testData is 2.0 ", 2.0, v_append_2.getEntry(3));
534
RealVector v_append_3 = v1.append(vec2);
535
assertEquals("testData len", 6, v_append_3.getDimension());
536
assertEquals("testData is ", 4.0, v_append_3.getEntry(3));
538
RealVector v_append_4 = v1.append(v2_t);
539
assertEquals("testData len", 6, v_append_4.getDimension());
540
assertEquals("testData is 4.0 ", 4.0, v_append_4.getEntry(3));
542
RealVector vout5 = v4.getSubVector(3, 3);
543
assertEquals("testData len", 3, vout5.getDimension());
544
assertEquals("testData is 4.0 ", 5.0, vout5.getEntry(1));
546
v4.getSubVector(3, 7);
547
fail("MatrixIndexException expected");
548
} catch (MatrixIndexException ex) {
550
} catch (Exception e) {
551
fail("wrong exception caught");
554
OpenMapRealVector v_set1 = (OpenMapRealVector) v1.copy();
555
v_set1.setEntry(1, 11.0);
556
assertEquals("testData is 11.0 ", 11.0, v_set1.getEntry(1));
558
v_set1.setEntry(3, 11.0);
559
fail("MatrixIndexException expected");
560
} catch (MatrixIndexException ex) {
562
} catch (Exception e) {
563
fail("wrong exception caught");
566
OpenMapRealVector v_set2 = (OpenMapRealVector) v4.copy();
567
v_set2.setSubVector(3, v1);
568
assertEquals("testData is 1.0 ", 1.0, v_set2.getEntry(3));
569
assertEquals("testData is 7.0 ", 7.0, v_set2.getEntry(6));
571
v_set2.setSubVector(7, v1);
572
fail("MatrixIndexException expected");
573
} catch (MatrixIndexException ex) {
575
} catch (Exception e) {
576
fail("wrong exception caught");
579
OpenMapRealVector v_set3 = (OpenMapRealVector) v1.copy();
581
assertEquals("testData is 13.0 ", 13.0, v_set3.getEntry(2));
585
fail("MatrixIndexException expected");
586
} catch (MatrixIndexException ex) {
588
} catch (Exception e) {
589
fail("wrong exception caught");
592
OpenMapRealVector v_set4 = (OpenMapRealVector) v4.copy();
593
v_set4.setSubVector(3, v2_t);
594
assertEquals("testData is 1.0 ", 4.0, v_set4.getEntry(3));
595
assertEquals("testData is 7.0 ", 7.0, v_set4.getEntry(6));
597
v_set4.setSubVector(7, v2_t);
598
fail("MatrixIndexException expected");
599
} catch (MatrixIndexException ex) {
601
} catch (Exception e) {
602
fail("wrong exception caught");
608
public void testMapFunctions() {
609
OpenMapRealVector v1 = new OpenMapRealVector(vec1);
612
RealVector v_mapAdd = v1.mapAdd(2.0d);
613
double[] result_mapAdd = {3d, 4d, 5d};
614
assertClose("compare vectors" ,result_mapAdd,v_mapAdd.getData(),normTolerance);
617
RealVector v_mapAddToSelf = v1.copy();
618
v_mapAddToSelf.mapAddToSelf(2.0d);
619
double[] result_mapAddToSelf = {3d, 4d, 5d};
620
assertClose("compare vectors" ,result_mapAddToSelf,v_mapAddToSelf.getData(),normTolerance);
623
RealVector v_mapSubtract = v1.mapSubtract(2.0d);
624
double[] result_mapSubtract = {-1d, 0d, 1d};
625
assertClose("compare vectors" ,result_mapSubtract,v_mapSubtract.getData(),normTolerance);
628
RealVector v_mapSubtractToSelf = v1.copy();
629
v_mapSubtractToSelf.mapSubtractToSelf(2.0d);
630
double[] result_mapSubtractToSelf = {-1d, 0d, 1d};
631
assertClose("compare vectors" ,result_mapSubtractToSelf,v_mapSubtractToSelf.getData(),normTolerance);
634
RealVector v_mapMultiply = v1.mapMultiply(2.0d);
635
double[] result_mapMultiply = {2d, 4d, 6d};
636
assertClose("compare vectors" ,result_mapMultiply,v_mapMultiply.getData(),normTolerance);
639
RealVector v_mapMultiplyToSelf = v1.copy();
640
v_mapMultiplyToSelf.mapMultiplyToSelf(2.0d);
641
double[] result_mapMultiplyToSelf = {2d, 4d, 6d};
642
assertClose("compare vectors" ,result_mapMultiplyToSelf,v_mapMultiplyToSelf.getData(),normTolerance);
645
RealVector v_mapDivide = v1.mapDivide(2.0d);
646
double[] result_mapDivide = {.5d, 1d, 1.5d};
647
assertClose("compare vectors" ,result_mapDivide,v_mapDivide.getData(),normTolerance);
650
RealVector v_mapDivideToSelf = v1.copy();
651
v_mapDivideToSelf.mapDivideToSelf(2.0d);
652
double[] result_mapDivideToSelf = {.5d, 1d, 1.5d};
653
assertClose("compare vectors" ,result_mapDivideToSelf,v_mapDivideToSelf.getData(),normTolerance);
656
RealVector v_mapPow = v1.mapPow(2.0d);
657
double[] result_mapPow = {1d, 4d, 9d};
658
assertClose("compare vectors" ,result_mapPow,v_mapPow.getData(),normTolerance);
661
RealVector v_mapPowToSelf = v1.copy();
662
v_mapPowToSelf.mapPowToSelf(2.0d);
663
double[] result_mapPowToSelf = {1d, 4d, 9d};
664
assertClose("compare vectors" ,result_mapPowToSelf,v_mapPowToSelf.getData(),normTolerance);
667
RealVector v_mapExp = v1.mapExp();
668
double[] result_mapExp = {2.718281828459045e+00d,7.389056098930650e+00d, 2.008553692318767e+01d};
669
assertClose("compare vectors" ,result_mapExp,v_mapExp.getData(),normTolerance);
672
RealVector v_mapExpToSelf = v1.copy();
673
v_mapExpToSelf.mapExpToSelf();
674
double[] result_mapExpToSelf = {2.718281828459045e+00d,7.389056098930650e+00d, 2.008553692318767e+01d};
675
assertClose("compare vectors" ,result_mapExpToSelf,v_mapExpToSelf.getData(),normTolerance);
679
RealVector v_mapExpm1 = v1.mapExpm1();
680
double[] result_mapExpm1 = {1.718281828459045d,6.38905609893065d, 19.085536923187668d};
681
assertClose("compare vectors" ,result_mapExpm1,v_mapExpm1.getData(),normTolerance);
684
RealVector v_mapExpm1ToSelf = v1.copy();
685
v_mapExpm1ToSelf.mapExpm1ToSelf();
686
double[] result_mapExpm1ToSelf = {1.718281828459045d,6.38905609893065d, 19.085536923187668d};
687
assertClose("compare vectors" ,result_mapExpm1ToSelf,v_mapExpm1ToSelf.getData(),normTolerance);
690
RealVector v_mapLog = v1.mapLog();
691
double[] result_mapLog = {0d,6.931471805599453e-01d, 1.098612288668110e+00d};
692
assertClose("compare vectors" ,result_mapLog,v_mapLog.getData(),normTolerance);
695
RealVector v_mapLogToSelf = v1.copy();
696
v_mapLogToSelf.mapLogToSelf();
697
double[] result_mapLogToSelf = {0d,6.931471805599453e-01d, 1.098612288668110e+00d};
698
assertClose("compare vectors" ,result_mapLogToSelf,v_mapLogToSelf.getData(),normTolerance);
701
RealVector v_mapLog10 = v1.mapLog10();
702
double[] result_mapLog10 = {0d,3.010299956639812e-01d, 4.771212547196624e-01d};
703
assertClose("compare vectors" ,result_mapLog10,v_mapLog10.getData(),normTolerance);
706
RealVector v_mapLog10ToSelf = v1.copy();
707
v_mapLog10ToSelf.mapLog10ToSelf();
708
double[] result_mapLog10ToSelf = {0d,3.010299956639812e-01d, 4.771212547196624e-01d};
709
assertClose("compare vectors" ,result_mapLog10ToSelf,v_mapLog10ToSelf.getData(),normTolerance);
712
RealVector v_mapLog1p = v1.mapLog1p();
713
double[] result_mapLog1p = {0.6931471805599453d,1.0986122886681096d,1.3862943611198906d};
714
assertClose("compare vectors" ,result_mapLog1p,v_mapLog1p.getData(),normTolerance);
717
RealVector v_mapLog1pToSelf = v1.copy();
718
v_mapLog1pToSelf.mapLog1pToSelf();
719
double[] result_mapLog1pToSelf = {0.6931471805599453d,1.0986122886681096d,1.3862943611198906d};
720
assertClose("compare vectors" ,result_mapLog1pToSelf,v_mapLog1pToSelf.getData(),normTolerance);
723
RealVector v_mapCosh = v1.mapCosh();
724
double[] result_mapCosh = {1.543080634815244e+00d,3.762195691083631e+00d, 1.006766199577777e+01d};
725
assertClose("compare vectors" ,result_mapCosh,v_mapCosh.getData(),normTolerance);
728
RealVector v_mapCoshToSelf = v1.copy();
729
v_mapCoshToSelf.mapCoshToSelf();
730
double[] result_mapCoshToSelf = {1.543080634815244e+00d,3.762195691083631e+00d, 1.006766199577777e+01d};
731
assertClose("compare vectors" ,result_mapCoshToSelf,v_mapCoshToSelf.getData(),normTolerance);
734
RealVector v_mapSinh = v1.mapSinh();
735
double[] result_mapSinh = {1.175201193643801e+00d,3.626860407847019e+00d, 1.001787492740990e+01d};
736
assertClose("compare vectors" ,result_mapSinh,v_mapSinh.getData(),normTolerance);
739
RealVector v_mapSinhToSelf = v1.copy();
740
v_mapSinhToSelf.mapSinhToSelf();
741
double[] result_mapSinhToSelf = {1.175201193643801e+00d,3.626860407847019e+00d, 1.001787492740990e+01d};
742
assertClose("compare vectors" ,result_mapSinhToSelf,v_mapSinhToSelf.getData(),normTolerance);
745
RealVector v_mapTanh = v1.mapTanh();
746
double[] result_mapTanh = {7.615941559557649e-01d,9.640275800758169e-01d,9.950547536867305e-01d};
747
assertClose("compare vectors" ,result_mapTanh,v_mapTanh.getData(),normTolerance);
750
RealVector v_mapTanhToSelf = v1.copy();
751
v_mapTanhToSelf.mapTanhToSelf();
752
double[] result_mapTanhToSelf = {7.615941559557649e-01d,9.640275800758169e-01d,9.950547536867305e-01d};
753
assertClose("compare vectors" ,result_mapTanhToSelf,v_mapTanhToSelf.getData(),normTolerance);
756
RealVector v_mapCos = v1.mapCos();
757
double[] result_mapCos = {5.403023058681398e-01d,-4.161468365471424e-01d, -9.899924966004454e-01d};
758
assertClose("compare vectors" ,result_mapCos,v_mapCos.getData(),normTolerance);
761
RealVector v_mapCosToSelf = v1.copy();
762
v_mapCosToSelf.mapCosToSelf();
763
double[] result_mapCosToSelf = {5.403023058681398e-01d,-4.161468365471424e-01d, -9.899924966004454e-01d};
764
assertClose("compare vectors" ,result_mapCosToSelf,v_mapCosToSelf.getData(),normTolerance);
767
RealVector v_mapSin = v1.mapSin();
768
double[] result_mapSin = {8.414709848078965e-01d,9.092974268256817e-01d,1.411200080598672e-01d};
769
assertClose("compare vectors" ,result_mapSin,v_mapSin.getData(),normTolerance);
772
RealVector v_mapSinToSelf = v1.copy();
773
v_mapSinToSelf.mapSinToSelf();
774
double[] result_mapSinToSelf = {8.414709848078965e-01d,9.092974268256817e-01d,1.411200080598672e-01d};
775
assertClose("compare vectors" ,result_mapSinToSelf,v_mapSinToSelf.getData(),normTolerance);
778
RealVector v_mapTan = v1.mapTan();
779
double[] result_mapTan = {1.557407724654902e+00d,-2.185039863261519e+00d,-1.425465430742778e-01d};
780
assertClose("compare vectors" ,result_mapTan,v_mapTan.getData(),normTolerance);
783
RealVector v_mapTanToSelf = v1.copy();
784
v_mapTanToSelf.mapTanToSelf();
785
double[] result_mapTanToSelf = {1.557407724654902e+00d,-2.185039863261519e+00d,-1.425465430742778e-01d};
786
assertClose("compare vectors" ,result_mapTanToSelf,v_mapTanToSelf.getData(),normTolerance);
788
double[] vat_a = {0d, 0.5d, 1.0d};
789
OpenMapRealVector vat = new OpenMapRealVector(vat_a);
792
RealVector v_mapAcos = vat.mapAcos();
793
double[] result_mapAcos = {1.570796326794897e+00d,1.047197551196598e+00d, 0.0d};
794
assertClose("compare vectors" ,result_mapAcos,v_mapAcos.getData(),normTolerance);
797
RealVector v_mapAcosToSelf = vat.copy();
798
v_mapAcosToSelf.mapAcosToSelf();
799
double[] result_mapAcosToSelf = {1.570796326794897e+00d,1.047197551196598e+00d, 0.0d};
800
assertClose("compare vectors" ,result_mapAcosToSelf,v_mapAcosToSelf.getData(),normTolerance);
803
RealVector v_mapAsin = vat.mapAsin();
804
double[] result_mapAsin = {0.0d,5.235987755982989e-01d,1.570796326794897e+00d};
805
assertClose("compare vectors" ,result_mapAsin,v_mapAsin.getData(),normTolerance);
808
RealVector v_mapAsinToSelf = vat.copy();
809
v_mapAsinToSelf.mapAsinToSelf();
810
double[] result_mapAsinToSelf = {0.0d,5.235987755982989e-01d,1.570796326794897e+00d};
811
assertClose("compare vectors" ,result_mapAsinToSelf,v_mapAsinToSelf.getData(),normTolerance);
814
RealVector v_mapAtan = vat.mapAtan();
815
double[] result_mapAtan = {0.0d,4.636476090008061e-01d,7.853981633974483e-01d};
816
assertClose("compare vectors" ,result_mapAtan,v_mapAtan.getData(),normTolerance);
819
RealVector v_mapAtanToSelf = vat.copy();
820
v_mapAtanToSelf.mapAtanToSelf();
821
double[] result_mapAtanToSelf = {0.0d,4.636476090008061e-01d,7.853981633974483e-01d};
822
assertClose("compare vectors" ,result_mapAtanToSelf,v_mapAtanToSelf.getData(),normTolerance);
825
RealVector v_mapInv = v1.mapInv();
826
double[] result_mapInv = {1d,0.5d,3.333333333333333e-01d};
827
assertClose("compare vectors" ,result_mapInv,v_mapInv.getData(),normTolerance);
830
RealVector v_mapInvToSelf = v1.copy();
831
v_mapInvToSelf.mapInvToSelf();
832
double[] result_mapInvToSelf = {1d,0.5d,3.333333333333333e-01d};
833
assertClose("compare vectors" ,result_mapInvToSelf,v_mapInvToSelf.getData(),normTolerance);
835
double[] abs_a = {-1.0d, 0.0d, 1.0d};
836
OpenMapRealVector abs_v = new OpenMapRealVector(abs_a);
838
//octave = abs(abs_v)
839
RealVector v_mapAbs = abs_v.mapAbs();
840
double[] result_mapAbs = {1d,0d,1d};
841
assertClose("compare vectors" ,result_mapAbs,v_mapAbs.getData(),normTolerance);
843
//octave = abs(abs_v)
844
RealVector v_mapAbsToSelf = abs_v.copy();
845
v_mapAbsToSelf.mapAbsToSelf();
846
double[] result_mapAbsToSelf = {1d,0d,1d};
847
assertClose("compare vectors" ,result_mapAbsToSelf,v_mapAbsToSelf.getData(),normTolerance);
850
RealVector v_mapSqrt = v1.mapSqrt();
851
double[] result_mapSqrt = {1d,1.414213562373095e+00d,1.732050807568877e+00d};
852
assertClose("compare vectors" ,result_mapSqrt,v_mapSqrt.getData(),normTolerance);
855
RealVector v_mapSqrtToSelf = v1.copy();
856
v_mapSqrtToSelf.mapSqrtToSelf();
857
double[] result_mapSqrtToSelf = {1d,1.414213562373095e+00d,1.732050807568877e+00d};
858
assertClose("compare vectors" ,result_mapSqrtToSelf,v_mapSqrtToSelf.getData(),normTolerance);
860
double[] cbrt_a = {-2.0d, 0.0d, 2.0d};
861
OpenMapRealVector cbrt_v = new OpenMapRealVector(cbrt_a);
864
RealVector v_mapCbrt = cbrt_v.mapCbrt();
865
double[] result_mapCbrt = {-1.2599210498948732d,0d,1.2599210498948732d};
866
assertClose("compare vectors" ,result_mapCbrt,v_mapCbrt.getData(),normTolerance);
869
RealVector v_mapCbrtToSelf = cbrt_v.copy();
870
v_mapCbrtToSelf.mapCbrtToSelf();
871
double[] result_mapCbrtToSelf = {-1.2599210498948732d,0d,1.2599210498948732d};
872
assertClose("compare vectors" ,result_mapCbrtToSelf,v_mapCbrtToSelf.getData(),normTolerance);
874
double[] ceil_a = {-1.1d, 0.9d, 1.1d};
875
OpenMapRealVector ceil_v = new OpenMapRealVector(ceil_a);
877
//octave = ceil(ceil_v)
878
RealVector v_mapCeil = ceil_v.mapCeil();
879
double[] result_mapCeil = {-1d,1d,2d};
880
assertClose("compare vectors" ,result_mapCeil,v_mapCeil.getData(),normTolerance);
882
//octave = ceil(ceil_v)
883
RealVector v_mapCeilToSelf = ceil_v.copy();
884
v_mapCeilToSelf.mapCeilToSelf();
885
double[] result_mapCeilToSelf = {-1d,1d,2d};
886
assertClose("compare vectors" ,result_mapCeilToSelf,v_mapCeilToSelf.getData(),normTolerance);
888
//octave = floor(ceil_v)
889
RealVector v_mapFloor = ceil_v.mapFloor();
890
double[] result_mapFloor = {-2d,0d,1d};
891
assertClose("compare vectors" ,result_mapFloor,v_mapFloor.getData(),normTolerance);
893
//octave = floor(ceil_v)
894
RealVector v_mapFloorToSelf = ceil_v.copy();
895
v_mapFloorToSelf.mapFloorToSelf();
896
double[] result_mapFloorToSelf = {-2d,0d,1d};
897
assertClose("compare vectors" ,result_mapFloorToSelf,v_mapFloorToSelf.getData(),normTolerance);
900
RealVector v_mapRint = ceil_v.mapRint();
901
double[] result_mapRint = {-1d,1d,1d};
902
assertClose("compare vectors" ,result_mapRint,v_mapRint.getData(),normTolerance);
905
RealVector v_mapRintToSelf = ceil_v.copy();
906
v_mapRintToSelf.mapRintToSelf();
907
double[] result_mapRintToSelf = {-1d,1d,1d};
908
assertClose("compare vectors" ,result_mapRintToSelf,v_mapRintToSelf.getData(),normTolerance);
911
RealVector v_mapSignum = ceil_v.mapSignum();
912
double[] result_mapSignum = {-1d,1d,1d};
913
assertClose("compare vectors" ,result_mapSignum,v_mapSignum.getData(),normTolerance);
916
RealVector v_mapSignumToSelf = ceil_v.copy();
917
v_mapSignumToSelf.mapSignumToSelf();
918
double[] result_mapSignumToSelf = {-1d,1d,1d};
919
assertClose("compare vectors" ,result_mapSignumToSelf,v_mapSignumToSelf.getData(),normTolerance);
922
// Is with the used resolutions of limited value as test
924
RealVector v_mapUlp = ceil_v.mapUlp();
925
double[] result_mapUlp = {2.220446049250313E-16d,1.1102230246251565E-16d,2.220446049250313E-16d};
926
assertClose("compare vectors" ,result_mapUlp,v_mapUlp.getData(),normTolerance);
929
RealVector v_mapUlpToSelf = ceil_v.copy();
930
v_mapUlpToSelf.mapUlpToSelf();
931
double[] result_mapUlpToSelf = {2.220446049250313E-16d,1.1102230246251565E-16d,2.220446049250313E-16d};
932
assertClose("compare vectors" ,result_mapUlpToSelf,v_mapUlpToSelf.getData(),normTolerance);
936
public void testBasicFunctions() {
937
OpenMapRealVector v1 = new OpenMapRealVector(vec1);
938
OpenMapRealVector v2 = new OpenMapRealVector(vec2);
939
OpenMapRealVector v_null = new OpenMapRealVector(vec_null);
941
SparseRealVectorTestImpl v2_t = new SparseRealVectorTestImpl(vec2);
943
//octave = sqrt(sumsq(v1))
944
double d_getNorm = v1.getNorm();
945
assertEquals("compare values ", 3.7416573867739413,d_getNorm);
947
double d_getL1Norm = v1.getL1Norm();
948
assertEquals("compare values ",6.0, d_getL1Norm);
950
double d_getLInfNorm = v1.getLInfNorm();
951
assertEquals("compare values ",6.0, d_getLInfNorm);
953
//octave = sqrt(sumsq(v1-v2))
954
double dist = v1.getDistance(v2);
955
assertEquals("compare values ",v1.subtract(v2).getNorm(), dist );
957
//octave = sqrt(sumsq(v1-v2))
958
double dist_2 = v1.getDistance(v2_t);
959
assertEquals("compare values ", v1.subtract(v2).getNorm(),dist_2 );
962
double d_getL1Distance = v1. getL1Distance(v2);
963
assertEquals("compare values ",9d, d_getL1Distance );
965
double d_getL1Distance_2 = v1. getL1Distance(v2_t);
966
assertEquals("compare values ",9d, d_getL1Distance_2 );
969
double d_getLInfDistance = v1. getLInfDistance(v2);
970
assertEquals("compare values ",3d, d_getLInfDistance );
972
double d_getLInfDistance_2 = v1. getLInfDistance(v2_t);
973
assertEquals("compare values ",3d, d_getLInfDistance_2 );
976
OpenMapRealVector v_add = v1.add(v2);
977
double[] result_add = {5d, 7d, 9d};
978
assertClose("compare vect" ,v_add.getData(),result_add,normTolerance);
980
SparseRealVectorTestImpl vt2 = new SparseRealVectorTestImpl(vec2);
981
RealVector v_add_i = v1.add(vt2);
982
double[] result_add_i = {5d, 7d, 9d};
983
assertClose("compare vect" ,v_add_i.getData(),result_add_i,normTolerance);
986
OpenMapRealVector v_subtract = v1.subtract(v2);
987
double[] result_subtract = {-3d, -3d, -3d};
988
assertClose("compare vect" ,v_subtract.getData(),result_subtract,normTolerance);
990
RealVector v_subtract_i = v1.subtract(vt2);
991
double[] result_subtract_i = {-3d, -3d, -3d};
992
assertClose("compare vect" ,v_subtract_i.getData(),result_subtract_i,normTolerance);
995
RealVector v_ebeMultiply = v1.ebeMultiply(v2);
996
double[] result_ebeMultiply = {4d, 10d, 18d};
997
assertClose("compare vect" ,v_ebeMultiply.getData(),result_ebeMultiply,normTolerance);
999
RealVector v_ebeMultiply_2 = v1.ebeMultiply(v2_t);
1000
double[] result_ebeMultiply_2 = {4d, 10d, 18d};
1001
assertClose("compare vect" ,v_ebeMultiply_2.getData(),result_ebeMultiply_2,normTolerance);
1004
RealVector v_ebeDivide = v1.ebeDivide(v2);
1005
double[] result_ebeDivide = {0.25d, 0.4d, 0.5d};
1006
assertClose("compare vect" ,v_ebeDivide.getData(),result_ebeDivide,normTolerance);
1008
RealVector v_ebeDivide_2 = v1.ebeDivide(v2_t);
1009
double[] result_ebeDivide_2 = {0.25d, 0.4d, 0.5d};
1010
assertClose("compare vect" ,v_ebeDivide_2.getData(),result_ebeDivide_2,normTolerance);
1012
// octave dot(v1,v2)
1013
double dot = v1.dotProduct(v2);
1014
assertEquals("compare val ",32d, dot);
1016
// octave dot(v1,v2_t)
1017
double dot_2 = v1.dotProduct(v2_t);
1018
assertEquals("compare val ",32d, dot_2);
1020
RealMatrix m_outerProduct = v1.outerProduct(v2);
1021
assertEquals("compare val ",4d, m_outerProduct.getEntry(0,0));
1023
RealMatrix m_outerProduct_2 = v1.outerProduct(v2_t);
1024
assertEquals("compare val ",4d, m_outerProduct_2.getEntry(0,0));
1026
RealVector v_unitVector = v1.unitVector();
1027
RealVector v_unitVector_2 = v1.mapDivide(v1.getNorm());
1028
assertClose("compare vect" ,v_unitVector.getData(),v_unitVector_2.getData(),normTolerance);
1031
v_null.unitVector();
1032
fail("Expecting ArithmeticException");
1033
} catch (ArithmeticException ex) {
1034
// expected behavior
1035
} catch (Exception e) {
1036
fail("wrong exception caught");
1039
OpenMapRealVector v_unitize = (OpenMapRealVector)v1.copy();
1040
v_unitize.unitize();
1041
assertClose("compare vect" ,v_unitVector_2.getData(),v_unitize.getData(),normTolerance);
1044
fail("Expecting ArithmeticException");
1045
} catch (ArithmeticException ex) {
1046
// expected behavior
1047
} catch (Exception e) {
1048
fail("wrong exception caught");
1051
RealVector v_projection = v1.projection(v2);
1052
double[] result_projection = {1.662337662337662, 2.0779220779220777, 2.493506493506493};
1053
assertClose("compare vect", v_projection.getData(), result_projection, normTolerance);
1055
RealVector v_projection_2 = v1.projection(v2_t);
1056
double[] result_projection_2 = {1.662337662337662, 2.0779220779220777, 2.493506493506493};
1057
assertClose("compare vect", v_projection_2.getData(), result_projection_2, normTolerance);
1061
public void testMisc() {
1062
OpenMapRealVector v1 = new OpenMapRealVector(vec1);
1064
String out1 = v1.toString();
1065
assertTrue("some output ", out1.length()!=0);
1067
v1.checkVectorDimensions(2);
1068
fail("IllegalArgumentException expected");
1069
} catch (IllegalArgumentException ex) {
1070
// expected behavior
1071
} catch (Exception e) {
1072
fail("wrong exception caught");
1078
public void testPredicates() {
1080
OpenMapRealVector v = new OpenMapRealVector(new double[] { 0, 1, 2 });
1082
assertFalse(v.isNaN());
1083
v.setEntry(1, Double.NaN);
1084
assertTrue(v.isNaN());
1086
assertFalse(v.isInfinite());
1087
v.setEntry(0, Double.POSITIVE_INFINITY);
1088
assertFalse(v.isInfinite()); // NaN has higher priority than infinity
1090
assertTrue(v.isInfinite());
1093
assertEquals(v, new OpenMapRealVector(new double[] { 0, 1, 2 }));
1094
assertNotSame(v, new OpenMapRealVector(new double[] { 0, 1, 2 + Math.ulp(2)}));
1095
assertNotSame(v, new OpenMapRealVector(new double[] { 0, 1, 2, 3 }));
1099
public void testSerial() {
1100
OpenMapRealVector v = new OpenMapRealVector(new double[] { 0, 1, 2 });
1101
assertEquals(v,TestUtils.serializeAndRecover(v));
1104
/** verifies that two vectors are close (sup norm) */
1105
protected void assertClose(String msg, double[] m, double[] n,
1107
if (m.length != n.length) {
1108
fail("vectors have different lengths");
1110
for (int i = 0; i < m.length; i++) {
1111
assertEquals(msg + " " + i + " elements differ", m[i],n[i],tolerance);