Search code examples
javamatrixsparse-matrixcolt

How can I create 1000000 x 1000000 sparse matrix using Java Colt?


public class SparseMatrix
{       
     static SparseObjectMatrix2D matrix = new SparseObjectMatrix2D(1000000, 1000000);

     public static void main(String[] args)
     {
            matrix.set(1, 2, 3.0);
     }      
}

Here is the error that I'm getting:

java.lang.ExceptionInInitializerError Caused by:
java.lang.IllegalArgumentException: matrix too large at
cern.colt.matrix.impl.AbstractMatrix2D.setUp(Unknown Source) at
cern.colt.matrix.impl.AbstractMatrix2D.setUp(Unknown Source) at
cern.colt.matrix.impl.SparseObjectMatrix2D.<init>(Unknown Source) at
cern.colt.matrix.impl.SparseObjectMatrix2D.<init>(Unknown Source) at
SparseMatrix.<clinit>(SparseMatrix.java:18) Exception in thread "main"

Solution

  • You cannot. From the documentation:

    Throws:
    IllegalArgumentException - if rows<0 || columns<0 || > (double)columns*rows > Integer.MAX_VALUE.

    Instead of creating a matrix addressed with x and y coordinates, returning a Value, create a HashMap<Coordinates, Value>, where Coordinates is a simple class holding x and y.