2008-08-24 23:15:32 +08:00
namespace Eigen {
2010-07-04 16:14:47 +08:00
/** \page TopicCustomizingEigen Customizing/Extending Eigen
2008-08-24 23:15:32 +08:00
2009-11-20 01:09:04 +08:00
Eigen can be extended in several ways, for instance, by defining global methods, \ref ExtendingMatrixBase "by adding custom methods to MatrixBase", adding support to \ref CustomScalarType "custom types" etc.
2008-08-24 23:15:32 +08:00
2008-09-14 19:59:10 +08:00
\b Table \b of \b contents
- \ref ExtendingMatrixBase
2009-02-09 06:36:28 +08:00
- \ref InheritingFromMatrix
2008-09-14 19:59:10 +08:00
- \ref CustomScalarType
2008-08-24 23:15:32 +08:00
\section ExtendingMatrixBase Extending MatrixBase
In this section we will see how to add custom methods to MatrixBase. Since all expressions and matrix types inherit MatrixBase, adding a method to MatrixBase make it immediately available to all expressions ! A typical use case is, for instance, to make Eigen compatible with another API.
2010-04-29 20:04:42 +08:00
You certainly know that in C++ it is not possible to add methods to an existing class. So how that's possible ? Here the trick is to include in the declaration of MatrixBase a file defined by the preprocessor token \c EIGEN_MATRIXBASE_PLUGIN:
2008-08-24 23:15:32 +08:00
\code
class MatrixBase {
// ...
#ifdef EIGEN_MATRIXBASE_PLUGIN
#include EIGEN_MATRIXBASE_PLUGIN
#endif
};
\endcode
Therefore to extend MatrixBase with you own methods you just have to create a file with your method declaration and define EIGEN_MATRIXBASE_PLUGIN before you include any Eigen's header file.
Here is an example of such an extension file: \n
\b MatrixBaseAddons.h
\code
inline Scalar at(uint i, uint j) const { return this->operator()(i,j); }
inline Scalar& at(uint i, uint j) { return this->operator()(i,j); }
inline Scalar at(uint i) const { return this->operator[](i); }
inline Scalar& at(uint i) { return this->operator[](i); }
2008-11-04 03:14:17 +08:00
inline RealScalar squaredLength() const { return squaredNorm(); }
2008-08-24 23:15:32 +08:00
inline RealScalar length() const { return norm(); }
2008-11-04 03:14:17 +08:00
inline RealScalar invLength(void) const { return fast_inv_sqrt(squaredNorm()); }
2008-08-24 23:15:32 +08:00
template<typename OtherDerived>
inline Scalar squaredDistanceTo(const MatrixBase<OtherDerived>& other) const
2008-11-04 03:14:17 +08:00
{ return (derived() - other.derived()).squaredNorm(); }
2008-08-24 23:15:32 +08:00
template<typename OtherDerived>
inline RealScalar distanceTo(const MatrixBase<OtherDerived>& other) const
2010-10-25 22:15:22 +08:00
{ return internal::sqrt(derived().squaredDistanceTo(other)); }
2008-08-24 23:15:32 +08:00
inline void scaleTo(RealScalar l) { RealScalar vl = norm(); if (vl>1e-9) derived() *= (l/vl); }
2010-01-18 03:04:49 +08:00
inline Transpose<Derived> transposed() {return this->transpose();}
inline const Transpose<Derived> transposed() const {return this->transpose();}
2008-08-24 23:15:32 +08:00
2010-01-18 03:04:49 +08:00
inline uint minComponentId(void) const { int i; this->minCoeff(&i); return i; }
inline uint maxComponentId(void) const { int i; this->maxCoeff(&i); return i; }
2008-08-24 23:15:32 +08:00
template<typename OtherDerived>
2010-01-18 03:04:49 +08:00
void makeFloor(const MatrixBase<OtherDerived>& other) { derived() = derived().cwiseMin(other.derived()); }
2008-08-24 23:15:32 +08:00
template<typename OtherDerived>
2010-01-18 03:04:49 +08:00
void makeCeil(const MatrixBase<OtherDerived>& other) { derived() = derived().cwiseMax(other.derived()); }
2008-08-24 23:15:32 +08:00
2010-10-25 22:15:22 +08:00
const CwiseUnaryOp<internal::scalar_add_op<Scalar>, Derived>
2010-01-18 03:04:49 +08:00
operator+(const Scalar& scalar) const
2010-10-25 22:15:22 +08:00
{ return CwiseUnaryOp<internal::scalar_add_op<Scalar>, Derived>(derived(), internal::scalar_add_op<Scalar>(scalar)); }
2008-08-24 23:15:32 +08:00
2010-10-25 22:15:22 +08:00
friend const CwiseUnaryOp<internal::scalar_add_op<Scalar>, Derived>
2010-01-18 03:04:49 +08:00
operator+(const Scalar& scalar, const MatrixBase<Derived>& mat)
2010-10-25 22:15:22 +08:00
{ return CwiseUnaryOp<internal::scalar_add_op<Scalar>, Derived>(mat.derived(), internal::scalar_add_op<Scalar>(scalar)); }
2008-08-24 23:15:32 +08:00
\endcode
Then one can the following declaration in the config.h or whatever prerequisites header file of his project:
\code
#define EIGEN_MATRIXBASE_PLUGIN "MatrixBaseAddons.h"
\endcode
2009-02-09 06:36:28 +08:00
\section InheritingFromMatrix Inheriting from Matrix
2008-09-14 19:59:10 +08:00
2009-02-09 06:36:28 +08:00
Before inheriting from Matrix, be really, i mean REALLY sure that using
EIGEN_MATRIX_PLUGIN is not what you really want (see previous section).
If you just need to add few members to Matrix, this is the way to go.
An example of when you actually need to inherit Matrix, is when you have
several layers of heritage such as MyVerySpecificVector1,MyVerySpecificVector1 -> MyVector1 -> Matrix and.
MyVerySpecificVector3,MyVerySpecificVector4 -> MyVector2 -> Matrix.
In order for your object to work within the Eigen framework, you need to
define a few members in your inherited class.
Here is a minimalistic example:\n
\code
class MyVectorType : public Eigen::VectorXd
{
public:
MyVectorType(void):Eigen::VectorXd() {}
// You need to define this for your object to work
typedef Eigen::VectorXd Base;
template<typename OtherDerived>
MyVectorType & operator= (const Eigen::MatrixBase <OtherDerived>& other)
{
this->Base::operator=(other);
return *this;
}
};
\endcode
This is the kind of error you can get if you don't provide those methods
\code
error: no match for ‘ operator=’ in ‘ delta =
(((Eigen::MatrixBase<Eigen::Matrix<std::complex<float>, 10000, 1, 2, 10000,
1> >*)(& delta)) + 8u)->Eigen::MatrixBase<Derived>::cwise [with Derived =
Eigen::Matrix<std::complex<float>, 10000, 1, 2, 10000,
1>]().Eigen::Cwise<ExpressionType>::operator* [with OtherDerived =
Eigen::Matrix<std::complex<float>, 10000, 1, 2, 10000, 1>, ExpressionType =
Eigen::Matrix<std::complex<float>, 10000, 1, 2, 10000, 1>](((const
Eigen::MatrixBase<Eigen::Matrix<std::complex<float>, 10000, 1, 2, 10000, 1>
>&)(((const Eigen::MatrixBase<Eigen::Matrix<std::complex<float>, 10000, 1,
>2, 10000, 1> >*)((const spectral1d*)where)) + 8u)))’
\endcode
2008-09-14 19:59:10 +08:00
2010-01-15 22:45:07 +08:00
\anchor user_defined_scalars \section CustomScalarType Using custom scalar types
2008-09-14 19:59:10 +08:00
By default, Eigen currently supports the following scalar types: \c int, \c float, \c double, \c std::complex<float>, \c std::complex<double>, \c long \c double, \c long \c long \c int (64 bits integers), and \c bool. The \c long \c double is especially useful on x86-64 systems or when the SSE2 instruction set is enabled because it enforces the use of x87 registers with extended accuracy.
In order to add support for a custom type \c T you need:
1 - make sure the common operator (+,-,*,/,etc.) are supported by the type \c T
2008-09-15 23:45:41 +08:00
2 - add a specialization of struct Eigen::NumTraits<T> (see \ref NumTraits)
2010-10-25 22:15:22 +08:00
3 - define a couple of math functions for your type such as: internal::sqrt, internal::abs, etc...
2008-09-14 19:59:10 +08:00
(see the file Eigen/src/Core/MathFunctions.h)
2009-09-27 07:56:50 +08:00
Here is a concrete example adding support for the Adolc's \c adouble type. <a href="https://projects.coin-or.org/ADOL-C">Adolc</a> is an automatic differentiation library. The type \c adouble is basically a real value tracking the values of any number of partial derivatives.
2008-09-14 19:59:10 +08:00
\code
#ifndef ADLOCSUPPORT_H
#define ADLOCSUPPORT_H
#define ADOLC_TAPELESS
#include <adolc/adouble.h>
#include <Eigen/Core>
namespace Eigen {
template<> struct NumTraits<adtl::adouble>
{
typedef adtl::adouble Real;
2010-04-29 06:51:38 +08:00
typedef adtl::adouble NonInteger;
typedef adtl::adouble Nested;
2008-09-14 19:59:10 +08:00
enum {
IsComplex = 0,
2010-04-29 06:51:38 +08:00
IsInteger = 0,
IsSigned,
2008-09-14 19:59:10 +08:00
ReadCost = 1,
AddCost = 1,
MulCost = 1
};
};
}
// the Adolc's type adouble is defined in the adtl namespace
2010-10-25 22:15:22 +08:00
// therefore, the following internal::* functions *must* be defined
2008-09-14 19:59:10 +08:00
// in the same namespace
namespace adtl {
2010-10-25 22:15:22 +08:00
inline const adouble& internal::conj(const adouble& x) { return x; }
inline const adouble& internal::real(const adouble& x) { return x; }
inline adouble internal::imag(const adouble&) { return 0.; }
inline adouble internal::abs(const adouble& x) { return fabs(x); }
inline adouble internal::abs2(const adouble& x) { return x*x; }
inline adouble internal::sqrt(const adouble& x) { return sqrt(x); }
inline adouble internal::exp(const adouble& x) { return exp(x); }
inline adouble internal::log(const adouble& x) { return log(x); }
inline adouble internal::sin(const adouble& x) { return sin(x); }
inline adouble internal::cos(const adouble& x) { return cos(x); }
inline adouble internal::pow(const adouble& x, adouble y) { return pow(x, y); }
2008-09-14 19:59:10 +08:00
}
#endif // ADLOCSUPPORT_H
\endcode
2010-12-27 23:07:07 +08:00
\sa \ref TopicPreprocessorDirectives
2008-08-24 23:15:32 +08:00
*/
}