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