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  
23  package org.hipparchus.linear;
24  
25  import org.hipparchus.exception.LocalizedCoreFormats;
26  import org.hipparchus.exception.MathRuntimeException;
27  import org.hipparchus.linear.RealVector.Entry;
28  import org.junit.jupiter.api.Test;
29  
30  import java.util.Iterator;
31  
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  import static org.junit.jupiter.api.Assertions.assertThrows;
34  import static org.junit.jupiter.api.Assertions.fail;
35  
36  /**
37   * Tests for {@link RealVector}.
38   */
39  public class RealVectorTest extends RealVectorAbstractTest {
40  
41      @Override
42      public RealVector create(final double[] data) {
43          return new RealVectorTestImpl(data);
44      }
45  
46      @Test
47      @Override
48      public void testAppendVector() {
49          checkUnsupported(super::testAppendVector);
50      }
51  
52      @Test
53      @Override
54      public void testAppendScalar() {
55          checkUnsupported(super::testAppendScalar);
56      }
57  
58      @Test
59      @Override
60      public void testGetSubVector() {
61          checkUnsupported(super::testGetSubVector);
62      }
63  
64      @Test
65      @Override
66      public void testGetSubVectorInvalidIndex1() {
67          assertThrows(UnsupportedOperationException.class, () -> {
68              final int n = 10;
69              create(new double[n]).getSubVector(-1, 2);
70          });
71      }
72  
73      @Test
74      @Override
75      public void testGetSubVectorInvalidIndex2() {
76          assertThrows(UnsupportedOperationException.class, () -> {
77              final int n = 10;
78              create(new double[n]).getSubVector(2, 2);
79          });
80      }
81  
82      @Test
83      @Override
84      public void testGetSubVectorInvalidIndex3() {
85          assertThrows(UnsupportedOperationException.class, () -> {
86              final int n = 10;
87              create(new double[n]).getSubVector(0, 2);
88          });
89      }
90  
91      @Test
92      @Override
93      public void testGetSubVectorInvalidIndex4() {
94          assertThrows(UnsupportedOperationException.class, () -> {
95              final int n = 10;
96              create(new double[n]).getSubVector(3, 2);
97          });
98      }
99  
100     @Test
101     @Override
102     public void testSetSubVectorSameType() {
103         checkUnsupported(super::testSetSubVectorSameType);
104     }
105 
106     @Test
107     @Override
108     public void testSetSubVectorMixedType() {
109         checkUnsupported(super::testSetSubVectorMixedType);
110     }
111 
112     @Test
113     @Override
114     public void testSetSubVectorInvalidIndex1() {
115         assertThrows(UnsupportedOperationException.class, () -> create(new double[10]).setSubVector(-1, create(new double[2])));
116     }
117 
118     @Test
119     @Override
120     public void testSetSubVectorInvalidIndex2() {
121         assertThrows(UnsupportedOperationException.class, () -> create(new double[10]).setSubVector(10, create(new double[2])));
122     }
123 
124     @Test
125     @Override
126     public void testSetSubVectorInvalidIndex3() {
127         assertThrows(UnsupportedOperationException.class, () -> create(new double[10]).setSubVector(9, create(new double[2])));
128     }
129 
130     @Test
131     @Override
132     public void testIsNaN() {
133         checkUnsupported(super::testIsNaN);
134     }
135 
136     @Test
137     @Override
138     public void testIsInfinite() {
139         checkUnsupported(super::testIsInfinite);
140     }
141 
142     @Test
143     @Override
144     public void testEbeMultiplySameType() {
145         checkUnsupported(super::testEbeMultiplySameType);
146     }
147 
148     @Test
149     @Override
150     public void testEbeMultiplyMixedTypes() {
151         checkUnsupported(super::testEbeMultiplyMixedTypes);
152     }
153 
154     @Test
155     @Override
156     public void testEbeMultiplyDimensionMismatch() {
157         assertThrows(UnsupportedOperationException.class, () -> doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.MUL));
158     }
159 
160     @Test
161     @Override
162     public void testEbeDivideSameType() {
163         checkUnsupported(super::testEbeDivideSameType);
164     }
165 
166     @Test
167     @Override
168     public void testEbeDivideMixedTypes() {
169         checkUnsupported(super::testEbeDivideMixedTypes);
170     }
171 
172     @Test
173     @Override
174     public void testEbeDivideDimensionMismatch() {
175         assertThrows(UnsupportedOperationException.class, () -> doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.DIV));
176     }
177 
178     @Test
179     void testSparseIterator() {
180         /*
181          * For non-default values, use x + 1, x + 2, etc... to make sure that
182          * these values are really different from x.
183          */
184         final double x = getPreferredEntryValue();
185         final double[] data = {
186             x, x + 1d, x, x, x + 2d, x + 3d, x + 4d, x, x, x, x + 5d, x + 6d, x
187         };
188 
189         RealVector v = create(data);
190         Entry e;
191         int i = 0;
192         final double[] nonDefault = {
193             x + 1d, x + 2d, x + 3d, x + 4d, x + 5d, x + 6d
194         };
195         for (Iterator<Entry> it = v.sparseIterator(); it.hasNext(); i++) {
196             e = it.next();
197             assertEquals(nonDefault[i], e.getValue(), 0);
198         }
199         double [] onlyOne = {x, x + 1d, x};
200         v = create(onlyOne);
201         for(Iterator<Entry> it = v.sparseIterator(); it.hasNext(); ) {
202             e = it.next();
203             assertEquals(onlyOne[1], e.getValue(), 0);
204         }
205     }
206 
207     @Test
208     @Override
209     public void testSerial() {
210         checkUnsupported(super::testSerial);
211     }
212 
213     @Test
214     @Override
215     public void testEquals() {
216         checkUnsupported(super::testEquals);
217     }
218 
219     interface Thunk {
220         void call();
221     }
222 
223     private void checkUnsupported(final Thunk t) {
224         try {
225             t.call();
226             fail("an exception should have been thrown");
227         } catch (MathRuntimeException mre) {
228             assertEquals(LocalizedCoreFormats.UNSUPPORTED_OPERATION, mre.getSpecifier());
229         } catch (UnsupportedOperationException uoe) {
230             // expected
231         }
232     }
233 
234 }