Eigen and std::vector
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?
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);