从一个稀疏矩阵中提取一个块,作为另一个稀疏矩阵。

5
如何从 Eigen::SparseMatrix<double> 中提取一个块。似乎没有我用于密集矩阵的方法。
‘class Eigen::SparseMatrix<double>’ has no member named ‘topLeftCorner’
‘class Eigen::SparseMatrix<double>’ has no member named ‘block’

有一种方法可以将一个块提取为 Eigen::SparseMatrix<double> 吗?

也许没有方法,因为它是一个稀疏矩阵,可能为空,因此提取起来没有什么用处? - Lee Jacobs
3个回答

5

我编写了这个函数来从一个Eigen::SparseMatrix<double,ColMaior>中提取块

typedef Triplet<double> Tri;
SparseMatrix<double> sparseBlock(SparseMatrix<double,ColMajor> M,
        int ibegin, int jbegin, int icount, int jcount){
        //only for ColMajor Sparse Matrix
    assert(ibegin+icount <= M.rows());
    assert(jbegin+jcount <= M.cols());
    int Mj,Mi,i,j,currOuterIndex,nextOuterIndex;
    vector<Tri> tripletList;
    tripletList.reserve(M.nonZeros());

    for(j=0; j<jcount; j++){
        Mj=j+jbegin;
        currOuterIndex = M.outerIndexPtr()[Mj];
        nextOuterIndex = M.outerIndexPtr()[Mj+1];

        for(int a = currOuterIndex; a<nextOuterIndex; a++){
            Mi=M.innerIndexPtr()[a];

            if(Mi < ibegin) continue;
            if(Mi >= ibegin + icount) break;

            i=Mi-ibegin;    
            tripletList.push_back(Tri(i,j,M.valuePtr()[a]));
        }
    }
    SparseMatrix<double> matS(icount,jcount);
    matS.setFromTriplets(tripletList.begin(), tripletList.end());
    return matS;
}

如果子矩阵在四个角之一:

SparseMatrix<double> sparseTopLeftBlock(SparseMatrix<double> M,
        int icount, int jcount){
    return sparseBlock(M,0,0,icount,jcount);
}
SparseMatrix<double> sparseTopRightBlock(SparseMatrix<double> M,
        int icount, int jcount){
    return sparseBlock(M,0,M.cols()-jcount,icount,jcount);
}
SparseMatrix<double> sparseBottomLeftBlock(SparseMatrix<double> M,
        int icount, int jcount){
    return sparseBlock(M,M.rows()-icount,0,icount,jcount);
}
SparseMatrix<double> sparseBottomRightBlock(SparseMatrix<double> M,
        int icount, int jcount){
    return sparseBlock(M,M.rows()-icount,M.cols()-jcount,icount,jcount);
}

1
也许您可以扩展一下,添加测试并将其发送到eigen邮件列表。 - Jakob

4

这在Eigen 3.2.2文档中得到支持(虽然较早的版本可能也支持)。

#include <iostream>
#include <Eigen/Dense>
#include <Eigen/Sparse>

using namespace Eigen;

int main()
{
  MatrixXd silly(6, 3);

  silly << 0, 1, 2,
           0, 3, 0,
           2, 0, 0,
           3, 2, 1,
           0, 1, 0,
           2, 0, 0;



  SparseMatrix<double, RowMajor> sparse_silly = silly.sparseView();

  std::cout <<"Whole Matrix" << std::endl;
  std::cout << sparse_silly << std::endl;

  std::cout << "block of matrix" << std::endl;
  std::cout << sparse_silly.block(1,1,3,2) << std::endl;

  return 0;
}

1

对于稀疏矩阵中的子矩阵,支持非常有限(抱歉,没有双关语)。实际上,您只能访问行优先和列优先的连续一组行和列。原因不是矩阵可能为空,而是索引方案比密集矩阵更加复杂。对于密集矩阵,您只需要一个额外的步幅数字即可支持子矩阵支持。


您链接的页面显示支持阻塞稀疏矩阵,也许自上次发布以来已经更新。 - Akavall

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接