I am working with sparse matrices stored in a general sparse format (compressed row). I.e. I store the matrix:
0 x y 0
0 0 0 0
z 0 0 0
in the form of:
- Matrix dimensions: [3,4]
- The offset of each row: [0,2,2,3]
- The column of each nonzero entry: [1,2,0]
- Nonzero elements: [x,y,z]
I am looking for hash function which would allow me to “cache” the sparsity patterns (i.e. the first three vectors with integers above). For that I suppose that I need a good hash function that I can supply to the hash map implementation I was planning to use, namely C++’s std::unordered_map.
Does anyone have some tips on how to find a good hash function for a problem like this?
If your CSR representation is unique per matrix, i.e. the nonzero elements appear in left-to-right, top-to-bottom order, then you can hash all the vectors and combine the hashes using
boost::hash_combine(if you don’t want a dependency on Boost, just copy-paste it, it’s very short).