View Javadoc
1   /*
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
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
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.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
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   * Test cases for the {@link OpenMapRealVector} class.
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          //SparseRealVector v4 = new SparseRealVector(vec4, 3, 2);
75          //Assert.assertEquals("testData len", 2, v4.getDimension());
76          //Assert.assertEquals("testData is 4.0 ", 4.0, v4.getEntry(0));
77          //try {
78          //    new SparseRealVector(vec4, 8, 3);
79          //    Assert.fail("MathIllegalArgumentException expected");
80          //} catch (MathIllegalArgumentException ex) {
81              // expected behavior
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     /* Check that the operations do not throw an exception (cf. MATH-645). */
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 }