hello all, I'm studying about "sparse matrix" and i have a problem with it.
I don't know how to store the sparse matrix.Please help me.
Thanks everybody.
ico
Option 1: http://math.nist.gov/sparselib++/
Option 2: Write your own class using an std::map<Position, Value>.
Consider a matrix with elements ai,j with 0 <= i < m and 0 <= j < n.
Then Position would be an integer p = i + j*m and Value would be a double v = ai,j
Option 2 is greatly inferior to option 1.
What functions do you want from your matrix class and how large do you need it to be? If it's nothing extreme, I could show you the simple solution with a map.
I disagree. Libraries like sparselib are designed for common, average case. You can do your work fast with them, but the final result will be slow.
When you want to use sparse matrices, you are probably doing something that is very specific, e.g. like solving huge equation systems or so. There are many different ways of implementing sparse matrices - with different performance characteristics. For example, it depends, whether you are going to just add those matrices or maybe multiply them, or inverse. What will be the most often required operation? Do you need to fill the zero elements while running your computations or just modify only the non-zero elements?
I agree that you can better optimize when you know what you are going to do, but my point was that OP (same goes for me) probably doesn't know the algorithms needed. I'm pretty sure that the naive implementation I suggested is a lot worse than the one in sparselib++.