Search code examples
c++performancememory-managementsparse-matrix

Triangle and sparse matrixes in C++


This is a very simple question - what is the best practice to work with triangle matrixes and to work with sparse matrices in C++?

For triangle matrix I suggest a data format as easy as

double* myMatrix;
int dimension;

as data structure in custom class. (I suggest that it was a square matrix in the full form.) And there will be methods for setting and accessing elements.

For sparse matrices - I know a couple of methods like saving just positions of elements in the row/column and their values. It's the question for your experience - which implementation of sparse matrix will be the best one?

P.S. Less memory, less CPU usage - that is my target, I am looking for the best solution, not the simplest one. All matrices will be used for solving systems of linear equations. And the size of matrices will be huge.

Thanks a lot for every advice!


Solution

  • If you have no idea about the structure of the matrix, then it is basically the same as a map. You could use std::map<std::pair<int,int>,double>. Or perhaps std::unordered_map if you have it.