Input and Output

From Efficient Java Matrix Library
Revision as of 18:36, 17 February 2021 by Peter (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.


DMatrixRMaj 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");
        DMatrixRMaj 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);
    }
}

Matlab

Want to read and write EJML matrices in matlab format? HEBI Robotic's got you covered with their library https://github.com/HebiRobotics/MFL

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

Deprecated

The binary format which used Java serialization has been deprecated and will be removed in the not to distant future. Basically it's now considered a significant security risk.

https://medium.com/swlh/hacking-java-deserialization-7625c8450334

In the future a new binary format might be provided (you can request this on Github) but for now you can use the Matlab format discussed above.