Re: [eigen] [FAQ] how to have an initialized static Matrix

[ Thread Index | Date Index | More Archives ]

On 03/21/2013 11:36:24 AM, Christoph Hertzberg wrote:
On 21.03.2013 10:30, Helmut Jarausch wrote:
unfortunately, Eigen's elegant initialization is an expression.

Is there an "elegant" solution for this ugly code (within a function)

static double _C[6] = { 1.0 / 5, 3.0 / 10, 4.0 / 5, 8.0 / 9, 1.0, 1.0 }; static const Eigen::Map<Eigen::Matrix<double,6,1>,0> C((double*)_C);

I found these three solutions:

typedef Eigen::Matrix<double, 6, 1> Vec6d; // for brevity

// Comma-initializer:
static const Vec6d C0 =
    (Vec6d() << 1.0/5, 3.0/10, 4.0/5, 8.0/9, 1.0, 1.0).finished();

I didn't think of this myself. I like it. Especially, when initializing matrices with more then one row/column this versions shows the coefficients as we mathematicians
write them down, i.e. row wise.

// Initialize from const data
static const double C1_data[] = {1.0/5, 3.0/10, 4.0/5, 8.0/9, 1.0, 1.0};
static const Vec6d C1(C1_data);

// Map to aligned const data:
static const double C2_data[] = {1.0/5, 3.0/10, 4.0/5, 8.0/9, 1.0, 1.0};
static const Eigen::Map<const Vec6d, Eigen::Aligned> C2(C2_data);

The first two most likely will have a one-time copy overhead, the third might have a very minor overhead when using it (e.g. if you pass it by reference, two pointers need to be dereferenced instead of one).

What about Matrix with a constructor taking an initializer_list ?

That would be possible with C++11 only (still worth thinking about).

Yes, please. It's only a few additional constructors which could be generated only if
__cplusplus is 201103L or beyond.

But, if we are at it, what about move constructors or move assignment operators,

Mail converted by MHonArc 2.6.19+