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