Pergunta

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?

Foi útil?

Solução

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

Outras dicas

There is a la4j (Linear Algebra for Java) library that handles this with CRSMatrix/CCSMatrix sparse matrix types and MatrixMarketStream class. Here is the brief example:

Source file "matrix.mm" (5x5 matrix with 8 non-zero elements):

%%MatrixMarket matrix coordinate real general
5  5  8
1     1   1.000e+00
2     2   1.050e+01
3     3   1.500e-02
1     4   6.000e+00
4     2   2.505e+02
4     4  -2.800e+02
4     5   3.332e+01
5     5   1.200e+01

Java source (la4j usage):

Matrix a = new CRSMatrix(Matrices.asMatrixMarketSource(
                           new FileInputStream(new File("matrix.mm"))));

System.out.println("DET(A) = " + a.determinant());

About the MatrixMarket format read here. About CRS/CCS formats read here and here.

To generate MatrixMarket output use following code:

Matrix a = new CRSMatrix(...);
MatrixStream out = new MatrixMarketStream(new FileOutputStream(
                        new File("matrix.mm")));

out.writeMatrix(a);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top