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