How to store "Sparse matrix"

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.

and by the way, don't make duplicate threads
Last edited on
Thanks "hamsterman", can you code? i'm beginner so it is very difficult to understand to me.
If you have any code please post it.
Thanks
I have never used sparselib++. It's just something I googled. Though I suppose that they would have all needed operators overloaded. Some things are explained in http://math.nist.gov/sparselib++/sparselib-userguide.pdf

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.

Option 2 is greatly inferior to option 1.


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++.
Topic archived. No new replies allowed.