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.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   * Formats a {@code nxm} matrix in components list format
32   * "{{a<sub>0</sub><sub>0</sub>,a<sub>0</sub><sub>1</sub>, ...,
33   * a<sub>0</sub><sub>m-1</sub>},{a<sub>1</sub><sub>0</sub>,
34   * a<sub>1</sub><sub>1</sub>, ..., a<sub>1</sub><sub>m-1</sub>},{...},{
35   * a<sub>n-1</sub><sub>0</sub>, a<sub>n-1</sub><sub>1</sub>, ...,
36   * a<sub>n-1</sub><sub>m-1</sub>}}".
37   * <p>The prefix and suffix "{" and "}", the row prefix and suffix "{" and "}",
38   * the row separator "," and the column separator "," can be replaced by any
39   * user-defined strings. The number format for components can be configured.</p>
40   *
41   * <p>White space is ignored at parse time, even if it is in the prefix, suffix
42   * or separator specifications. So even if the default separator does include a space
43   * character that is used at format time, both input string "{{1,1,1}}" and
44   * " { { 1 , 1 , 1 } } " will be parsed without error and the same matrix will be
45   * returned. In the second case, however, the parse position after parsing will be
46   * just after the closing curly brace, i.e. just before the trailing space.</p>
47   *
48   * <p><b>Note:</b> the grouping functionality of the used {@link NumberFormat} is
49   * disabled to prevent problems when parsing (e.g. 1,345.34 would be a valid number
50   * but conflicts with the default column separator).</p>
51   *
52   * @since 3.1
53   */
54  public class RealMatrixFormat {
55  
56      /** The default prefix: "{". */
57      private static final String DEFAULT_PREFIX = "{";
58      /** The default suffix: "}". */
59      private static final String DEFAULT_SUFFIX = "}";
60      /** The default row prefix: "{". */
61      private static final String DEFAULT_ROW_PREFIX = "{";
62      /** The default row suffix: "}". */
63      private static final String DEFAULT_ROW_SUFFIX = "}";
64      /** The default row separator: ",". */
65      private static final String DEFAULT_ROW_SEPARATOR = ",";
66      /** The default column separator: ",". */
67      private static final String DEFAULT_COLUMN_SEPARATOR = ",";
68      /** Prefix. */
69      private final String prefix;
70      /** Suffix. */
71      private final String suffix;
72      /** Row prefix. */
73      private final String rowPrefix;
74      /** Row suffix. */
75      private final String rowSuffix;
76      /** Row separator. */
77      private final String rowSeparator;
78      /** Column separator. */
79      private final String columnSeparator;
80      /** The format used for components. */
81      private final NumberFormat format;
82  
83      /**
84       * Create an instance with default settings.
85       * <p>The instance uses the default prefix, suffix and row/column separator:
86       * "[", "]", ";" and ", " and the default number format for components.</p>
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       * Create an instance with a custom number format for components.
95       * @param format the custom format for components.
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      * Create an instance with custom prefix, suffix and separator.
104      * @param prefix prefix to use instead of the default "{"
105      * @param suffix suffix to use instead of the default "}"
106      * @param rowPrefix row prefix to use instead of the default "{"
107      * @param rowSuffix row suffix to use instead of the default "}"
108      * @param rowSeparator tow separator to use instead of the default ";"
109      * @param columnSeparator column separator to use instead of the default ", "
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      * Create an instance with custom prefix, suffix, separator and format
120      * for components.
121      * @param prefix prefix to use instead of the default "{"
122      * @param suffix suffix to use instead of the default "}"
123      * @param rowPrefix row prefix to use instead of the default "{"
124      * @param rowSuffix row suffix to use instead of the default "}"
125      * @param rowSeparator tow separator to use instead of the default ";"
126      * @param columnSeparator column separator to use instead of the default ", "
127      * @param format the custom format for components.
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         // disable grouping to prevent parsing problems
141         this.format.setGroupingUsed(false);
142     }
143 
144     /**
145      * Get the set of locales for which real vectors formats are available.
146      * <p>This is the same set as the {@link NumberFormat} set.</p>
147      * @return available real vector format locales.
148      */
149     public static Locale[] getAvailableLocales() {
150         return NumberFormat.getAvailableLocales();
151     }
152 
153     /**
154      * Get the format prefix.
155      * @return format prefix.
156      */
157     public String getPrefix() {
158         return prefix;
159     }
160 
161     /**
162      * Get the format suffix.
163      * @return format suffix.
164      */
165     public String getSuffix() {
166         return suffix;
167     }
168 
169     /**
170      * Get the format prefix.
171      * @return format prefix.
172      */
173     public String getRowPrefix() {
174         return rowPrefix;
175     }
176 
177     /**
178      * Get the format suffix.
179      * @return format suffix.
180      */
181     public String getRowSuffix() {
182         return rowSuffix;
183     }
184 
185     /**
186      * Get the format separator between rows of the matrix.
187      * @return format separator for rows.
188      */
189     public String getRowSeparator() {
190         return rowSeparator;
191     }
192 
193     /**
194      * Get the format separator between components.
195      * @return format separator between components.
196      */
197     public String getColumnSeparator() {
198         return columnSeparator;
199     }
200 
201     /**
202      * Get the components format.
203      * @return components format.
204      */
205     public NumberFormat getFormat() {
206         return format;
207     }
208 
209     /**
210      * Returns the default real vector format for the current locale.
211      * @return the default real vector format.
212      */
213     public static RealMatrixFormat getInstance() {
214         return getInstance(Locale.getDefault());
215     }
216 
217     /**
218      * Returns the default real vector format for the given locale.
219      * @param locale the specific locale used by the format.
220      * @return the real vector format specific to the given locale.
221      */
222     public static RealMatrixFormat getInstance(final Locale locale) {
223         return new RealMatrixFormat(CompositeFormat.getDefaultNumberFormat(locale));
224     }
225 
226     /**
227      * This method calls {@link #format(RealMatrix,StringBuffer,FieldPosition)}.
228      *
229      * @param m RealMatrix object to format.
230      * @return a formatted matrix.
231      */
232     public String format(RealMatrix m) {
233         return format(m, new StringBuffer(), new FieldPosition(0)).toString();
234     }
235 
236     /**
237      * Formats a {@link RealMatrix} object to produce a string.
238      * @param matrix the object to format.
239      * @param toAppendTo where the text is to be appended
240      * @param pos On input: an alignment field, if desired. On output: the
241      *            offsets of the alignment field
242      * @return the value passed in as toAppendTo.
243      */
244     public StringBuffer format(RealMatrix matrix, StringBuffer toAppendTo,
245                                FieldPosition pos) {
246 
247         pos.setBeginIndex(0);
248         pos.setEndIndex(0);
249 
250         // format prefix
251         toAppendTo.append(prefix);
252 
253         // format rows
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         // format suffix
270         toAppendTo.append(suffix);
271 
272         return toAppendTo;
273     }
274 
275     /**
276      * Parse a string to produce a {@link RealMatrix} object.
277      *
278      * @param source String to parse.
279      * @return the parsed {@link RealMatrix} object.
280      * @throws MathParseException if the beginning of the specified string
281      * cannot be parsed.
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      * Parse a string to produce a {@link RealMatrix} object.
296      *
297      * @param source String to parse.
298      * @param pos input/output parsing parameter.
299      * @return the parsed {@link RealMatrix} object.
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         // parse prefix
312         CompositeFormat.parseAndIgnoreWhitespace(source, pos);
313         if (!CompositeFormat.parseFixedstring(source, trimmedPrefix, pos)) {
314             return null;
315         }
316 
317         // parse components
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                         // invalid component
355                         // set index back to initial, error index should already be set
356                         pos.setIndex(initialIndex);
357                         return null;
358                     }
359                 }
360             }
361         }
362 
363         if (!rowComponents.isEmpty()) {
364             matrix.add(rowComponents);
365         }
366 
367         // parse suffix
368         CompositeFormat.parseAndIgnoreWhitespace(source, pos);
369         if (!CompositeFormat.parseFixedstring(source, trimmedSuffix, pos)) {
370             return null;
371         }
372 
373         // do not allow an empty matrix
374         if (matrix.isEmpty()) {
375             pos.setIndex(initialIndex);
376             return null;
377         }
378 
379         // build vector
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 }