[eigen] static initialization of a const Eigen::Matrix

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

Is it possible to statically initialize a const Eigen::Matrix whose
size and entries are known at compile time?  I.e, something like:

const Eigen::Matrix<float, 2, 2> A = {1.0f, 2.0f, 3.0f, 4.0f};

I wrote on the forum and there was no response, and I searched the
mailing list archives but didn't find anything.  I tried variations on
the above syntax but wasn't able to get anything to work.

On the #eigen IRC channel, one user suggested:

EIGEN_ALIGN16 static const double data[]= {1.0f, 2.0f, 3.0f, 4.0f};
static const Eigen::Matrix<float, 2, 2>::ConstAlignedMapType xx(data);

In the development branch Doxygen, it looks like the Map class offers
the functionality that I'm looking for:

  static const float A_[4] = {1.0f, 2.0f, 3.0f, 4.0f};
  Map< const Matrix<float, 2, 2> > A(A_);
  std::cout << "sizeof(A_) = " << sizeof(A_) << std::endl;
  std::cout << "sizeof(A) = " << sizeof(A) << std::endl;
  std::cout << A << std::endl;

which gives the output:

sizeof(A_) = 16
sizeof(A) = 16
1 3
2 4

Can I safely assume that Map is not a copy but instead just a
convenient wrapper to the C-array and that the matrix elements are not
being copied?

I tried debugging this program and depending on the optimization
settings (g++ -O0 or -O2), it seems like the Map of the float array
may or may not result in instructions being generated.  I'm looking
for a solution that generates little to no overhead as I am on an
embedded environment where space is tight, so ideally static
initialization of the Map or Matrix instance would be performed.  I
presume there are some other member variables besides the matrix
elements that need to be initialized before it can be used, which is
fine, I just want to avoid copies  of large matrices occuring, both
for space and for cpu usage considerations.

Are there a set of best practices established for accomplishing what I
am trying to acheive?


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