Search code examples
javaalgorithmmatrixsparse-matrixcolt

Create Sparse Matrix with Coordinate Storage System?


I am writing a java program which involves working with a 1058 X 1058 matrix containing float values. This matrix contains many zero values and so I need to store this as a sparse matrix and later use this matrix to generate a spanning tree. I checked many popular libraries like Colt, Jama, but somehow I am unable to put them to work with my code. I would like to have a coordinate storage system (similar to obtained in matlab using the sparse() function) like this:

(1055,1045)    1.0000
(1056,1045)    1.0000
(1057,1045)    1.0000
(1058,1045)    1.0000
(1047,1046)    1.0000
(1048,1046)    1.0000
(1049,1046)    1.0000
(1050,1046)    1.0000
(1051,1046)    1.0000
(1052,1046)    1.0000
(1053,1046)    1.0000
(1054,1046)    1.0000
(1055,1046)    1.0000

Can anyone suggest how to go about this?


Solution

  • You can do it quickly without any lib. Create the following class :

    MatrixIndex implements Comparable<MatrixIndex>
    {
      private final int _x;
      private final int _y;
    
      ...
    }
    

    Then use it in some :

    TreeMap<MatrixIndex,Double>
    

    Cheers