Re: [eigen] C++11 Style Initialisation

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Hello,

if we really want to implement new constructors I would definitely enforce
double braces as they represent a generic interface and they way Eigen handles
vectors: As matrices with fixed ColsAtCompileTime.

If we say std::initializer_list<std::initializer_list<Scalar>> is a list of rows
(as I've used it in the example plugin), shouldn't we assert here:

 VectorXd{{3,1}}; -> [3,1]^T

?

RowVectorXd{{3, 1}} -> [3, 1]^T should be legal, as well as VectorXd{{3}, {1}} -> [3, 1].
This approach would also avoid the problem of implicit transposition, Gaël mentioned.

However, is implementing new constructors better than providing static members?
We've done so in the case of random, constant or zero initialization. On the
other hand, constructing matrices using std::initializer_list would be a more
C++11-like solution.

If Toby doesn't want to or cannot find the time, I would try to implement it the next days.

Cheers,
David

On 15. Jan 2019, at 16:16, Gael Guennebaud <gael.guennebaud@xxxxxxxxx> wrote:


Hi,

this would be a very welcome addition. It seems to me that bug 954 is exactly about that (though the initial request is slightly different). Regarding c++11, it is sufficient to guard the additional c++11 code with:

#if EIGEN_HAS_CXX11
#endif

or something more specific about initializer_lists and/or variadic templates if needed.

David's addons could be a starting point that should be extended:
 - implemented as a ctor
 - asserts if the sizes don't match
 - support 1D vectors
 - support dynamic-size vectors and matrices: sizes must be extracted from the initializer lists.
 - make sure broadcasting for fixed size arrays still work: Array4f twos{2.f}; Array44f fours{4.f};

To be implemented we'll have to deal with the tricky ambiguous cases of initialing a runtime vector or matrix with either 1 or 2 elements. (we've already dealt with the compile-time versions)

Ideally, we would like to be able to write:

   VectorXd v{1,2,3,4,5,6};
   MatrixXd v{1,2,3,4}; // init a 1x4 matrix

but as mentioned by Daniel, we'll get troubles with:

  VectorXd{3}; -> non initialized 3x1 or 1x1 [3]?
  VectorXd{3,1}; -> non initialized 3x1 or 1x1 [3,1]^T?
  MatrixXd{3,2}; -> non initialized 3x2 or 1x2 [3,2]?

Possible solutions:
 - enforce the usage of a tagged ctor (as suggested by Daniel)
 - use a static member
 - enforce usage of double braces:
 VectorXd{{3}}; -> [3]
 VectorXd{{3,1}}; -> [3,1]^T
 MatrixXd{{3,2}}; -> [3,2]

If we go with the always double brace approach, we would enable implicit transposition for column vectors:

VectorXd{{3,1}}  <=> VectorXd{{3},{1}};

cheers,
Gaël.


On Tue, Jan 15, 2019 at 1:55 PM Wood, Tobias <tobias.wood@xxxxxxxxx> wrote:

Hello,

 

I would very much like to write expressions like:

 

Eigen::Matrix3f example{1, 2, 3, 4, 5, 6, 7, 8, 9};

 

I have tried searching, but I cannot see a current Bugzilla entry for functionality like this. I found http://eigen.tuxfamily.org/bz/show_bug.cgi?id=954, but that’s slightly different. I can think of several use cases for this, and there are similar StackOverflow questions (https://stackoverflow.com/questions/21280476/uniform-initialization-with-eigen-dynamic-types and https://stackoverflow.com/questions/25159349/is-it-possible-to-initialize-a-const-eigen-matrix)

 

I know that Eigen is committed to supporting C++03. Is it possible to add support for this in unsupported/cxx11? I would be tempted to try adding this myself, but I would need some pointers on where to start.

 

Interestingly,

 

Eigen::Vector3f example2{1, 2, 3};

 

does work, similarly Vector2f and Vector4f, because they have specific constructors that allow you to write

 

Eigen::Vector3f example(1, 2, 3);

 

however, these do not extend to the general case.

 

Best wishes,

Toby




Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/