Re: [eigen] [FAQ] how to have an initialized static Matrix |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
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();
// 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
----------------------------------------------