1
votes

To declare a vector of dense matrix in Eigen I am using the following format

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

where idx is the vector size. 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.

1
You could do something like std::vector<Eigen::SparseMatrix<double>> BMATvec(idx, BMAT);. Is that sufficient for you?JHBonarius
I forgot to mention that the size of the matrix can be different for each index. So the correct definition of the dense matrix would be std::vector<Eigen::MatrixXd> AMAT(idx, Eigen::MatrixXd::Zero(x,x)); where x can be different for each idxpk68
Following declaration seems to work. std::vector<Eigen::SparseMatrix<double>> BMAT(idx, Eigen::SparseMatrix<double> (x,x));. Thanks for your help JHBonariuspk68
Hmm, sounds like you should use a generator. The current vector constructor you are using is filling all the items with copied of the same initializer object. You probaly have to resize them all after this.JHBonarius

1 Answers

0
votes

Seeing you want the matrices in the vector to have differen sizes, you should probably not use that initialization constructor for 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);});
}