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.jupiter.api.Test;
45  
46  import static org.junit.jupiter.api.Assertions.assertEquals;
47  
48  /**
49   * Test cases for the {@link OpenMapRealVector} class.
50   *
51   */
52  public class SparseRealVectorTest extends RealVectorAbstractTest {
53  
54      @Override
55      public RealVector create(double[] data) {
56          return new OpenMapRealVector(data);
57      }
58  
59      @Test
60      void testConstructors() {
61          final double[] vec1 = {1d, 2d, 3d};
62          final Double[] dvec1 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d};
63  
64          OpenMapRealVector v0 = new OpenMapRealVector();
65          assertEquals(0, v0.getDimension(), "testData len");
66  
67          OpenMapRealVector v1 = new OpenMapRealVector(7);
68          assertEquals(7, v1.getDimension(), "testData len");
69          assertEquals(0.0, v1.getEntry(6), 0, "testData is 0.0 ");
70  
71          OpenMapRealVector v3 = new OpenMapRealVector(vec1);
72          assertEquals(3, v3.getDimension(), "testData len");
73          assertEquals(2.0, v3.getEntry(1), 0, "testData is 2.0 ");
74  
75          //SparseRealVector v4 = new SparseRealVector(vec4, 3, 2);
76          //Assertions.assertEquals("testData len", 2, v4.getDimension());
77          //Assertions.assertEquals("testData is 4.0 ", 4.0, v4.getEntry(0));
78          //try {
79          //    new SparseRealVector(vec4, 8, 3);
80          //    Assertions.fail("MathIllegalArgumentException expected");
81          //} catch (MathIllegalArgumentException ex) {
82              // expected behavior
83          //}
84  
85          RealVector v5_i = new OpenMapRealVector(dvec1);
86          assertEquals(9, v5_i.getDimension(), "testData len");
87          assertEquals(9.0, v5_i.getEntry(8), 0, "testData is 9.0 ");
88  
89          OpenMapRealVector v5 = new OpenMapRealVector(dvec1);
90          assertEquals(9, v5.getDimension(), "testData len");
91          assertEquals(9.0, v5.getEntry(8), 0, "testData is 9.0 ");
92  
93          OpenMapRealVector v7 = new OpenMapRealVector(v1);
94          assertEquals(7, v7.getDimension(), "testData len");
95          assertEquals(0.0, v7.getEntry(6), 0, "testData is 0.0 ");
96  
97          RealVectorTestImpl v7_i = new RealVectorTestImpl(vec1);
98  
99          OpenMapRealVector v7_2 = new OpenMapRealVector(v7_i);
100         assertEquals(3, v7_2.getDimension(), "testData len");
101         assertEquals(2.0d, v7_2.getEntry(1), 0, "testData is 0.0 ");
102 
103         OpenMapRealVector v8 = new OpenMapRealVector(v1);
104         assertEquals(7, v8.getDimension(), "testData len");
105         assertEquals(0.0, v8.getEntry(6), 0, "testData is 0.0 ");
106 
107     }
108 
109     /* Check that the operations do not throw an exception (cf. MATH-645). */
110     @Test
111     void testConcurrentModification() {
112         final RealVector u = new OpenMapRealVector(3, 1e-6);
113         u.setEntry(0, 1);
114         u.setEntry(1, 0);
115         u.setEntry(2, 2);
116 
117         final RealVector v1 = new OpenMapRealVector(3, 1e-6);
118         v1.setEntry(0, 0);
119         v1.setEntry(1, 3);
120         v1.setEntry(2, 0);
121 
122         u.ebeMultiply(v1);
123         u.ebeDivide(v1);
124     }
125 
126     @Test
127     @Override
128     public void testEbeMultiplyMixedTypes() {
129         doTestEbeBinaryOperation(BinaryOperation.MUL, true, true);
130     }
131 
132     @Test
133     @Override
134     public void testEbeMultiplySameType() {
135         doTestEbeBinaryOperation(BinaryOperation.MUL, false, true);
136     }
137 
138     @Test
139     @Override
140     public void testEbeDivideSameType() {
141         doTestEbeBinaryOperation(BinaryOperation.DIV, false, true);
142     }
143 
144     @Override
145     protected UnivariateFunction[] createFunctions() {
146         return new UnivariateFunction[] {
147             new Power(2.0), new Exp(), new Expm1(),
148             new Log1p(), new Cosh(), new Sinh(), new Tanh(), new Cos(),
149             new Sin(), new Tan(), new Acos(), new Asin(), new Atan(),
150             new Abs(), new Sqrt(), new Cbrt(), new Ceil(),
151             new Floor(), new Rint()
152         };
153     }
154 
155 }