Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

c++ - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...