The most common choices are CSC or CSR storage. These are both efficient for matrix-vector multiplication. It's also very easy to code those multiplication routines, if you have to do it yourself.
That said, Yale storage also yields very efficient matrix-vector multiply. If you are performing matrix element lookup, then you have misunderstood how to use the format. I suggest you study some of the standard sparse libraries to learn how matrix-vector multiplication is implemented.
Even with your current storage you can perform matrix multiplication in O(n) complexity. All sparse matrix-vector multiplication algorithms that I have ever seen boil down to the same steps. For example consider y = Ax.
- Zeroise the result vector, y.
- Initialise an iterator for the non-zero elements of the matrix, A.
- Get the next non-zero element of the matrix, A[i,j] say. Note that the pattern of i,j doesn't follow a regular pattern. It simply reflects the order in which the non-zero elements of A are stored.
- y[i] += A[i,j]*x[j]
- If there are more elements of A, goto 3.
I suspect you are writing the classic double for loop dense multiplication code:
for (i=0; i<N; i++)
for (j=0; j<N; j++)
y[i] += A[i,j]*x[j]
and that's what is leading you to perform lookups.
But I'm not suggesting that you stick with your std::map
storage. That's not going to be super efficient. I'd recommend CSC mainly because it is the most widely used.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…