I was using Eigen matrix framework, and the SparseVector library. I was running into performance issues, and I all need from it is Sparse vector dot product. So I rolled my own SparseMatrix implemenation, hoping it’d somehow be a bit faster:
A bit of sample code:
#include <map>
using namespace std ;
struct SparseMatrix
{
map<int, Vector> vals ;
Vector dot( SparseMatrix& o )
{
SparseMatrix *LS, *RS ;
// iterate over the smaller of the 2
if( vals.size() < o.vals.size() )
{
// walk vals
LS = this ;
RS = &o ;
}
else
{
LS = &o ;
RS = this ;
}
// walk LS
Vector sum = 0 ;
for( map<int,Vector>::iterator LSIter = LS->vals.begin() ; LSIter != LS->vals.end() ; ++LSIter )
{
const int& key = LSIter->first ;
// use the key, see if RS has a similar entry.
map<int,Vector>::iterator RSIter = RS->vals.find( key );
if( RSIter != RS->vals.end() )
sum += RSIter->second * LSIter->second ;
}
return sum ;
}
} ;
So the dot product of 2 vectors, say that had entries like:
+---------------+ | vec 1 | | index value | | 2 18 | | 7 4 | | 18 33 | +---------------+ +---------------+ | vec 2 | | index value | | 2 1 | | 15 87 | | 21 92 | +---------------+
The dot product is then 18.
So, as you can see I used std::map to do element look up, to see if an element from one vector is in another vector.
Since I’m using integer indexing and 1d arrays only, is there a way I can make the lookup faster? My sparse matrix multiply is still a bottleneck (performance of my code is only marginally faster than Eigen)
Have a vector of pairs: index -> value, sorted by index. Then iterate both vectors at once. If you have the same index in two, multiply values, add this to result and go to the next pair in both vectors. Else increment iteration index on the vector where the first element of pair is smaller.
Assuming you don’t modify data a lot, of course.
I.e. in pseudocode: