简体   繁体   中英

Multiplying sparse sub-matrices in Eigen

I am trying to multiply sub-matrices such as blocks, columns, and rows of Eigen::SparseMatrix. However, whenever multiple sub-matrices are involved my program crashes (and gdb with it). I am working with Eigen 3.2.1.

Here an example:

const unsigned m = 3, d = 1;
SparseMatrix<double> H(3*m,3*m);
H.setIdentity();
SparseMatrix<double> G(m,d);
G.coeffRef(0,0) = 1;

// this works
SparseMatrix<double> H_00 = H.block(0,0,m,m);
double val = SparseMatrix<double>(G.col(0).transpose() * H_00 * G.col(0)).coeffRef(0,0);

// this crashes
val = SparseMatrix<double>(G.col(0).transpose() * H.block(0,0,m,m) * G.col(0)).coeffRef(0,0);

Is there a way of avoiding the costly ( m and d >> 1000) construction of H_00?

I checked out the 3.2.1 version of Eigen and this appears to be related to this Eigen bug: http://eigen.tuxfamily.org/bz/show_bug.cgi?id=875

What happens is that a function (named SparseMatrixBase::nonZeros() ) internally is called recursively Unfortunately, this was not properly fixed in the 3.2 branch (with version 3.2.10 your example does not compile). It does work with the latest 3.3 version of Eigen, however (3.3rc1 at the moment) -- which answers your question: Yes, upgrade to Eigen 3.3rc1 (or newer)

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.

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