/csr-formatter

C++ package to store Matrix Market (.mtx) file format sparse matrices in Compressed Row Storage (CSR) format.

Primary LanguageC++

csr-formatter

C++ package to store Matrix Market (.mtx) file format sparse matrices in Compressed Sparse Row (CSR) format.

About

Compressed Sparse Row is a matrix storaging technique that only cares about non-zero values in the matrix and, for that reason, it is more memory efficient than regular matrix storaging costs. Instead of storaging elements, CSR format storages elements, where equals the number of non-zero elements in the matrix.

A simple explanation on the CSR format can be found in netlib.org.

A nice collection of .mtx format matrices can be seen in SuiteSparse Matrix Collection.

Usage

Save csr_formatter.h to your code directory and inform it as a header in your file.

#include "csr_formatter.h"

When dealing with a simetric matrix:

#include "csr_formatter.h"

int main(int argc, char* argv[]){
  CSR sim = assemble_simetric_csr_matrix("simetricMatrix.mtx");
}

Otherwise (asymmetric):

#include "csr_formatter.h"

int main(int argc, char* argv[]){
  CSR asym = assemble_csr_matrix("asymmetricMatrix.mtx");
}

Both functions will return a CSR object with the three characteristic csr format lists: col_ind, data and row_ptr.

Aditional functions that might be useful for the user were also implemented.

To obtain the matrix bandwidth, use:

#include "csr_formatter.h"

int main(int argc, char* argv[]){
  CSR asym = assemble_csr_matrix("asymmetricMatrix.mtx");
  cout << "Matrix bandwidth is " << getBandwidth(asym) << '\n';
}

To obtain the degree of a given vertex:

#include "csr_formatter.h"

int main(int argc, char* argv[]){
  CSR asym = assemble_csr_matrix("asymmetricMatrix.mtx");
  cout << "Degree of 2: " << getDegree(asym.row_ptr, 2) << '\n';
}

To obtain the adjacency list of a vertex:

#include "csr_formatter.h"

int main(int argc, char* argv[]){
  CSR asym = assemble_csr_matrix("asymmetricMatrix.mtx");
  cout << "Adjcents to 2: \n";
  printArray(getAdjVertices(asym.col_ind, asym.row_ptr, 2));
}

The example folder contains an example usage file with two example matrices that can be used, one simetric (494_bus.mtx) and one asymmetric (gre_343.mtx).

Any doubts/suggestions, feel free to contact me.