Input and Output

From Efficient Java Matrix Library
Revision as of 09:07, 18 May 2017 by Peter (talk | contribs)
Jump to navigation Jump to search

EJML provides several different methods for loading, saving, and displaying a matrix. A matrix can be saved and loaded from a file, displayed visually in a window, printed to the console, created from raw arrays or strings.


Text Output

A matrix can be printed to standard out using its built in print() command, this works for both DMatrixRMaj and SimpleMatrix. To create a custom output the user can provide a formatting string that is compatible with printf().

Code:

public static void main( String []args ) {
    DMatrixRMaj A = new DMatrixRMaj(2,3,true,1.1,2.34,3.35436,4345,59505,0.00001234);

    A.print();
    System.out.println();
    A.print("%e");
    System.out.println();
    A.print("%10.2f");
}

Output:

Type = dense real , numRows = 2 , numCols = 3
 1.100   2.340   3.354  
4345.000  59505.000   0.000  

Type = dense real , numRows = 2 , numCols = 3
1.100000e+00 2.340000e+00 3.354360e+00 
4.345000e+03 5.950500e+04 1.234000e-05 

Type = dense real , numRows = 2 , numCols = 3
      1.10       2.34       3.35 
   4345.00   59505.00       0.00 

CSV Input/Outut

A Column Space Value (CSV) reader and writer is provided by EJML. The advantage of this file format is that it's human readable, the disadvantage is that its large and slow. Two CSV formats are supported, one where the first line specifies the matrix dimension and the other the user specifies it pro grammatically.

In the example below, the matrix size and type is specified in the first line; row, column, and real/complex. The remainder of the file contains the value of each element in the matrix in a row-major format. A file containing

2 3 real
2.4 6.7 9
-2 3 5

would describe a real matrix with 2 rows and 3 columns.


DenseMatrix64F Example:

public static void main( String args[] ) {
    DMatrixRMaj A = new DMatrixRMaj(2,3,true,new double[]{1,2,3,4,5,6});

    try {
        MatrixIO.saveCSV(A, "matrix_file.csv");
        DenseMatrix64F B = MatrixIO.loadCSV("matrix_file.csv");
        B.print();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

SimpleMatrix Example:

public static void main( String args[] ) {
    SimpleMatrix A = new SimpleMatrix(2,3,true,new double[]{1,2,3,4,5,6});

    try {
        A.saveToFileCSV("matrix_file.csv");
        SimpleMatrix B = SimpleMatrix.loadCSV("matrix_file.csv");
        B.print();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

Serialized Binary Input/Output

DMatrixRMaj is a serializable object and is fully compatible with any Java serialization routine. MatrixIO provides save() and load() functions for saving to and reading from a file. The matrix is saved as a Java binary serialized object. SimpleMatrix provides its own function (that are wrappers around MatrixIO) for saving and loading from files.

MatrixIO Example:

public static void main( String args[] ) {
    DMatrixRMaj A = new DMatrixRMaj(2,3,true,new double[]{1,2,3,4,5,6});

    try {
        MatrixIO.saveBin(A,"matrix_file.data");
        DMatrixRMaj B = MatrixIO.loadBin("matrix_file.data");
        B.print();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
  • NOTE* in v0.18 saveBin/loadBin is actually saveXML/loadXML, which is a mistake since its not in an xml format.

SimpleMatrix Example:

public static void main( String args[] ) {
    SimpleMatrix A = new SimpleMatrix(2,3,true,new double[]{1,2,3,4,5,6});

    try {
        A.saveToFileBinary("matrix_file.data");
        SimpleMatrix B = SimpleMatrix.loadBinary("matrix_file.data");
        B.print();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

Visual Display

Understanding the state of a matrix from text output can be difficult, especially for large matrices. To help in these situations a visual way of viewing a matrix is provided in DMatrixVisualization. By calling MatrixIO.show() a window will be created that shows the matrix. Positive elements will appear as a shade of red, negative ones as a shade of blue, and zeros as black. How red or blue an element is depends on its magnitude.

Example Code:

public static void main( String args[] ) {
    DMatrixRMaj A = new DMatrixRMaj(4,4,true,
            0,2,3,4,-2,0,2,3,-3,-2,0,2,-4,-3,-2,0);

    MatrixIO.show(A,"Small Matrix");

    DMatrixRMaj B = new DMatrixRMaj(25,50);
    for( int i = 0; i < 25; i++ )
        B.set(i,i,i+1);

    MatrixIO.show(B,"Larger Diagonal Matrix");
}

Output:

small_matrix.gif larger_matrix.gif