[eigen] Eigen C++11 alias templates

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


Hi all,

Another C++11 feature I'd like to see available to users is alias
templates for commonly used types.

Currently, users can create e.g. an arbitrary sized matrix of doubles
using the Eigen::MatrixXd typedef, defined in Eigen/src/Core/Matrix.h.
However, if one wants to do this with a templated type, it is necessary
to write out in full each time:

    Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> mat;

The attached header defines alias templates similar to MatrixXd, etc.,
which allow for the same functionality with less verbosity.  With this,
the above type definition can be written as:

    Eigen::MatrixX<T> mat;

I've been using this now for several months and it makes my code much
clearer.  Please feel free to adapt it and include it in Eigen.

Regards,
Jim
#ifndef _EIGEN3_TYPEDEFS
#define _EIGEN3_TYPEDEFS

// adapted from Eigen/src/Core/Matrix.h

namespace Eigen
{

#define EIGEN_MAKE_TYPEDEFS(Size, SizeSuffix)                     \
/** \ingroup matrixtypedefs */                                    \
template <typename Type>                                          \
using Matrix##SizeSuffix = Matrix<Type, Size, Size>;              \
/** \ingroup matrixtypedefs */                                    \
template <typename Type>                                          \
using Vector##SizeSuffix = Matrix<Type, Size, 1>;                 \
/** \ingroup matrixtypedefs */                                    \
template <typename Type>                                          \
using RowVector##SizeSuffix = Matrix<Type, 1, Size>;

#define EIGEN_MAKE_FIXED_TYPEDEFS(Size)                           \
/** \ingroup matrixtypedefs */                                    \
template <typename Type>                                          \
using Matrix##Size##X = Matrix<Type, Size, Dynamic>;              \
/** \ingroup matrixtypedefs */                                    \
template <typename Type>                                          \
using Matrix##X##Size = Matrix<Type, Dynamic, Size>;

EIGEN_MAKE_TYPEDEFS(2, 2)
EIGEN_MAKE_TYPEDEFS(3, 3)
EIGEN_MAKE_TYPEDEFS(4, 4)
EIGEN_MAKE_TYPEDEFS(Dynamic, X)
EIGEN_MAKE_FIXED_TYPEDEFS(2)
EIGEN_MAKE_FIXED_TYPEDEFS(3)
EIGEN_MAKE_FIXED_TYPEDEFS(4)

#undef EIGEN_MAKE_TYPEDEFS
#undef EIGEN_MAKE_FIXED_TYPEDEFS

}

#endif


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