1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math4.legacy.linear;
19
20 import java.text.FieldPosition;
21 import java.text.NumberFormat;
22 import java.text.ParsePosition;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Locale;
26
27 import org.apache.commons.math4.legacy.exception.MathParseException;
28 import org.apache.commons.math4.legacy.util.CompositeFormat;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class RealMatrixFormat {
55
56
57 private static final String DEFAULT_PREFIX = "{";
58
59 private static final String DEFAULT_SUFFIX = "}";
60
61 private static final String DEFAULT_ROW_PREFIX = "{";
62
63 private static final String DEFAULT_ROW_SUFFIX = "}";
64
65 private static final String DEFAULT_ROW_SEPARATOR = ",";
66
67 private static final String DEFAULT_COLUMN_SEPARATOR = ",";
68
69 private final String prefix;
70
71 private final String suffix;
72
73 private final String rowPrefix;
74
75 private final String rowSuffix;
76
77 private final String rowSeparator;
78
79 private final String columnSeparator;
80
81 private final NumberFormat format;
82
83
84
85
86
87
88 public RealMatrixFormat() {
89 this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ROW_PREFIX, DEFAULT_ROW_SUFFIX,
90 DEFAULT_ROW_SEPARATOR, DEFAULT_COLUMN_SEPARATOR, CompositeFormat.getDefaultNumberFormat());
91 }
92
93
94
95
96
97 public RealMatrixFormat(final NumberFormat format) {
98 this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ROW_PREFIX, DEFAULT_ROW_SUFFIX,
99 DEFAULT_ROW_SEPARATOR, DEFAULT_COLUMN_SEPARATOR, format);
100 }
101
102
103
104
105
106
107
108
109
110
111 public RealMatrixFormat(final String prefix, final String suffix,
112 final String rowPrefix, final String rowSuffix,
113 final String rowSeparator, final String columnSeparator) {
114 this(prefix, suffix, rowPrefix, rowSuffix, rowSeparator, columnSeparator,
115 CompositeFormat.getDefaultNumberFormat());
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129 public RealMatrixFormat(final String prefix, final String suffix,
130 final String rowPrefix, final String rowSuffix,
131 final String rowSeparator, final String columnSeparator,
132 final NumberFormat format) {
133 this.prefix = prefix;
134 this.suffix = suffix;
135 this.rowPrefix = rowPrefix;
136 this.rowSuffix = rowSuffix;
137 this.rowSeparator = rowSeparator;
138 this.columnSeparator = columnSeparator;
139 this.format = format;
140
141 this.format.setGroupingUsed(false);
142 }
143
144
145
146
147
148
149 public static Locale[] getAvailableLocales() {
150 return NumberFormat.getAvailableLocales();
151 }
152
153
154
155
156
157 public String getPrefix() {
158 return prefix;
159 }
160
161
162
163
164
165 public String getSuffix() {
166 return suffix;
167 }
168
169
170
171
172
173 public String getRowPrefix() {
174 return rowPrefix;
175 }
176
177
178
179
180
181 public String getRowSuffix() {
182 return rowSuffix;
183 }
184
185
186
187
188
189 public String getRowSeparator() {
190 return rowSeparator;
191 }
192
193
194
195
196
197 public String getColumnSeparator() {
198 return columnSeparator;
199 }
200
201
202
203
204
205 public NumberFormat getFormat() {
206 return format;
207 }
208
209
210
211
212
213 public static RealMatrixFormat getInstance() {
214 return getInstance(Locale.getDefault());
215 }
216
217
218
219
220
221
222 public static RealMatrixFormat getInstance(final Locale locale) {
223 return new RealMatrixFormat(CompositeFormat.getDefaultNumberFormat(locale));
224 }
225
226
227
228
229
230
231
232 public String format(RealMatrix m) {
233 return format(m, new StringBuffer(), new FieldPosition(0)).toString();
234 }
235
236
237
238
239
240
241
242
243
244 public StringBuffer format(RealMatrix matrix, StringBuffer toAppendTo,
245 FieldPosition pos) {
246
247 pos.setBeginIndex(0);
248 pos.setEndIndex(0);
249
250
251 toAppendTo.append(prefix);
252
253
254 final int rows = matrix.getRowDimension();
255 for (int i = 0; i < rows; ++i) {
256 toAppendTo.append(rowPrefix);
257 for (int j = 0; j < matrix.getColumnDimension(); ++j) {
258 if (j > 0) {
259 toAppendTo.append(columnSeparator);
260 }
261 CompositeFormat.formatDouble(matrix.getEntry(i, j), format, toAppendTo, pos);
262 }
263 toAppendTo.append(rowSuffix);
264 if (i < rows - 1) {
265 toAppendTo.append(rowSeparator);
266 }
267 }
268
269
270 toAppendTo.append(suffix);
271
272 return toAppendTo;
273 }
274
275
276
277
278
279
280
281
282
283 public RealMatrix parse(String source) {
284 final ParsePosition parsePosition = new ParsePosition(0);
285 final RealMatrix result = parse(source, parsePosition);
286 if (parsePosition.getIndex() == 0) {
287 throw new MathParseException(source,
288 parsePosition.getErrorIndex(),
289 Array2DRowRealMatrix.class);
290 }
291 return result;
292 }
293
294
295
296
297
298
299
300
301 public RealMatrix parse(String source, ParsePosition pos) {
302 int initialIndex = pos.getIndex();
303
304 final String trimmedPrefix = prefix.trim();
305 final String trimmedSuffix = suffix.trim();
306 final String trimmedRowPrefix = rowPrefix.trim();
307 final String trimmedRowSuffix = rowSuffix.trim();
308 final String trimmedColumnSeparator = columnSeparator.trim();
309 final String trimmedRowSeparator = rowSeparator.trim();
310
311
312 CompositeFormat.parseAndIgnoreWhitespace(source, pos);
313 if (!CompositeFormat.parseFixedstring(source, trimmedPrefix, pos)) {
314 return null;
315 }
316
317
318 List<List<Number>> matrix = new ArrayList<>();
319 List<Number> rowComponents = new ArrayList<>();
320 for (boolean loop = true; loop;){
321
322 if (!rowComponents.isEmpty()) {
323 CompositeFormat.parseAndIgnoreWhitespace(source, pos);
324 if (!CompositeFormat.parseFixedstring(source, trimmedColumnSeparator, pos)) {
325 if (!trimmedRowSuffix.isEmpty() &&
326 !CompositeFormat.parseFixedstring(source, trimmedRowSuffix, pos)) {
327 return null;
328 }
329 CompositeFormat.parseAndIgnoreWhitespace(source, pos);
330 if (CompositeFormat.parseFixedstring(source, trimmedRowSeparator, pos)) {
331 matrix.add(rowComponents);
332 rowComponents = new ArrayList<>();
333 continue;
334 }
335 loop = false;
336 }
337 } else {
338 CompositeFormat.parseAndIgnoreWhitespace(source, pos);
339 if (!trimmedRowPrefix.isEmpty() &&
340 !CompositeFormat.parseFixedstring(source, trimmedRowPrefix, pos)) {
341 return null;
342 }
343 }
344
345 if (loop) {
346 CompositeFormat.parseAndIgnoreWhitespace(source, pos);
347 Number component = CompositeFormat.parseNumber(source, format, pos);
348 if (component != null) {
349 rowComponents.add(component);
350 } else {
351 if (rowComponents.isEmpty()) {
352 loop = false;
353 } else {
354
355
356 pos.setIndex(initialIndex);
357 return null;
358 }
359 }
360 }
361 }
362
363 if (!rowComponents.isEmpty()) {
364 matrix.add(rowComponents);
365 }
366
367
368 CompositeFormat.parseAndIgnoreWhitespace(source, pos);
369 if (!CompositeFormat.parseFixedstring(source, trimmedSuffix, pos)) {
370 return null;
371 }
372
373
374 if (matrix.isEmpty()) {
375 pos.setIndex(initialIndex);
376 return null;
377 }
378
379
380 double[][] data = new double[matrix.size()][];
381 int row = 0;
382 for (List<Number> rowList : matrix) {
383 data[row] = new double[rowList.size()];
384 for (int i = 0; i < rowList.size(); i++) {
385 data[row][i] = rowList.get(i).doubleValue();
386 }
387 row++;
388 }
389 return MatrixUtils.createRealMatrix(data);
390 }
391 }