1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.linear;
23
24 import org.hipparchus.analysis.UnivariateFunction;
25 import org.hipparchus.analysis.function.Abs;
26 import org.hipparchus.analysis.function.Acos;
27 import org.hipparchus.analysis.function.Asin;
28 import org.hipparchus.analysis.function.Atan;
29 import org.hipparchus.analysis.function.Cbrt;
30 import org.hipparchus.analysis.function.Ceil;
31 import org.hipparchus.analysis.function.Cos;
32 import org.hipparchus.analysis.function.Cosh;
33 import org.hipparchus.analysis.function.Exp;
34 import org.hipparchus.analysis.function.Expm1;
35 import org.hipparchus.analysis.function.Floor;
36 import org.hipparchus.analysis.function.Log1p;
37 import org.hipparchus.analysis.function.Power;
38 import org.hipparchus.analysis.function.Rint;
39 import org.hipparchus.analysis.function.Sin;
40 import org.hipparchus.analysis.function.Sinh;
41 import org.hipparchus.analysis.function.Sqrt;
42 import org.hipparchus.analysis.function.Tan;
43 import org.hipparchus.analysis.function.Tanh;
44 import org.junit.Assert;
45 import org.junit.Test;
46
47
48
49
50
51 public class SparseRealVectorTest extends RealVectorAbstractTest {
52
53 @Override
54 public RealVector create(double[] data) {
55 return new OpenMapRealVector(data);
56 }
57
58 @Test
59 public void testConstructors() {
60 final double[] vec1 = {1d, 2d, 3d};
61 final Double[] dvec1 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d};
62
63 OpenMapRealVector v0 = new OpenMapRealVector();
64 Assert.assertEquals("testData len", 0, v0.getDimension());
65
66 OpenMapRealVector v1 = new OpenMapRealVector(7);
67 Assert.assertEquals("testData len", 7, v1.getDimension());
68 Assert.assertEquals("testData is 0.0 ", 0.0, v1.getEntry(6), 0);
69
70 OpenMapRealVector v3 = new OpenMapRealVector(vec1);
71 Assert.assertEquals("testData len", 3, v3.getDimension());
72 Assert.assertEquals("testData is 2.0 ", 2.0, v3.getEntry(1), 0);
73
74
75
76
77
78
79
80
81
82
83
84 RealVector v5_i = new OpenMapRealVector(dvec1);
85 Assert.assertEquals("testData len", 9, v5_i.getDimension());
86 Assert.assertEquals("testData is 9.0 ", 9.0, v5_i.getEntry(8), 0);
87
88 OpenMapRealVector v5 = new OpenMapRealVector(dvec1);
89 Assert.assertEquals("testData len", 9, v5.getDimension());
90 Assert.assertEquals("testData is 9.0 ", 9.0, v5.getEntry(8), 0);
91
92 OpenMapRealVector v7 = new OpenMapRealVector(v1);
93 Assert.assertEquals("testData len", 7, v7.getDimension());
94 Assert.assertEquals("testData is 0.0 ", 0.0, v7.getEntry(6), 0);
95
96 RealVectorTestImpl v7_i = new RealVectorTestImpl(vec1);
97
98 OpenMapRealVector v7_2 = new OpenMapRealVector(v7_i);
99 Assert.assertEquals("testData len", 3, v7_2.getDimension());
100 Assert.assertEquals("testData is 0.0 ", 2.0d, v7_2.getEntry(1), 0);
101
102 OpenMapRealVector v8 = new OpenMapRealVector(v1);
103 Assert.assertEquals("testData len", 7, v8.getDimension());
104 Assert.assertEquals("testData is 0.0 ", 0.0, v8.getEntry(6), 0);
105
106 }
107
108
109 @Test
110 public void testConcurrentModification() {
111 final RealVector u = new OpenMapRealVector(3, 1e-6);
112 u.setEntry(0, 1);
113 u.setEntry(1, 0);
114 u.setEntry(2, 2);
115
116 final RealVector v1 = new OpenMapRealVector(3, 1e-6);
117 v1.setEntry(0, 0);
118 v1.setEntry(1, 3);
119 v1.setEntry(2, 0);
120
121 u.ebeMultiply(v1);
122 u.ebeDivide(v1);
123 }
124
125 @Test
126 @Override
127 public void testEbeMultiplyMixedTypes() {
128 doTestEbeBinaryOperation(BinaryOperation.MUL, true, true);
129 }
130
131 @Test
132 @Override
133 public void testEbeMultiplySameType() {
134 doTestEbeBinaryOperation(BinaryOperation.MUL, false, true);
135 }
136
137 @Test
138 @Override
139 public void testEbeDivideSameType() {
140 doTestEbeBinaryOperation(BinaryOperation.DIV, false, true);
141 }
142
143 @Override
144 protected UnivariateFunction[] createFunctions() {
145 return new UnivariateFunction[] {
146 new Power(2.0), new Exp(), new Expm1(),
147 new Log1p(), new Cosh(), new Sinh(), new Tanh(), new Cos(),
148 new Sin(), new Tan(), new Acos(), new Asin(), new Atan(),
149 new Abs(), new Sqrt(), new Cbrt(), new Ceil(),
150 new Floor(), new Rint()
151 };
152 }
153
154 }