C++ 从稀疏矩阵中提取一个块作为另一个稀疏矩阵
如何从C++ 从稀疏矩阵中提取一个块作为另一个稀疏矩阵,c++,matrix,sparse-matrix,eigen,C++,Matrix,Sparse Matrix,Eigen,如何从特征::SparseMatrix中提取块。似乎没有我用于密集型的方法 ‘class Eigen::SparseMatrix<double>’ has no member named ‘topLeftCorner’ ‘class Eigen::SparseMatrix<double>’ has no member named ‘block’ “类特征::SparseMatrix”没有名为“topLeftCorner”的成员 “类特征::SparseMatrix”没有
特征::SparseMatrix
中提取块。似乎没有我用于密集型的方法
‘class Eigen::SparseMatrix<double>’ has no member named ‘topLeftCorner’
‘class Eigen::SparseMatrix<double>’ has no member named ‘block’
“类特征::SparseMatrix”没有名为“topLeftCorner”的成员
“类特征::SparseMatrix”没有名为“block”的成员
有一种方法可以将块提取为
Eigen::SparseMatrix
?对的支持非常少(抱歉,没有双关语)。实际上,您只能访问一组连续的行作为行主,列作为列主。这样做的原因不是矩阵可能为空,而是索引方案比密集矩阵要复杂一些。对于密集矩阵,您只需要额外的步幅数即可支持子矩阵支持 我创建此函数是为了从Eigen::SparseMatrix
typedef三重态trip;
SparseMatrix sparseBlock(SparseMatrix M,
int ibegin、int jbegin、int icount、int jcount){
//仅适用于稀疏矩阵
assert(ibegin+icount现在在Eigen 3.2.2
中支持它(尽管早期版本也支持它)
#包括
#包括
#包括
使用名称空间特征;
int main()
{
MatrixXd-Stilly(6,3);
愚蠢的可能没有一个方法,因为它是一个稀疏矩阵,可能是空的,因此提取起来不是很有用?也许你可以对此进行一些扩展,添加测试并将其发送到eigen邮件列表。你链接到的页面显示支持分块的是稀疏矩阵,也许它自上次发布以来已被更新。
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);
}
#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;
}