4

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:

  • all is not a member of Eigen
  • all was not declared in this scope
  • last was not declared in this scope
  • seq is not a member of Eigen
  • Function seq could not be resolved
  • MatrixXi::Random invalid arguments

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; } 
2
  • 1
    Using 2 huge namespaces will lead to namespace collisions. I urge you to avoid that and hope it's just for the example. Commented Nov 4, 2019 at 19:57
  • This is a snippet directly from the example QuickStart_example2_fixed.cpp, but thanks for the good advice. Commented Nov 4, 2019 at 20:12

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I changed the code accordingly to your advice. The #include <Eigen/Dense> part works at this point, but I still get those errors. Correct me if I'm wrong, but isn't the symbol all a part of Eigen namespace and should be defined in an appropriate #include directive? Thus I wouldn't need to defile it in my main().
Well, according the document link you provided (which is development branch 3.3.9) the all should exist, but you've noted your local version is 3.3.7 ... which is the latest stable version.
... indeed if I compile your program against 3.3.90 it does succeed (although I use explicit namespace also, eg, Eigen::all)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.