c++eigen3

Why Eigen does not resolve built-in symbols? (all, last, seq, etc...)


I have this piece of code form the Eigen documentation site about slicing and indexing:

#include <iostream>
#include <Eigen/Dense>
#include <vector>
using namespace std;
using namespace Eigen;

int main() {
    std::vector<int> ind{4,2,5,5,3};
    MatrixXi A = MatrixXi::Random(4,6);
    cout << "Initial matrix A:\n" << A << "\n\n";
    cout << "A(all,ind):\n" << A(all,ind) << "\n\n";

    return 0;
}

When I try to compile, I get multiple errors, for example:

How can I fix these errors?


It looks as if I had the wrong version of Eigen (it worked here), however, according to this answer I have: EIGEN_WORLD_VERSION 3 EIGEN_MAJOR_VERSION 3 EIGEN_MINOR_VERSION 7, which I believe is the latest.

As far as installation is concerned, I copied the Eigen folder to the project location and supplied a path (-I flag) to one folder above it for a g++ compiler. The library itself seems to work well; for example, this code (from supplied examples) works fine:

#include <iostream>
#include <Eigen/Dense>

using namespace Eigen;
using namespace std;

int main()
{
  Matrix3d m = Matrix3d::Random();
  m = (m + Matrix3d::Constant(1.2)) * 50;
  cout << "m =" << endl << m << endl;
  Vector3d v(1,2,3);
  
  cout << "m * v =" << endl << m * v << endl;
}

Solution

  • Your problem is probably the -I option to g++

    I suspect you have something like:

    g++  ....  -I<path_to_project>/Eigen
    

    ... whereas it should only be

    g++  ....  -I<path_to_project>
    

    ... i.e., the final Eigen directory should not be on the include path.

    Make this change, and then also change the source code so that all includes are like:

    #include <Eigen/Dense>
    #include <Eigen/Cholesky>
    

    Additionally, you are referring to a variable all but your program has not defined it.

    Also ensure you are enabling c++11 compiler option at least.


    EDIT:

    For completeness sake, I add the answer that appeared in the comments. The documentation in OP's question refers to the 3.3.9 version which does not support symbols all, last,seq. For the most recent stable (3.3.7) version block or reshape operators must be used.

    The lesson here is: always check if the documentation version matches the version of the used library.