Search code examples
c++reshapeeigen

Eigen 3.4 .reshaped()


I want to reshape an Eigen vector into a matrix and then take the .colwise().sum().

The reshape command as explained in the Eigen tutorial isn't compiling (https://eigen.tuxfamily.org/dox-devel/group__TutorialReshape.html)

Hence my question is as much about Eigen version numbering (has Eigen 3.4 been released?) as about the reshape command itself (my compile error says "Eigen::VectorXd has no member named 'reshaped'") and advice about an efficient alternative to the following:

VectorXd phi = X * beta; ArrayXd sumPhi = phi.reshaped(4,12).colwise().sum();


Solution

  • Eigen 3.4 has not been released (as of May 2019), I suggest trying out the development branch.

    As phi is an actual object (and not an expression), you could achieve the same using a Map:

    ArrayXd sumPhi = MatrixXd::Map(phi.data(),4,12).colwise().sum();
    

    This should work with any version of Eigen (at least since 3.0), but is less safe as it does not ensure that the number of elements actually match (if phi has less than 4*12 elements, this could access invalid memory).