Eigen和std :: vector [英] Eigen and std::vector

查看:1155
本文介绍了Eigen和std :: vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵,它给出为:

I have a matrix, which is given as:

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

我想将它映射到Eigen线性代数库,如下所示:

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

但是代码失败了

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

是否还有转换向量的向量以使Eigen可以使用它?

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

推荐答案

Eigen使用连续内存, std :: vector 然而,外部 std :: vector 包含 std :: vector< std :: complex< double> > ,每个都指向一组不同的复数(并且可以是不同的长度)。因此,std矩阵不是连续的。你可以做的是将数据复制到Eigen矩阵,有多种方法。最简单的是循环 i j ,更好的选项是

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

这篇关于Eigen和std :: vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆