4

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

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

1 Answers1

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.

Maverick
  • 420
  • 5
  • 16
Darren Smith
  • 2,261
  • 16
  • 16
  • I changed the code accordingly to your advice. The `#include ` 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()`. – Maverick Nov 04 '19 at 20:20
  • 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. – Darren Smith Nov 04 '19 at 20:35
  • ... indeed if I compile your program against 3.3.90 it does succeed (although I use explicit namespace also, eg, `Eigen::all`) – Darren Smith Nov 04 '19 at 20:41