I need a templated sparse matrix implementation but only to reduce memory footprint, not do any numerical solving. So I tried to use Eigen, even though I don't need the math part. Why ? It just happened to be lying on my machine, and I already had used it a little for other stuff. But I am surely no Eigen expert!
Context: I have a type T (say struct T{int a; float b; vector<int> c; }; and I need to store large matrices of this (say more than 1000x1000) and most of the values are null/irrelevant.
Since I don't do any math, I though it would be enough to provide an assignement operator to do storage / retrieval operations, like the following:
int main()
{
Eigen::SparseMatrix<MyClass> mat(1000,1000); // 1000000 elements
MyClass b{ 5, 1.2 };
mat.insert( 3, 4 ) = b;
}
So here is a datatype, with what I thought was necessary:
struct MyClass
{
int a;
float b;
std::vector<int> v_things;
MyClass( int aa, float bb ) : a(aa), b(bb) {}
MyClass( const MyClass& other ) // copy constructor
{
a = other.a;
b = other.b;
v_things = other.v_things;
}
MyClass& operator=( const MyClass& arg )
{
if( this != &arg )
{
MyClass tmp(arg);
std::swap( tmp, *this );
}
return *this;
}
};
But this fails to compile, because it seems to request some special form of the assignment operator:
/usr/include/eigen3/Eigen/src/SparseCore/SparseMatrix.h:1146:27: error: no match for ‘operator=’ (operand types are ‘Eigen::internal::CompressedStorage<MyClass, int>::Scalar {aka MyClass}’ and ‘int’)
return (m_data.value(p) = 0);'
(compiler: GCC 5.3 with -std=c++11)
Questions:
- Is it possible to do this with Eigen ?
- If yes, what do I need to add to the data type ? Is this the best approach ?
- If no, would you have suggestion on another libray?
Relevant Eigen manual pages: