Eigne Matrix Class Library

Dependents:   Eigen_test Odometry_test AttitudeEstimation_usingTicker MPU9250_Quaternion_Binary_Serial ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NumTraits.h Source File

NumTraits.h

00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_NUMTRAITS_H
00011 #define EIGEN_NUMTRAITS_H
00012 
00013 namespace Eigen {
00014 
00015 /** \class NumTraits
00016   * \ingroup Core_Module
00017   *
00018   * \brief Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
00019   *
00020   * \param T the numeric type at hand
00021   *
00022   * This class stores enums, typedefs and static methods giving information about a numeric type.
00023   *
00024   * The provided data consists of:
00025   * \li A typedef \a Real, giving the "real part" type of \a T. If \a T is already real,
00026   *     then \a Real is just a typedef to \a T. If \a T is \c std::complex<U> then \a Real
00027   *     is a typedef to \a U.
00028   * \li A typedef \a NonInteger, giving the type that should be used for operations producing non-integral values,
00029   *     such as quotients, square roots, etc. If \a T is a floating-point type, then this typedef just gives
00030   *     \a T again. Note however that many Eigen functions such as internal::sqrt simply refuse to
00031   *     take integers. Outside of a few cases, Eigen doesn't do automatic type promotion. Thus, this typedef is
00032   *     only intended as a helper for code that needs to explicitly promote types.
00033   * \li A typedef \a Nested giving the type to use to nest a value inside of the expression tree. If you don't know what
00034   *     this means, just use \a T here.
00035   * \li An enum value \a IsComplex. It is equal to 1 if \a T is a \c std::complex
00036   *     type, and to 0 otherwise.
00037   * \li An enum value \a IsInteger. It is equal to \c 1 if \a T is an integer type such as \c int,
00038   *     and to \c 0 otherwise.
00039   * \li Enum values ReadCost, AddCost and MulCost representing a rough estimate of the number of CPU cycles needed
00040   *     to by move / add / mul instructions respectively, assuming the data is already stored in CPU registers.
00041   *     Stay vague here. No need to do architecture-specific stuff.
00042   * \li An enum value \a IsSigned. It is equal to \c 1 if \a T is a signed type and to 0 if \a T is unsigned.
00043   * \li An enum value \a RequireInitialization. It is equal to \c 1 if the constructor of the numeric type \a T must
00044   *     be called, and to 0 if it is safe not to call it. Default is 0 if \a T is an arithmetic type, and 1 otherwise.
00045   * \li An epsilon() function which, unlike std::numeric_limits::epsilon(), returns a \a Real instead of a \a T.
00046   * \li A dummy_precision() function returning a weak epsilon value. It is mainly used as a default
00047   *     value by the fuzzy comparison operators.
00048   * \li highest() and lowest() functions returning the highest and lowest possible values respectively.
00049   */
00050 
00051 template<typename T> struct GenericNumTraits
00052 {
00053   enum {
00054     IsInteger = std::numeric_limits<T>::is_integer,
00055     IsSigned = std::numeric_limits<T>::is_signed,
00056     IsComplex = 0,
00057     RequireInitialization = internal::is_arithmetic<T>::value ? 0 : 1,
00058     ReadCost = 1,
00059     AddCost = 1,
00060     MulCost = 1
00061   };
00062 
00063   typedef T Real;
00064   typedef typename internal::conditional<
00065                      IsInteger,
00066                      typename internal::conditional<sizeof(T)<=2, float, double>::type,
00067                      T
00068                    >::type NonInteger;
00069   typedef T Nested;
00070 
00071   static inline Real epsilon() { return std::numeric_limits<T>::epsilon(); }
00072   static inline Real dummy_precision()
00073   {
00074     // make sure to override this for floating-point types
00075     return Real(0);
00076   }
00077   static inline T highest() { return (std::numeric_limits<T>::max)(); }
00078   static inline T lowest()  { return IsInteger ? (std::numeric_limits<T>::min)() : (-(std::numeric_limits<T>::max)()); }
00079   
00080 #ifdef EIGEN2_SUPPORT
00081   enum {
00082     HasFloatingPoint = !IsInteger
00083   };
00084   typedef NonInteger FloatingPoint;
00085 #endif
00086 };
00087 
00088 template<typename T> struct NumTraits : GenericNumTraits<T>
00089 {};
00090 
00091 template<> struct NumTraits<float>
00092   : GenericNumTraits<float>
00093 {
00094   static inline float dummy_precision() { return 1e-5f; }
00095 };
00096 
00097 template<> struct NumTraits<double> : GenericNumTraits<double>
00098 {
00099   static inline double dummy_precision() { return 1e-12; }
00100 };
00101 
00102 template<> struct NumTraits<long double>
00103   : GenericNumTraits<long double>
00104 {
00105   static inline long double dummy_precision() { return 1e-15l; }
00106 };
00107 
00108 template<typename _Real> struct NumTraits<std::complex<_Real> >
00109   : GenericNumTraits<std::complex<_Real> >
00110 {
00111   typedef _Real Real;
00112   enum {
00113     IsComplex = 1,
00114     RequireInitialization = NumTraits<_Real>::RequireInitialization,
00115     ReadCost = 2 * NumTraits<_Real>::ReadCost,
00116     AddCost = 2 * NumTraits<Real>::AddCost,
00117     MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
00118   };
00119 
00120   static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
00121   static inline Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
00122 };
00123 
00124 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
00125 struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
00126 {
00127   typedef Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> ArrayType;
00128   typedef typename NumTraits<Scalar>::Real RealScalar;
00129   typedef Array<RealScalar, Rows, Cols, Options, MaxRows, MaxCols> Real;
00130   typedef typename NumTraits<Scalar>::NonInteger NonIntegerScalar;
00131   typedef Array<NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols> NonInteger;
00132   typedef ArrayType & Nested;
00133   
00134   enum {
00135     IsComplex = NumTraits<Scalar>::IsComplex,
00136     IsInteger = NumTraits<Scalar>::IsInteger,
00137     IsSigned  = NumTraits<Scalar>::IsSigned,
00138     RequireInitialization = 1,
00139     ReadCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::ReadCost,
00140     AddCost  = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::AddCost,
00141     MulCost  = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::MulCost
00142   };
00143   
00144   static inline RealScalar epsilon() { return NumTraits<RealScalar>::epsilon(); }
00145   static inline RealScalar dummy_precision() { return NumTraits<RealScalar>::dummy_precision(); }
00146 };
00147 
00148 } // end namespace Eigen
00149 
00150 #endif // EIGEN_NUMTRAITS_H