简体   繁体   English

如何在 Eigen 中声明稀疏矩阵的向量

[英]How do I declare a vector of sparse matrix in Eigen

To declare a vector of dense matrix in Eigen I am using the following format要在 Eigen 中声明密集矩阵的向量,我使用以下格式

std::vector<Eigen::MatrixXd> AMAT(idx, Eigen::MatrixXd::Zero(1000,1000));

where idx is the vector size.其中 idx 是向量大小。 Is there an equivalent declaration to define a sparse matrix?是否有定义稀疏矩阵的等效声明? I am currently declaring a sparse matrix as我目前将稀疏矩阵声明为

Eigen::SparseMatrix<double>  BMAT(1000,1000);

It will be more efficient for to me define a vector of such matrix instead of declaring separate sparse matrix for each index.对我来说,定义这种矩阵的向量而不是为每个索引声明单独的稀疏矩阵会更有效。 Any help is appreciated.任何帮助表示赞赏。

Seeing you want the matrices in the vector to have differen sizes, you should probably not use that initialization constructor for std::vector .看到您希望向量中的矩阵具有不同的大小,您可能不应该对std::vector使用该初始化构造函数。

Instead just build-up the vector element by element:相反,只需逐个元素地构建向量:

#include <Eigen/Sparse>
#include <vector>
#include <algorithm>

int main() {
    auto sizes = { 100, 200, 300 };

    std::vector<Eigen::SparseMatrix<double>> BMATvec;
    BMATvec.reserve(sizes.size());

    std::transform(cbegin(sizes), cend(sizes), back_inserter(BMATvec),
        [](auto size) { return Eigen::SparseMatrix<double>(size,size);});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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