Difference between revisions of "Input and Output"

From Efficient Java Matrix Library
Jump to navigation Jump to search
(Created page with "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...")
 
 
(2 intermediate revisions by the same user not shown)
Line 6: Line 6:
 
= Text Output =
 
= Text Output =
  
A matrix can be printed to standard out using its built in ''print()'' command, this works for both DenseMatrix64F and SimpleMatrix.  To create a custom output the user can provide a formatting string that is compatible with printf().
+
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:
 
Code:
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
public static void main( String []args ) {
 
public static void main( String []args ) {
     DenseMatrix64F A = new DenseMatrix64F(2,3,true,1.1,2.34,3.35436,4345,59505,0.00001234);
+
     DMatrixRMaj A = new DMatrixRMaj(2,3,true,1.1,2.34,3.35436,4345,59505,0.00001234);
  
 
     A.print();
 
     A.print();
Line 49: Line 49:
  
  
DenseMatrix64F Example:
+
DMatrixRMaj Example:
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
public static void main( String args[] ) {
 
public static void main( String args[] ) {
     DenseMatrix64F A = new DenseMatrix64F(2,3,true,new double[]{1,2,3,4,5,6});
+
     DMatrixRMaj A = new DMatrixRMaj(2,3,true,new double[]{1,2,3,4,5,6});
  
 
     try {
 
     try {
 
         MatrixIO.saveCSV(A, "matrix_file.csv");
 
         MatrixIO.saveCSV(A, "matrix_file.csv");
         DenseMatrix64F B = MatrixIO.loadCSV("matrix_file.csv");
+
         DMatrixRMaj B = MatrixIO.loadCSV("matrix_file.csv");
 
         B.print();
 
         B.print();
 
     } catch (IOException e) {
 
     } catch (IOException e) {
Line 79: Line 79:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
= Serialized Binary Input/Output =
+
= Matlab =
  
DenseMatrix64F 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.
+
Want to read and write EJML matrices in matlab format? HEBI Robotic's got you covered with their library https://github.com/HebiRobotics/MFL
 
 
MatrixIO Example:
 
<syntaxhighlight lang="java">
 
public static void main( String args[] ) {
 
    DenseMatrix64F A = new DenseMatrix64F(2,3,true,new double[]{1,2,3,4,5,6});
 
 
 
    try {
 
        MatrixIO.saveBin(A,"matrix_file.data");
 
        DenseMatrix64F B = MatrixIO.loadBin("matrix_file.data");
 
        B.print();
 
    } catch (IOException e) {
 
        throw new RuntimeException(e);
 
    }
 
}
 
</syntaxhighlight>
 
 
 
*NOTE* in v0.18 saveBin/loadBin is actually saveXML/loadXML, which is a mistake since its not in an xml format.
 
 
 
SimpleMatrix Example:
 
<syntaxhighlight lang="java">
 
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);
 
    }
 
}
 
</syntaxhighlight>
 
  
 
= Visual Display =
 
= 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 MatrixVisualization.  By calling MatrixVisualization.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.
+
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:
 
Example Code:
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
public static void main( String args[] ) {
 
public static void main( String args[] ) {
     DenseMatrix64F A = new DenseMatrix64F(4,4,true,
+
     DMatrixRMaj A = new DMatrixRMaj(4,4,true,
 
             0,2,3,4,-2,0,2,3,-3,-2,0,2,-4,-3,-2,0);
 
             0,2,3,4,-2,0,2,3,-3,-2,0,2,-4,-3,-2,0);
  
 
     MatrixIO.show(A,"Small Matrix");
 
     MatrixIO.show(A,"Small Matrix");
  
     DenseMatrix64F B = new DenseMatrix64F(25,50);
+
     DMatrixRMaj B = new DMatrixRMaj(25,50);
 
     for( int i = 0; i < 25; i++ )
 
     for( int i = 0; i < 25; i++ )
 
         B.set(i,i,i+1);
 
         B.set(i,i,i+1);
Line 139: Line 107:
 
| http://ejml.org/wiki/MY_IMAGES/small_matrix.gif || http://ejml.org/wiki/MY_IMAGES/larger_matrix.gif
 
| http://ejml.org/wiki/MY_IMAGES/small_matrix.gif || http://ejml.org/wiki/MY_IMAGES/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.

Latest revision as of 19:36, 17 February 2021

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.