有时,ago用户ggael给出了将eigen::vectorXf映射到eigen::matrixXf的问题的答案。
现在,我需要做一些类似于现有矩阵的事情,例如,我知道我可以:
for(int i=0;i<p;++i){
VectorXf vec=q.col(i);
/*q is a p**2 by n matrix*/
Map<MatrixXf> qi(vec.data(),p,p);
/*run function that uses qi to produce a scalare and store that scalar*/
}但是(在我看来)在循环之外一次性创建qi,然后反复使用相同的qi会更有效(对吗?)
此外,我想知道将q.col(i)映射到vec的中间步骤是否真的有必要……
最近的回答proposes to do:
qi=Map<MatrixXd>(vec.data(),p,p);但这样做会产生以下结果:
In function ‘Eigen::VectorXi DepType(const MatrixXf&, const MatrixXf&, const int&)’:
DeC.cpp:279:34: error: no matching function for call to ‘Eigen::Map<Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001> >::Map(Eigen::PlainObjectBase<Eigen::Matrix<float, -0x00000000000000001, 1> >::Scalar*, int&, int&)’
DeC.cpp:279:34: note: candidates are:
/eigen/Eigen/src/Core/Map.h:179:12: note: Eigen::Map<MatrixType, MapOptions, StrideType>::Map(Eigen::Map<MatrixType, MapOptions, StrideType>::PointerArgType, Eigen::Map<MatrixType, MapOptions, StrideType>::Index, Eigen::Map<MatrixType, MapOptions, StrideType>::Index, const StrideType&) [with PlainObjectType = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>, int MapOptions = 0, StrideType = Eigen::Stride<0, 0>, Eigen::Map<MatrixType, MapOptions, StrideType>::PointerArgType = double*, Eigen::Map<MatrixType, MapOptions, StrideType>::Index = long int]
/home/kaveh/Desktop/work/p1/geqw4/vi3/out/sp/ccode/eigen/Eigen/src/Core/Map.h:179:12: note: no known conversion for argument 1 from ‘Eigen::PlainObjectBase<Eigen::Matrix<float, -0x00000000000000001, 1> >::Scalar* {aka float*}’ to ‘double*’
/eigen/Eigen/src/Core/Map.h:166:12: note: Eigen::Map<MatrixType, MapOptions, StrideType>::Map(Eigen::Map<MatrixType, MapOptions, StrideType>::PointerArgType, Eigen::Map<MatrixType, MapOptions, StrideType>::Index, const StrideType&) [with PlainObjectType = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>, int MapOptions = 0, StrideType = Eigen::Stride<0, 0>, Eigen::Map<MatrixType, MapOptions, StrideType>::PointerArgType = double*, Eigen::Map<MatrixType, MapOptions, StrideType>::Index = long int]
/eigen/Eigen/src/Core/Map.h:166:12: note: no known conversion for argument 1 from ‘Eigen::PlainObjectBase<Eigen::Matrix<float, -0x00000000000000001, 1> >::Scalar* {aka float*}’ to ‘double*’
/eigen/Eigen/src/Core/Map.h:154:12: note: Eigen::Map<MatrixType, MapOptions, StrideType>::Map(Eigen::Map<MatrixType, MapOptions, StrideType>::PointerArgType, const StrideType&) [with PlainObjectType = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>, int MapOptions = 0, StrideType = Eigen::Stride<0, 0>, Eigen::Map<MatrixType, MapOptions, StrideType>::PointerArgType = double*]
/eigen/Eigen/src/Core/Map.h:154:12: note: candidate expects 2 arguments, 3 provided
/eigen/Eigen/src/Core/Map.h:119:79: note: Eigen::Map<Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001> >::Map(const Eigen::Map<Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001> >&)
/eigen/Eigen/src/Core/Map.h:119:79: note: candidate expects 1 argument, 3 provided例如,似乎Eigen认为qi是一个vector....:(
发布于 2013-01-11 02:37:37
以下内容:
qi=Map<MatrixXd>(vec.data(),p,p);意味着您希望将Map(vec.data(),p,p)的系数复制到qi引用的矩阵中。但是,您真正想要的是重新初始化Map<>对象。为此,必须使用C++的构造新语法再次调用构造函数:
new (&qi) Map<MatrixXd>(vec.data(),p,p);此外,我必须指出,Map<>对象是非常轻量级的:它只有一个指针和两个静态分配在堆栈上的整数。因此,将Map<> qi的声明移出循环不会对性能产生任何影响。另一方面,请注意,您不需要将q.col(i)复制到临时缓冲区。如果q是主要列,你可以直接这样做:
Map<MatrixXf> qi(q.col(i).data(),p,p);https://stackoverflow.com/questions/14264413
复制相似问题