我需要一个模板化的稀疏矩阵实现,但只是为了减少内存占用,not进行任何数值求解。所以我尝试使用 Eigen,尽管我不需要数学部分。为什么 ?它恰好就在我的机器上,而且我已经用它来做其他事情了。但我肯定不是本征专家!
Context: 我有一个类型T
(say struct T{int a; float b; vector<int> c; };
我需要存储这个大型矩阵(比如超过 1000x1000),并且大多数值都是空/不相关的。
由于我不做任何数学,我认为提供一个赋值运算符来执行存储/检索操作就足够了,如下所示:
int main()
{
Eigen::SparseMatrix<MyClass> mat(1000,1000); // 1000000 elements
MyClass b{ 5, 1.2 };
mat.insert( 3, 4 ) = b;
}
所以这是一个数据类型,我认为这是必要的:
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;
}
};
但这无法编译,因为它似乎需要某种特殊形式的赋值运算符:
/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);'
(编译器:GCC 5.3,带 -std=c++11)
问题:
- Is it possible to do this with Eigen ?
- 如果是,我需要在数据类型中添加什么?这是最好的方法吗?
- 如果没有,您对另一个图书馆有什么建议吗?
相关 Eigen 手册页:
- http://eigen.tuxfamily.org/dox/group__TutorialSparse.html http://eigen.tuxfamily.org/dox/group__TutorialSparse.html
- http://eigen.tuxfamily.org/dox/classEigen_1_1SparseMatrix.html http://eigen.tuxfamily.org/dox/classEigen_1_1SparseMatrix.html
- http://eigen.tuxfamily.org/dox/group__SparseQuickRefPage.html http://eigen.tuxfamily.org/dox/group__SparseQuickRefPage.html