Multidimensional arrays in eigen library

10.9k Views Asked by At

3 simple questions about the usage and future of the excellent eigen library:

  1. Does it have a reason why the access to Matrices is not possible via matrix[i][j], but only via matrix(i,j)?
  2. Are there plans to implement such a syntax?
  3. Will there be an implementation of multidimensional arrays matrix[n][m]...[l]?

I really like the eigen library, it is fast and easy to use. The only thing missing for me are really multidimensional arrays.

2

There are 2 best solutions below

1
On BEST ANSWER

I can't speak for the eigen library because I've never used it, but I can speak to the design of the code. In order to use the [][] notation, that typically means that the matrix is built upon underlying vectors that have also overloaded the [] operator.

It's possible the author of the eigen library did not want to go through the trouble of defining vectors to be the basis of the matrix classes.

Take the following example.

class Matrix {
   Vector& operator[](std::size_t ind);
};

class Vector {
   double& operator[](std::size_t ind);
};

Allows us to use the Matrix class like this:

Matrix matrix;
matrix[0][0] = 1.2;

Where as defining the peren operator is typically easier because it doesn't also rely on the implementation of a Vector class:

class Matrix {
    double& operator()(std::size_t i, std::size_t j);
    const double& operator()(std::size_t i, std::size_t j) const;
};

Allows us to use the Matrix class like this:

Matrix matrix;
matrix(4, 3) = 9.2;
2
On

Multidimensional arrays are supported through the new Tensor module:

http://eigen.tuxfamily.org/dox-devel/unsupported/group__CXX11__Tensor__Module.html