c++ - Eigen Library:: How do I create a block diagonal sparse matrix out of existing sparse matrices? -
i have bunch of (n*n) sized sparse matrices called m1, m2... , mj.
i want create large block-diagonal sparse matrix looks this:
|m1 0 0 . . . | |0 m2 0 . . . | |. . . . . . | |. . . mj-1 0| |0 0 0 ... mj| i tried following:
eigen::sparsematrix<double> matblk(j*n,j*n); matblk.reserve(eigen::vectorxd::constant(j*n,3); //i know there @ 3 nonzero elements per row matblk.topleftcorner(n,n) = m1.topleftcorner(n,n); matblk.block(n,n,n,n) = m2.topleftcorner(n,n); . . matblk(bottomrightcorner(n,n)) = mj.topleftcorner(n,n); matblk.makecompressed(); this method not working. values in smaller matrices aren't getting copied larger block matrix. function:
matblk.nonzeros() returns 0.
i new library. appreciated.
unfortunately looks can't assign sparse matrices in way due how inefficient resulting code be. forum post 2 years old seems things still same (https://forum.kde.org/viewtopic.php?f=74&t=112018)
you have assign entries 1 one, either direct assignment or triplets.
a.block(i,j,m,n) = b; becomes
for (int ii = i; ii < i+m; ++ii) { (int jj = j; jj < j+n; ++jj) { // direct assignment a.insert(ii, jj) = b(ii - i, jj - j); // triplets triplets.push_back(triplet(ii, jj, b(ii-i,jj-j))); } }
Comments
Post a Comment