Eigen and std :: vector

I have a matrix that is given as:

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

And I want to map this to the Eigen linear algebra library as follows:

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

But the code crashes with

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

Do I need to convert a vector of a vector so that Eigen can use it?

+5
source share
1 answer

Eigen uses continuous memory, like std::vector . However, the external std::vector contains a continuous set of std::vector<std::complex<double> > , each of which points to a different set of complex numbers (and can be of different lengths). Therefore, the std matrix is ​​not adjacent. What you can do is copy the data into the Eigen matrix, there are several ways to do this. The easiest would be to sort through i and j , with the best option would be 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); 
+12
source

Source: https://habr.com/ru/post/1235769/


All Articles