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.exception.MathIllegalArgumentException;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  /**
29   * Test cases for the {@link ArrayRealVector} class.
30   *
31   */
32  public class ArrayRealVectorTest extends RealVectorAbstractTest {
33  
34  
35      @Override
36      public RealVector create(final double[] data) {
37          return new ArrayRealVector(data, true);
38      }
39  
40      @Test
41      public void testConstructors() {
42          final double[] vec1 = {1d, 2d, 3d};
43          final double[] vec3 = {7d, 8d, 9d};
44          final double[] vec4 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d};
45          final Double[] dvec1 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d};
46  
47          ArrayRealVector v0 = new ArrayRealVector();
48          Assert.assertEquals("testData len", 0, v0.getDimension());
49  
50          ArrayRealVector v1 = new ArrayRealVector(7);
51          Assert.assertEquals("testData len", 7, v1.getDimension());
52          Assert.assertEquals("testData is 0.0 ", 0.0, v1.getEntry(6), 0);
53  
54          ArrayRealVector v2 = new ArrayRealVector(5, 1.23);
55          Assert.assertEquals("testData len", 5, v2.getDimension());
56          Assert.assertEquals("testData is 1.23 ", 1.23, v2.getEntry(4), 0);
57  
58          ArrayRealVector v3 = new ArrayRealVector(vec1);
59          Assert.assertEquals("testData len", 3, v3.getDimension());
60          Assert.assertEquals("testData is 2.0 ", 2.0, v3.getEntry(1), 0);
61  
62          ArrayRealVector v3_bis = new ArrayRealVector(vec1, true);
63          Assert.assertEquals("testData len", 3, v3_bis.getDimension());
64          Assert.assertEquals("testData is 2.0 ", 2.0, v3_bis.getEntry(1), 0);
65          Assert.assertNotSame(v3_bis.getDataRef(), vec1);
66          Assert.assertNotSame(v3_bis.toArray(), vec1);
67  
68          ArrayRealVector v3_ter = new ArrayRealVector(vec1, false);
69          Assert.assertEquals("testData len", 3, v3_ter.getDimension());
70          Assert.assertEquals("testData is 2.0 ", 2.0, v3_ter.getEntry(1), 0);
71          Assert.assertSame(v3_ter.getDataRef(), vec1);
72          Assert.assertNotSame(v3_ter.toArray(), vec1);
73  
74          ArrayRealVector v4 = new ArrayRealVector(vec4, 3, 2);
75          Assert.assertEquals("testData len", 2, v4.getDimension());
76          Assert.assertEquals("testData is 4.0 ", 4.0, v4.getEntry(0), 0);
77          try {
78              new ArrayRealVector(vec4, 8, 3);
79              Assert.fail("MathIllegalArgumentException expected");
80          } catch (MathIllegalArgumentException ex) {
81              // expected behavior
82          }
83  
84          RealVector v5_i = new ArrayRealVector(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          ArrayRealVector v5 = new ArrayRealVector(dvec1);
89          Assert.assertEquals("testData len", 9, v5.getDimension());
90          Assert.assertEquals("testData is 9.0 ", 9.0, v5.getEntry(8), 0);
91  
92          ArrayRealVector v6 = new ArrayRealVector(dvec1, 3, 2);
93          Assert.assertEquals("testData len", 2, v6.getDimension());
94          Assert.assertEquals("testData is 4.0 ", 4.0, v6.getEntry(0), 0);
95          try {
96              new ArrayRealVector(dvec1, 8, 3);
97              Assert.fail("MathIllegalArgumentException expected");
98          } catch (MathIllegalArgumentException ex) {
99              // expected behavior
100         }
101 
102         ArrayRealVector v7 = new ArrayRealVector(v1);
103         Assert.assertEquals("testData len", 7, v7.getDimension());
104         Assert.assertEquals("testData is 0.0 ", 0.0, v7.getEntry(6), 0);
105 
106         RealVectorTestImpl v7_i = new RealVectorTestImpl(vec1);
107 
108         ArrayRealVector v7_2 = new ArrayRealVector(v7_i);
109         Assert.assertEquals("testData len", 3, v7_2.getDimension());
110         Assert.assertEquals("testData is 0.0 ", 2.0d, v7_2.getEntry(1), 0);
111 
112         ArrayRealVector v8 = new ArrayRealVector(v1, true);
113         Assert.assertEquals("testData len", 7, v8.getDimension());
114         Assert.assertEquals("testData is 0.0 ", 0.0, v8.getEntry(6), 0);
115         Assert.assertNotSame("testData not same object ", v1.getDataRef(), v8.getDataRef());
116 
117         ArrayRealVector v8_2 = new ArrayRealVector(v1, false);
118         Assert.assertEquals("testData len", 7, v8_2.getDimension());
119         Assert.assertEquals("testData is 0.0 ", 0.0, v8_2.getEntry(6), 0);
120         Assert.assertEquals("testData same object ", v1.getDataRef(), v8_2.getDataRef());
121 
122         ArrayRealVector v9 = new ArrayRealVector(v1, v3);
123         Assert.assertEquals("testData len", 10, v9.getDimension());
124         Assert.assertEquals("testData is 1.0 ", 1.0, v9.getEntry(7), 0);
125 
126         ArrayRealVector v10 = new ArrayRealVector(v2, new RealVectorTestImpl(vec3));
127         Assert.assertEquals("testData len", 8, v10.getDimension());
128         Assert.assertEquals("testData is 1.23 ", 1.23, v10.getEntry(4), 0);
129         Assert.assertEquals("testData is 7.0 ", 7.0, v10.getEntry(5), 0);
130 
131         ArrayRealVector v11 = new ArrayRealVector(new RealVectorTestImpl(vec3), v2);
132         Assert.assertEquals("testData len", 8, v11.getDimension());
133         Assert.assertEquals("testData is 9.0 ", 9.0, v11.getEntry(2), 0);
134         Assert.assertEquals("testData is 1.23 ", 1.23, v11.getEntry(3), 0);
135 
136         ArrayRealVector v12 = new ArrayRealVector(v2, vec3);
137         Assert.assertEquals("testData len", 8, v12.getDimension());
138         Assert.assertEquals("testData is 1.23 ", 1.23, v12.getEntry(4), 0);
139         Assert.assertEquals("testData is 7.0 ", 7.0, v12.getEntry(5), 0);
140 
141         ArrayRealVector v13 = new ArrayRealVector(vec3, v2);
142         Assert.assertEquals("testData len", 8, v13.getDimension());
143         Assert.assertEquals("testData is 9.0 ", 9.0, v13.getEntry(2), 0);
144         Assert.assertEquals("testData is 1.23 ", 1.23, v13.getEntry(3), 0);
145 
146         ArrayRealVector v14 = new ArrayRealVector(vec3, vec4);
147         Assert.assertEquals("testData len", 12, v14.getDimension());
148         Assert.assertEquals("testData is 9.0 ", 9.0, v14.getEntry(2), 0);
149         Assert.assertEquals("testData is 1.0 ", 1.0, v14.getEntry(3), 0);
150 
151     }
152 
153     @Test
154     public void testGetDataRef() {
155         final double[] data = {1d, 2d, 3d, 4d};
156         final ArrayRealVector v = new ArrayRealVector(data);
157         v.getDataRef()[0] = 0d;
158         Assert.assertEquals("", 0d, v.getEntry(0), 0);
159     }
160 
161     @Test
162     public void testPredicates() {
163 
164         Assert.assertEquals(create(new double[] { Double.NaN, 1, 2 }).hashCode(),
165                      create(new double[] { 0, Double.NaN, 2 }).hashCode());
166 
167         Assert.assertTrue(create(new double[] { Double.NaN, 1, 2 }).hashCode() !=
168                    create(new double[] { 0, 1, 2 }).hashCode());
169     }
170 
171     @Test
172     public void testZeroVectors() {
173         Assert.assertEquals(0, new ArrayRealVector(new double[0]).getDimension());
174         Assert.assertEquals(0, new ArrayRealVector(new double[0], true).getDimension());
175         Assert.assertEquals(0, new ArrayRealVector(new double[0], false).getDimension());
176     }
177 }