5 import java.io.DataInput;
6 import java.io.DataInputStream;
7 import java.io.FileInputStream;
8 import java.io.FileNotFoundException;
9 import java.io.IOException;
10 import java.io.InputStream;
12 import org.apache.commons.math.linear.MatrixIndexException;
13 import org.apache.commons.math.linear.RealMatrix;
14 import org.apache.commons.math.linear.RealVector;
50 private static final long serialVersionUID = -3505742802789851382L;
74 private DataInput getDataInput(InputStream in) {
77 return new DataInputStream(in);
94 public RealMatrix
readMatrix(InputStream in)
throws IOException {
95 return MathFactory.createRealMatrix(readRawDoubleMatrix(in));
108 public RealMatrix
readMatrix(String file)
throws IOException, FileNotFoundException {
112 private double[][] readRawDoubleMatrix(DataInput rd,
int rows,
int cols)
throws MatrixIndexException, IOException {
113 double[][] res =
new double[rows][];
114 for (
int i = 0; i < rows; i++) {
115 res[i] = readRawDoubleVector(rd, cols);
132 double[][] res = null;
134 DataInput di = getDataInput(in);
137 res = readRawDoubleMatrix(di, rows, cols);
157 return readRawDoubleMatrix(getDataInput(in), rows, cols);
160 private double[] readRawDoubleVector(DataInput rd,
int size)
throws MatrixIndexException, IOException {
161 double[] res =
new double[size];
162 for (
int i = 0; i < size; i++) {
163 res[i] = rd.readDouble();
180 DataInput di = getDataInput(in);
181 res = readRawDoubleVector(di, di.readInt());
188 private float[] readRawFloatVector(DataInput di,
int size)
throws IOException {
189 float[] res =
new float[size];
190 for (
int i = 0; i < size; i++) {
191 res[i] = di.readFloat();
199 DataInput di = getDataInput(in);
200 res = readRawFloatVector(in, di.readInt());
219 return readRawFloatVector(getDataInput(in), size);
232 public RealVector
readVector(InputStream in)
throws IOException {
233 return MathFactory.createRealVector(readRawDoubleVector(in));
249 DataInput di = getDataInput(in);
258 short[] res =
new short[size];
259 for (
int i = 0; i < size; i++) {
260 res[i] = di.readShort();
274 public RealVector
readVector(String filename)
throws FileNotFoundException, IOException {
275 return readVector(
new FileInputStream(filename));
Factory method to create new RealMatrix instances.
float[] readRawFloatVector(InputStream in, int size)
This method exists due to compatibility with the old rbAppMIT models.
short[] readRawShortVector(InputStream in)
Reads a short array/vector from the given input stream, autodetecting its size from the first bytes r...
RealVector readVector(String filename)
Reads a real vector including dimension from a given binary file.
double[][] readRawDoubleMatrix(InputStream in, int rows, int cols)
Reads a double matrix of size rows*cols from the input stream.
float[] readRawFloatVector(InputStream in)
Reading matrices and vectors with a bunch of convenient overloads for different sources and output fo...
double[] readRawDoubleVector(InputStream in)
Reads a real vector from a binary input stream, including dimension detection.
RealMatrix readMatrix(InputStream in)
Reads a matrix from an InputStream, pointing to a binary file.
MathReaderException(String msg, Exception inner)
RealVector readVector(InputStream in)
Reads a real vector including dimension from a given input stream which points to a binary file...
double[][] readRawDoubleMatrix(InputStream in)
Reads a real matrix as double[][] array from a given binary input stream, including dimension detecti...
RealMatrix readMatrix(String file)
Reads a matrix from a given binary file in the file system (accessible via java.io, i.e.
MachineFormats MachineFormat
Determines which machine format to use.