I am relatively new to Eigen, and am facing the following issue when using sparse matrices in Eigen.
When I use the below code, the allocatedsize for the variable C increases to 20 after addition. 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
temp.reserve((std::max)(src.rows(),src.cols())*2);
and afterwards, temp
is moved to the actual destination. 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.
Independent of that, if you are working with N x 1
sparse matrices you should consider switching to SparseVector<double>
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.