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