1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
82
83
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
96
97
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 }