Multidimensional arrays in eigen library

試著忘記壹切 提交于 2019-12-10 16:18:16

问题


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.


回答1:


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:


Multidimensional arrays are supported through the new Tensor module:

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



来源:https://stackoverflow.com/questions/30917762/multidimensional-arrays-in-eigen-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!