问题
I have some vector vec and i want to obtain a new "expression" vec2 by copying values along dimension of vector
Eigen::VectorXf vec(5);
vec << 1, 2, 3, 4, 5;
const auto vec2 = vec.someAwesomeEigenMagic<3>();
//vec2 should contains (1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5)^T
//Not (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5)^T
Of course i can create such vector manually or by using replicate + vectorization by Eigen::Map:
MatrixXf tmp = vec.replicate(1, 3).transpose();
const Map<VectorXf> vec2(tmp.data(), vec.rows() * 3, 1);
But i want vec2
to be some kind of "eigen template expression" without allication (vec can be quite big and i will call this routine quite often) and immediate computing of values. (vec contains per vertex weights and i want to use it for weighted least squares)
I thought about kronecker product trick with vector of ones, but i'm not sure is it optimized for product by ones. Also i prefer to avoid unsupported module
PS Sorry for my English
回答1:
Using the devel branch you can use LinSpaced to generate the sequence of indices and then index the input vector:
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
int main()
{
VectorXf vec(5);
vec << 1, 2, 3, 4, 5;
auto vecrep = vec(ArrayXi::LinSpaced(5*3,0,4));
cout << vecrep.transpose() << endl;
}
you can then wrap the key line within a free function returning auto
, in c++14:
template<typename XprType>
auto magic_rep(const XprType &xpr, Index K) {
return xpr(Eigen::ArrayXi::LinSpaced(xpr.size()*K,0,xpr.size()-1));
}
and in main:
cout << magic_rep(vec,3).transpose() << endl;
来源:https://stackoverflow.com/questions/46110917/eigen-replicate-items-along-one-dimension-without-useless-allocations