简体   繁体   English

特征库,使用稀疏矩阵的简单线性代数运算增加了它们的分配大小

[英]Eigen library, simple linear algebra operations with sparse matrices increasing their allocated size

I am relatively new to Eigen, and am facing the following issue when using sparse matrices in Eigen. 我对Eigen相对较新,在Eigen中使用稀疏矩阵时面临以下问题。

When I use the below code, the allocatedsize for the variable C increases to 20 after addition. 当我使用下面的代码时,变量C的allocatedsize在添加后增加到20。 I am lost as to why is this happening. 我迷失了为什么会发生这种情况。

Eigen::SparseMatrix< double > A( 10, 1 );
A.reserve( Eigen::VectorXi::Constant(1,3) );

A.coeffRef( 2, 0 ) = 2;
A.coeffRef( 3, 0 ) = 3;
A.coeffRef( 7, 0 ) = 7;

Eigen::SparseMatrix< double > B( 10, 1 );
B.reserve( Eigen::VectorXi::Constant(1,3) );

B.coeffRef( 0, 0 ) = 0;
B.coeffRef( 1, 0 ) = 1;
B.coeffRef( 8, 0 ) = 8;

Eigen::SparseMatrix< double > C( 10, 1 );
C.reserve( Eigen::VectorXi::Constant(1,6) );

C = A + B;

It looks like in assign_sparse_to_sparse there is a line 看起来在assign_sparse_to_sparse有一条线

temp.reserve((std::max)(src.rows(),src.cols())*2);

and afterwards, temp is moved to the actual destination. 然后,将temp移动到实际目的地。 That means, prior reserving (and resizing) in your case does not help. 这意味着,在您的情况下预先保留(和调整大小)并没有帮助。 I'm not sure though, why temp is not reserved to nonZerosEstimate() of the corresponding evaluator. 我不确定,为什么temp不保留给相应评估器的nonZerosEstimate()

Independent of that, if you are working with N x 1 sparse matrices you should consider switching to SparseVector<double> instead. 独立SparseVector<double> ,如果您使用N x 1稀疏矩阵,则应考虑切换到SparseVector<double>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM