Eigen and std::vector
Asked Answered
T

1

6

I have a matrix, which is given as:

std::vector<std::vector<std::complex<double>>> A;

And I want to map that to the Eigen linear algebra library like this:

Eigen::Map<Eigen::MatrixXcd, Eigen::RowMajor> mat(A.data(),51,51);

But the code fails with

error: no matching function for call to        
‘Eigen::Map<Eigen::Matrix<std::complex<double>, -1, -1>, 1>::

Is there anyway to convert a vector of a vector so that Eigen can use it?

Timisoara answered 12/11, 2015 at 9:47 Comment(2)
Why would you have a vector of vectors in the first place?Shape
I thought this was an easy way to store a matrix? What is a better way?Timisoara
R
16

Eigen uses contiguous memory, as does std::vector. However, the outer std::vector contains a contiguous set of std::vector<std::complex<double> >, each pointing to a different set of complex numbers (and can be different lengths). Therefore, the std "matrix" is not contiguous. What you can do is copy the data to the Eigen matrix, there are multiple ways of doing that. The simplest would be to loop over i and j, with a better option being something like

Eigen::MatrixXcd mat(rows, cols);
for(int i = 0; i < cols; i++)
    mat.col(i) = Eigen::Map<Eigen::VectorXcd> (A[i].data(), rows);
Rodmur answered 12/11, 2015 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.