Difference between revisions of "Example Sparse Matrices"

From Efficient Java Matrix Library
Jump to: navigation, search
(Sparse Matrix Example)
 
Line 11: Line 11:
 
|}
 
|}
 
</center>
 
</center>
 +
 +
External Resources:
 +
* [https://github.com/lessthanoptimal/ejml/blob/v0.41/examples/src/org/ejml/example/ExampleSparseMatrix.java ExampleSparseMatrix.java]
  
 
== Sparse Matrix Example ==
 
== Sparse Matrix Example ==
Line 21: Line 24:
 
  */
 
  */
 
public class ExampleSparseMatrix {
 
public class ExampleSparseMatrix {
 
 
 
     public static int ROWS = 100000;
 
     public static int ROWS = 100000;
 
     public static int COLS = 1000;
 
     public static int COLS = 1000;
 
     public static int XCOLS = 1;
 
     public static int XCOLS = 1;
  
     public static void main(String[] args) {
+
     public static void main( String[] args ) {
 
         Random rand = new Random(234);
 
         Random rand = new Random(234);
  
Line 34: Line 35:
 
         //      If you don't it will be forced to thrash memory as it grows its internal data structures.
 
         //      If you don't it will be forced to thrash memory as it grows its internal data structures.
 
         //      Failure to heed this advice will make construction of large matrices 4x slower and use 2x more memory
 
         //      Failure to heed this advice will make construction of large matrices 4x slower and use 2x more memory
         DMatrixSparseTriplet work = new DMatrixSparseTriplet(5,4,5);
+
         DMatrixSparseTriplet work = new DMatrixSparseTriplet(5, 4, 5);
         work.addItem(0,1,1.2);
+
         work.addItem(0, 1, 1.2);
         work.addItem(3,0,3);
+
         work.addItem(3, 0, 3);
         work.addItem(1,1,22.21234);
+
         work.addItem(1, 1, 22.21234);
         work.addItem(2,3,6);
+
         work.addItem(2, 3, 6);
  
 
         // convert into a format that's easier to perform math with
 
         // convert into a format that's easier to perform math with
         DMatrixSparseCSC Z = DConvertMatrixStruct.convert(work,(DMatrixSparseCSC)null);
+
         DMatrixSparseCSC Z = DConvertMatrixStruct.convert(work, (DMatrixSparseCSC)null);
  
 
         // print the matrix to standard out in two different formats
 
         // print the matrix to standard out in two different formats
Line 50: Line 51:
  
 
         // Create a large matrix that is 5% filled
 
         // Create a large matrix that is 5% filled
         DMatrixSparseCSC A = RandomMatrices_DSCC.rectangle(ROWS,COLS,(int)(ROWS*COLS*0.05),rand);
+
         DMatrixSparseCSC A = RandomMatrices_DSCC.rectangle(ROWS, COLS, (int)(ROWS*COLS*0.05), rand);
 
         //          large vector that is 70% filled
 
         //          large vector that is 70% filled
         DMatrixSparseCSC x = RandomMatrices_DSCC.rectangle(COLS,XCOLS,(int)(XCOLS*COLS*0.7),rand);
+
         DMatrixSparseCSC x = RandomMatrices_DSCC.rectangle(COLS, XCOLS, (int)(XCOLS*COLS*0.7), rand);
  
 
         System.out.println("Done generating random matrices");
 
         System.out.println("Done generating random matrices");
 
         // storage for the initial solution
 
         // storage for the initial solution
         DMatrixSparseCSC y = new DMatrixSparseCSC(ROWS,XCOLS,0);
+
         DMatrixSparseCSC y = new DMatrixSparseCSC(ROWS, XCOLS, 0);
         DMatrixSparseCSC z = new DMatrixSparseCSC(ROWS,XCOLS,0);
+
         DMatrixSparseCSC z = new DMatrixSparseCSC(ROWS, XCOLS, 0);
  
 
         // To demonstration how to perform sparse math let's multiply:
 
         // To demonstration how to perform sparse math let's multiply:
Line 66: Line 67:
 
         DGrowArray workB = new DGrowArray(A.numRows);
 
         DGrowArray workB = new DGrowArray(A.numRows);
 
         for (int i = 0; i < 100; i++) {
 
         for (int i = 0; i < 100; i++) {
             CommonOps_DSCC.mult(A,x,y,workA,workB);
+
             CommonOps_DSCC.mult(A, x, y, workA, workB);
             CommonOps_DSCC.add(1.5,y,0.75,y,z,workA,workB);
+
             CommonOps_DSCC.add(1.5, y, 0.75, y, z, workA, workB);
 
         }
 
         }
 
         long after = System.currentTimeMillis();
 
         long after = System.currentTimeMillis();
  
         System.out.println("norm = "+ NormOps_DSCC.fastNormF(y)+"  sparse time = "+(after-before)+" ms");
+
         System.out.println("norm = " + NormOps_DSCC.fastNormF(y) + "  sparse time = " + (after - before) + " ms");
  
         DMatrixRMaj Ad = DConvertMatrixStruct.convert(A,(DMatrixRMaj)null);
+
         DMatrixRMaj Ad = DConvertMatrixStruct.convert(A, (DMatrixRMaj)null);
         DMatrixRMaj xd = DConvertMatrixStruct.convert(x,(DMatrixRMaj)null);
+
         DMatrixRMaj xd = DConvertMatrixStruct.convert(x, (DMatrixRMaj)null);
         DMatrixRMaj yd = new DMatrixRMaj(y.numRows,y.numCols);
+
         DMatrixRMaj yd = new DMatrixRMaj(y.numRows, y.numCols);
         DMatrixRMaj zd = new DMatrixRMaj(y.numRows,y.numCols);
+
         DMatrixRMaj zd = new DMatrixRMaj(y.numRows, y.numCols);
  
 
         before = System.currentTimeMillis();
 
         before = System.currentTimeMillis();
 
         for (int i = 0; i < 100; i++) {
 
         for (int i = 0; i < 100; i++) {
 
             CommonOps_DDRM.mult(Ad, xd, yd);
 
             CommonOps_DDRM.mult(Ad, xd, yd);
             CommonOps_DDRM.add(1.5,yd,0.75, yd, zd);
+
             CommonOps_DDRM.add(1.5, yd, 0.75, yd, zd);
 
         }
 
         }
 
         after = System.currentTimeMillis();
 
         after = System.currentTimeMillis();
         System.out.println("norm = "+ NormOps_DDRM.fastNormF(yd)+"  dense time  = "+(after-before)+" ms");
+
         System.out.println("norm = " + NormOps_DDRM.fastNormF(yd) + "  dense time  = " + (after - before) + " ms");
 
 
 
     }
 
     }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 08:25, 7 July 2021

Support for sparse matrices has recently been added to EJML. It supports many but not all of the standard operations that are supported for dense matrics. The code below shows the basics of working with a sparse matrix. In some situations the speed improvement of using a sparse matrix can be substantial. Do note that if your system isn't sparse enough or if its structure isn't advantageous it could run even slower using sparse operations!

Type Execution Time (ms)
Dense 12660
Sparse 1642

External Resources:

Sparse Matrix Example

/**
 * Example showing how to construct and solve a linear system using sparse matrices
 *
 * @author Peter Abeles
 */
public class ExampleSparseMatrix {
    public static int ROWS = 100000;
    public static int COLS = 1000;
    public static int XCOLS = 1;

    public static void main( String[] args ) {
        Random rand = new Random(234);

        // easy to work with sparse format, but hard to do computations with
        // NOTE: It is very important to you set 'initLength' to the actual number of elements in the final array
        //       If you don't it will be forced to thrash memory as it grows its internal data structures.
        //       Failure to heed this advice will make construction of large matrices 4x slower and use 2x more memory
        DMatrixSparseTriplet work = new DMatrixSparseTriplet(5, 4, 5);
        work.addItem(0, 1, 1.2);
        work.addItem(3, 0, 3);
        work.addItem(1, 1, 22.21234);
        work.addItem(2, 3, 6);

        // convert into a format that's easier to perform math with
        DMatrixSparseCSC Z = DConvertMatrixStruct.convert(work, (DMatrixSparseCSC)null);

        // print the matrix to standard out in two different formats
        Z.print();
        System.out.println();
        Z.printNonZero();
        System.out.println();

        // Create a large matrix that is 5% filled
        DMatrixSparseCSC A = RandomMatrices_DSCC.rectangle(ROWS, COLS, (int)(ROWS*COLS*0.05), rand);
        //          large vector that is 70% filled
        DMatrixSparseCSC x = RandomMatrices_DSCC.rectangle(COLS, XCOLS, (int)(XCOLS*COLS*0.7), rand);

        System.out.println("Done generating random matrices");
        // storage for the initial solution
        DMatrixSparseCSC y = new DMatrixSparseCSC(ROWS, XCOLS, 0);
        DMatrixSparseCSC z = new DMatrixSparseCSC(ROWS, XCOLS, 0);

        // To demonstration how to perform sparse math let's multiply:
        //                  y=A*x
        // Optional storage is set to null so that it will declare it internally
        long before = System.currentTimeMillis();
        IGrowArray workA = new IGrowArray(A.numRows);
        DGrowArray workB = new DGrowArray(A.numRows);
        for (int i = 0; i < 100; i++) {
            CommonOps_DSCC.mult(A, x, y, workA, workB);
            CommonOps_DSCC.add(1.5, y, 0.75, y, z, workA, workB);
        }
        long after = System.currentTimeMillis();

        System.out.println("norm = " + NormOps_DSCC.fastNormF(y) + "  sparse time = " + (after - before) + " ms");

        DMatrixRMaj Ad = DConvertMatrixStruct.convert(A, (DMatrixRMaj)null);
        DMatrixRMaj xd = DConvertMatrixStruct.convert(x, (DMatrixRMaj)null);
        DMatrixRMaj yd = new DMatrixRMaj(y.numRows, y.numCols);
        DMatrixRMaj zd = new DMatrixRMaj(y.numRows, y.numCols);

        before = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            CommonOps_DDRM.mult(Ad, xd, yd);
            CommonOps_DDRM.add(1.5, yd, 0.75, yd, zd);
        }
        after = System.currentTimeMillis();
        System.out.println("norm = " + NormOps_DDRM.fastNormF(yd) + "  dense time  = " + (after - before) + " ms");
    }
}