简体   繁体   English

Eigen和std :: vector

[英]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线性代数库,如下所示:

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可以使用它吗?

Eigen uses contiguous memory, as does std::vector . Eigen使用连续内存, 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). 但是,外部std::vector包含一组连续的std::vector<std::complex<double> > ,每个都指向一组不同的复数(并且可以是不同的长度)。 Therefore, the std "matrix" is not contiguous. 因此,std“矩阵”不是连续的。 What you can do is copy the data to the Eigen matrix, there are multiple ways of doing that. 你可以做的是将数据复制到Eigen矩阵,有多种方法可以做到这一点。 The simplest would be to loop over i and j , with a better option being something like 最简单的是循环遍历ij ,更好的选择是类似的

Eigen::MatrixXcd mat(rows, cols);
for(int i = 0; i < cols; i++)
    mat.col(i) = Eigen::Map<Eigen::VectorXcd> (A[i].data(), rows);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM