[eigen] What is the 'correct' way to pass matrix results?

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


Hi,

I guess there already were numerous discussions regarding that in the
forum and on IRC. The question is how to correctly implement a function
that gets and returns Eigen matrices ...

I found several more-or-less solutions:

1) Implement an expression template -- this might be ok if your code is
   in pre-final stage ... or if you return using C++0x auto return type;
   otherwise, I think it is a bit too complicated.
2) Pass output parameters by values, inline everything and rely on your
   compiler to optimize away unnecessary overhead. I think this works
   reasonably well for small types, I experienced some overhead when
   assigning vectorized expressions to unaligned destinations, though.
3) Pass a Matrix<...> by reference. This does not work when assigning
   to a block or similar.
4) Pass a const MatrixBase<...>& as proposed here [1] and const_cast
   it. Obvious drawback of this is that it easily bypasses const-
   correctness (actually only for plain matrices, not for blocks/maps).

1 and 2 just work for single return parameters (ok, one could return a
std::pair or boost::tuple ... and assign to a tuple of references)

Another thing I would actually like sometimes, is to encode size
requirements of the matrix directly in the type, as in passing a
Matrix2d&, but still be able to pass a Block/Map (of correct size).

A solution I thought of, is to pass output parameters as some kind of
  template<class X> MatrixReference
which is a base of Block and Map, and can be constructed from a
(non-const) Matrix<...>&. However, the problem here is that implicit
conversion to templated types does not work in C++ ...

Sorry for the long mail ;)
Christoph


[1] http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html#TopicPlainFunctionsFailing





--
----------------------------------------------
Dipl.-Inf. Christoph Hertzberg
Cartesium 0.051
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/