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 java.text.NumberFormat;
26  import java.text.ParsePosition;
27  import java.util.Locale;
28  
29  import org.hipparchus.exception.MathIllegalStateException;
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  public abstract class RealMatrixFormatAbstractTest {
34  
35      RealMatrixFormat realMatrixFormat = null;
36      RealMatrixFormat realMatrixFormatOctave = null;
37  
38      protected abstract Locale getLocale();
39  
40      protected abstract char getDecimalCharacter();
41  
42      public RealMatrixFormatAbstractTest() {
43          realMatrixFormat = RealMatrixFormat.getRealMatrixFormat(getLocale());
44          final NumberFormat nf = NumberFormat.getInstance(getLocale());
45          nf.setMaximumFractionDigits(2);
46          realMatrixFormatOctave = new RealMatrixFormat("[", "]", "", "", "; ", ", ", nf);
47      }
48  
49      @Test
50      public void testSimpleNoDecimals() {
51          RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1, 1, 1}, {1, 1, 1}});
52          String expected = "{{1,1,1},{1,1,1}}";
53          String actual = realMatrixFormat.format(m);
54          Assert.assertEquals(expected, actual);
55      }
56  
57      @Test
58      public void testSimpleWithDecimals() {
59          RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.23, 1.43, 1.63}, {2.46, 2.46, 2.66}});
60          String expected =
61              "{{1"    + getDecimalCharacter() +
62              "23,1" + getDecimalCharacter() +
63              "43,1" + getDecimalCharacter() +
64              "63},{2" + getDecimalCharacter() +
65              "46,2" + getDecimalCharacter() +
66              "46,2" + getDecimalCharacter() +
67              "66}}";
68          String actual = realMatrixFormat.format(m);
69          Assert.assertEquals(expected, actual);
70      }
71  
72      @Test
73      public void testSimpleWithDecimalsTrunc() {
74          RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.232323232323, 1.43, 1.63},
75                                                                      {2.46, 2.46, 2.666666666666}});
76          String expected =
77                  "{{1"    + getDecimalCharacter() +
78                  "2323232323,1" + getDecimalCharacter() +
79                  "43,1" + getDecimalCharacter() +
80                  "63},{2" + getDecimalCharacter() +
81                  "46,2" + getDecimalCharacter() +
82                  "46,2" + getDecimalCharacter() +
83                  "6666666667}}";
84          String actual = realMatrixFormat.format(m);
85          Assert.assertEquals(expected, actual);
86      }
87  
88      @Test
89      public void testNegativeComponent() {
90          RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{-1.232323232323, 1.43, 1.63},
91                                                                      {2.46, 2.46, 2.66}});
92          String expected =
93                  "{{-1"    + getDecimalCharacter() +
94                  "2323232323,1" + getDecimalCharacter() +
95                  "43,1" + getDecimalCharacter() +
96                  "63},{2" + getDecimalCharacter() +
97                  "46,2" + getDecimalCharacter() +
98                  "46,2" + getDecimalCharacter() +
99                  "66}}";
100         String actual = realMatrixFormat.format(m);
101         Assert.assertEquals(expected, actual);
102     }
103 
104     @Test
105     public void testNegativeComponent2() {
106         RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.23, -1.434343434343, 1.63},
107                                                                     {2.46, 2.46, 2.66}});
108         String expected =
109                 "{{1"    + getDecimalCharacter() +
110                 "23,-1" + getDecimalCharacter() +
111                 "4343434343,1" + getDecimalCharacter() +
112                 "63},{2" + getDecimalCharacter() +
113                 "46,2" + getDecimalCharacter() +
114                 "46,2" + getDecimalCharacter() +
115                 "66}}";
116         String actual = realMatrixFormat.format(m);
117         Assert.assertEquals(expected, actual);
118     }
119 
120     @Test
121     public void testNegativeSecondRow() {
122         RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.23, 1.43, 1.63},
123                                                                     {-2.66666666666, 2.46, 2.66}});
124         String expected =
125                 "{{1"    + getDecimalCharacter() +
126                 "23,1" + getDecimalCharacter() +
127                 "43,1" + getDecimalCharacter() +
128                 "63},{-2" + getDecimalCharacter() +
129                 "6666666667,2" + getDecimalCharacter() +
130                 "46,2" + getDecimalCharacter() +
131                 "66}}";
132         String actual = realMatrixFormat.format(m);
133         Assert.assertEquals(expected, actual);
134     }
135 
136     @Test
137     public void testNonDefaultSetting() {
138         RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1, 1, 1}, {1, 1, 1}});
139         String expected = "[1, 1, 1; 1, 1, 1]";
140         String actual = realMatrixFormatOctave.format(m);
141         Assert.assertEquals(expected, actual);
142     }
143 
144     @Test
145     public void testDefaultFormat() {
146         Locale defaultLocale = Locale.getDefault();
147         Locale.setDefault(getLocale());
148 
149         RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{232.2222222222, -342.33333333333, 432.44444444444}});
150         String expected =
151             "{{232"    + getDecimalCharacter() +
152             "2222222222,-342" + getDecimalCharacter() +
153             "3333333333,432" + getDecimalCharacter() +
154             "4444444444}}";
155         String actual = (new RealMatrixFormat()).format(m);
156         Assert.assertEquals(expected, actual);
157 
158         Locale.setDefault(defaultLocale);
159     }
160 
161     @Test
162     public void testNan() {
163         RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{Double.NaN, Double.NaN, Double.NaN}});
164         String expected = "{{(NaN),(NaN),(NaN)}}";
165         String actual = realMatrixFormat.format(m);
166         Assert.assertEquals(expected, actual);
167     }
168 
169     @Test
170     public void testPositiveInfinity() {
171         RealMatrix m = MatrixUtils.createRealMatrix(
172                 new double[][] {{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}});
173         String expected = "{{(Infinity),(Infinity),(Infinity)}}";
174         String actual = realMatrixFormat.format(m);
175         Assert.assertEquals(expected, actual);
176     }
177 
178     @Test
179     public void tesNegativeInfinity() {
180         RealMatrix m = MatrixUtils.createRealMatrix(
181                 new double[][] {{Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY}});
182         String expected = "{{(-Infinity),(-Infinity),(-Infinity)}}";
183         String actual = realMatrixFormat.format(m);
184         Assert.assertEquals(expected, actual);
185     }
186 
187     @Test
188     public void testParseSimpleNoDecimals() {
189         String source = "{{1, 1, 1}, {1, 1, 1}}";
190         RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1, 1, 1}, {1, 1, 1}});
191         RealMatrix actual = realMatrixFormat.parse(source);
192         Assert.assertEquals(expected, actual);
193     }
194 
195     @Test
196     public void testParseSimpleWithClosingRowSeparator() {
197         String source = "{{1, 1, 1},{1, 1, 1}, }}";
198         Assert.assertNull(realMatrixFormat.parse(source));
199     }
200 
201     @Test
202     public void testParseIgnoredWhitespace() {
203         RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1, 1, 1}, {1, 1, 1}});
204         ParsePosition pos1 = new ParsePosition(0);
205         String source1 = "{{1,1,1},{1,1,1}}";
206         Assert.assertEquals(expected, realMatrixFormat.parse(source1, pos1));
207         Assert.assertEquals(source1.length(), pos1.getIndex());
208         ParsePosition pos2 = new ParsePosition(0);
209         String source2 = " { { 1 , 1 , 1 } , { 1 , 1 , 1 } } ";
210         Assert.assertEquals(expected, realMatrixFormat.parse(source2, pos2));
211         Assert.assertEquals(source2.length() - 1, pos2.getIndex());
212     }
213 
214     @Test
215     public void testParseSimpleWithDecimals() {
216         String source =
217             "{{1" + getDecimalCharacter() +
218             "23,1" + getDecimalCharacter() +
219             "43,1" + getDecimalCharacter() +
220             "63}}";
221         RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1.23, 1.43, 1.63}});
222         RealMatrix actual = realMatrixFormat.parse(source);
223         Assert.assertEquals(expected, actual);
224     }
225 
226     @Test
227     public void testParseSimpleWithDecimalsTrunc() {
228         String source =
229             "{{1" + getDecimalCharacter() +
230             "2323,1" + getDecimalCharacter() +
231             "4343,1" + getDecimalCharacter() +
232             "6333}}";
233         RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1.2323, 1.4343, 1.6333}});
234         RealMatrix actual = realMatrixFormat.parse(source);
235         Assert.assertEquals(expected, actual);
236     }
237 
238     @Test
239     public void testParseNegativeComponent() {
240         String source =
241             "{{-1" + getDecimalCharacter() +
242             "2323,1" + getDecimalCharacter() +
243             "4343,1" + getDecimalCharacter() +
244             "6333}}";
245         RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{-1.2323, 1.4343, 1.6333}});
246         RealMatrix actual = realMatrixFormat.parse(source);
247         Assert.assertEquals(expected, actual);
248     }
249 
250     @Test
251     public void testParseNegativeAll() {
252         String source =
253             "{{-1" + getDecimalCharacter() +
254             "2323,-1" + getDecimalCharacter() +
255             "4343,-1" + getDecimalCharacter() +
256             "6333}}";
257         RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{-1.2323, -1.4343, -1.6333}});
258         RealMatrix actual = realMatrixFormat.parse(source);
259         Assert.assertEquals(expected, actual);
260     }
261 
262     @Test
263     public void testParseZeroComponent() {
264         String source =
265             "{{0" + getDecimalCharacter() +
266             "0,-1" + getDecimalCharacter() +
267             "4343,1" + getDecimalCharacter() +
268             "6333}}";
269         RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{0.0, -1.4343, 1.6333}});
270         RealMatrix actual = realMatrixFormat.parse(source);
271         Assert.assertEquals(expected, actual);
272     }
273 
274     @Test
275     public void testParseNonDefaultSetting() {
276         String source =
277             "[1" + getDecimalCharacter() +
278             "2323, 1" + getDecimalCharacter() +
279             "4343, 1" + getDecimalCharacter() +
280             "6333]";
281         RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1.2323, 1.4343, 1.6333}});
282         RealMatrix actual = realMatrixFormatOctave.parse(source);
283         Assert.assertEquals(expected, actual);
284     }
285 
286     @Test
287     public void testParseNan() {
288         String source = "{{(NaN), (NaN), (NaN)}}";
289         RealMatrix actual = realMatrixFormat.parse(source);
290         RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{Double.NaN, Double.NaN, Double.NaN}});
291         for (int i = 0; i < expected.getRowDimension(); i++) {
292             for (int j = 0; j < expected.getColumnDimension(); j++) {
293                 Assert.assertTrue(Double.isNaN(actual.getEntry(i, j)));
294             }
295         }
296     }
297 
298     @Test
299     public void testParsePositiveInfinity() {
300         String source = "{{(Infinity), (Infinity), (Infinity)}}";
301         RealMatrix actual = realMatrixFormat.parse(source);
302         RealMatrix expected = MatrixUtils.createRealMatrix(
303                 new double[][] {{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}});
304         Assert.assertEquals(expected, actual);
305     }
306 
307     @Test
308     public void testParseNegativeInfinity() {
309         String source = "{{(-Infinity), (-Infinity), (-Infinity)}}";
310         RealMatrix actual = realMatrixFormat.parse(source);
311         RealMatrix expected = MatrixUtils.createRealMatrix(
312                 new double[][] {{Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY}});
313         Assert.assertEquals(expected, actual);
314     }
315 
316     @Test
317     public void testParseNoComponents() {
318         try {
319             realMatrixFormat.parse("{{ }}");
320             Assert.fail("Expecting MathIllegalStateException");
321         } catch (MathIllegalStateException pe) {
322             // expected behavior
323         }
324     }
325 
326     @Test
327     public void testParseManyComponents() {
328         RealMatrix parsed = realMatrixFormat.parse("{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}");
329         Assert.assertEquals(24, parsed.getColumnDimension());
330     }
331 
332     @Test
333     public void testConstructorSingleFormat() {
334         NumberFormat nf = NumberFormat.getInstance();
335         RealMatrixFormat mf = new RealMatrixFormat(nf);
336         Assert.assertNotNull(mf);
337         Assert.assertEquals(nf, mf.getFormat());
338     }
339 
340     @Test
341     public void testForgottenPrefix() {
342         ParsePosition pos = new ParsePosition(0);
343         final String source = "1; 1; 1]";
344         Assert.assertNull("Should not parse <"+source+">", realMatrixFormat.parse(source, pos));
345         Assert.assertEquals(0, pos.getErrorIndex());
346     }
347 
348     @Test
349     public void testForgottenSeparator() {
350         ParsePosition pos = new ParsePosition(0);
351         final String source = "{{1, 1 1}}";
352         Assert.assertNull("Should not parse <"+source+">", realMatrixFormat.parse(source, pos));
353         Assert.assertEquals(7, pos.getErrorIndex());
354     }
355 
356     @Test
357     public void testForgottenSuffix() {
358         ParsePosition pos = new ParsePosition(0);
359         final String source = "{{1, 1, 1 ";
360         Assert.assertNull("Should not parse <"+source+">", realMatrixFormat.parse(source, pos));
361         Assert.assertEquals(9, pos.getErrorIndex());
362     }
363 }