View Javadoc
1   /*
2    * Copyright 2009 Ange Optimization ApS
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package eu.simuline.octave.io.impl;
17  
18  import java.io.BufferedReader;
19  
20  import eu.simuline.octave.exception.OctaveParseException;
21  import eu.simuline.octave.io.OctaveIO;
22  import eu.simuline.octave.io.spi.OctaveDataReader;
23  import eu.simuline.octave.type.OctaveComplex;
24  import eu.simuline.octave.util.StringUtil;
25  
26  /**
27   * Reader for the octave type "complex matrix" 
28   * reading an {@link OctaveComplex} from a {@link BufferedReader}. 
29   * Note that the entries are of type "complex scalar". 
30   *
31   * Format is:
32   *
33   * <pre>
34   * # type: complex matrix
35   * # rows: 2
36   * # columns: 2
37   *  (1.1,0) (0,1.1)
38   *  (2.2,0) (0,2.2)
39   * </pre>
40   */
41  // **** seems as if this is not general enough. 
42  // **** should be subclass of AbstractGenericReader? 
43  public final class ComplexMatrixReader extends OctaveDataReader {
44  
45  
46      private static final String COLUMNS = "# columns: ";
47      private static final String ROWS    = "# rows: ";
48  
49      @Override
50      public String octaveType() {
51          return "complex matrix";
52      }
53  
54      @Override
55      public OctaveComplex read(final BufferedReader reader) {
56          final int rows = parseRows(reader);
57          final int columns = parseColumns(reader);
58          final OctaveComplex complex = new OctaveComplex(rows, columns);
59          for (int r = 1; r <= rows; ++r) {
60              final String line = OctaveIO.readerReadLine(reader);
61              final String[] split = line.split(" ");
62              if (split.length != columns + 1) {
63                  throw new OctaveParseException
64  		    ("Error in complex matrix-format: '" + line + "'");
65              }
66              for (int c = 1; c < split.length; c++) {
67                  final int commaIndex = split[c].indexOf(',');
68                  final double real = StringUtil
69  		    .parseDouble(split[c].substring(1, commaIndex));
70                  final double imag = StringUtil
71  		    .parseDouble(split[c].substring(commaIndex + 1,
72  						    split[c].length() - 1));
73                  complex.setReal(real, r, c);
74                  complex.setImag(imag, r, c);
75              }
76          }
77          return complex;
78      }
79  
80      /**
81       * @param reader
82       * @return
83       *   the number of rows
84       */
85      public int parseRows(final BufferedReader reader) {
86          final String line = OctaveIO.readerReadLine(reader);
87          if (!line.startsWith(ROWS)) {
88              throw new OctaveParseException
89  		("Expected <" + ROWS + "> got <" + line + ">");
90          }
91          return Integer.parseInt(line.substring(ROWS.length()));
92      }
93  
94      /**
95       * @param reader
96       * @return
97       *    the number of columns
98       */
99      public int parseColumns(final BufferedReader reader) {
100         final String line = OctaveIO.readerReadLine(reader);
101         if (!line.startsWith(COLUMNS)) {
102             throw new OctaveParseException
103 		("Expected <" + COLUMNS + "> got <" + line + ">");
104         }
105         return Integer.parseInt(line.substring(COLUMNS.length()));
106     }
107 
108 }