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

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




2013/3/21 Christoph Hertzberg <chtz@xxxxxxxxxxxxxxxxxxxxxxxx>
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();

Note that in large software projects that care about their startup times, static initializers (i.e. static-lifetime things initialized by calling constructors) are frowned upon because they can seriously slow down your DSO's load time. See e.g.

http://glandium.org/blog/?p=1068

That's of course only a concern if you are concerned about load times, which is meaningful only for large enough DSOs.

Benoit

 

// 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:
EIGEN_ALIGN16
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).

hth
Christoph

--
----------------------------------------------
Dipl.-Inf., Dipl.-Math. Christoph Hertzberg
Cartesium 0.049
Universität Bremen
Enrique-Schmidt-Straße 5
28359 Bremen

Tel: +49 (421) 218-64252
----------------------------------------------





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