Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c++ - Eigen: replicate items along one dimension without useless allocations

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

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...