Committer:
ykuroda
Date:
Thu Oct 13 04:07:23 2016 +0000
Revision:
0:13a5d365ba16
First commint, Eigne Matrix Class Library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ykuroda 0:13a5d365ba16 1 // This file is part of Eigen, a lightweight C++ template library
ykuroda 0:13a5d365ba16 2 // for linear algebra.
ykuroda 0:13a5d365ba16 3 //
ykuroda 0:13a5d365ba16 4 // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
ykuroda 0:13a5d365ba16 5 //
ykuroda 0:13a5d365ba16 6 // This Source Code Form is subject to the terms of the Mozilla
ykuroda 0:13a5d365ba16 7 // Public License v. 2.0. If a copy of the MPL was not distributed
ykuroda 0:13a5d365ba16 8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
ykuroda 0:13a5d365ba16 9
ykuroda 0:13a5d365ba16 10 #ifndef EIGEN_NUMTRAITS_H
ykuroda 0:13a5d365ba16 11 #define EIGEN_NUMTRAITS_H
ykuroda 0:13a5d365ba16 12
ykuroda 0:13a5d365ba16 13 namespace Eigen {
ykuroda 0:13a5d365ba16 14
ykuroda 0:13a5d365ba16 15 /** \class NumTraits
ykuroda 0:13a5d365ba16 16 * \ingroup Core_Module
ykuroda 0:13a5d365ba16 17 *
ykuroda 0:13a5d365ba16 18 * \brief Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
ykuroda 0:13a5d365ba16 19 *
ykuroda 0:13a5d365ba16 20 * \param T the numeric type at hand
ykuroda 0:13a5d365ba16 21 *
ykuroda 0:13a5d365ba16 22 * This class stores enums, typedefs and static methods giving information about a numeric type.
ykuroda 0:13a5d365ba16 23 *
ykuroda 0:13a5d365ba16 24 * The provided data consists of:
ykuroda 0:13a5d365ba16 25 * \li A typedef \a Real, giving the "real part" type of \a T. If \a T is already real,
ykuroda 0:13a5d365ba16 26 * then \a Real is just a typedef to \a T. If \a T is \c std::complex<U> then \a Real
ykuroda 0:13a5d365ba16 27 * is a typedef to \a U.
ykuroda 0:13a5d365ba16 28 * \li A typedef \a NonInteger, giving the type that should be used for operations producing non-integral values,
ykuroda 0:13a5d365ba16 29 * such as quotients, square roots, etc. If \a T is a floating-point type, then this typedef just gives
ykuroda 0:13a5d365ba16 30 * \a T again. Note however that many Eigen functions such as internal::sqrt simply refuse to
ykuroda 0:13a5d365ba16 31 * take integers. Outside of a few cases, Eigen doesn't do automatic type promotion. Thus, this typedef is
ykuroda 0:13a5d365ba16 32 * only intended as a helper for code that needs to explicitly promote types.
ykuroda 0:13a5d365ba16 33 * \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
ykuroda 0:13a5d365ba16 34 * this means, just use \a T here.
ykuroda 0:13a5d365ba16 35 * \li An enum value \a IsComplex. It is equal to 1 if \a T is a \c std::complex
ykuroda 0:13a5d365ba16 36 * type, and to 0 otherwise.
ykuroda 0:13a5d365ba16 37 * \li An enum value \a IsInteger. It is equal to \c 1 if \a T is an integer type such as \c int,
ykuroda 0:13a5d365ba16 38 * and to \c 0 otherwise.
ykuroda 0:13a5d365ba16 39 * \li Enum values ReadCost, AddCost and MulCost representing a rough estimate of the number of CPU cycles needed
ykuroda 0:13a5d365ba16 40 * to by move / add / mul instructions respectively, assuming the data is already stored in CPU registers.
ykuroda 0:13a5d365ba16 41 * Stay vague here. No need to do architecture-specific stuff.
ykuroda 0:13a5d365ba16 42 * \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.
ykuroda 0:13a5d365ba16 43 * \li An enum value \a RequireInitialization. It is equal to \c 1 if the constructor of the numeric type \a T must
ykuroda 0:13a5d365ba16 44 * 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.
ykuroda 0:13a5d365ba16 45 * \li An epsilon() function which, unlike std::numeric_limits::epsilon(), returns a \a Real instead of a \a T.
ykuroda 0:13a5d365ba16 46 * \li A dummy_precision() function returning a weak epsilon value. It is mainly used as a default
ykuroda 0:13a5d365ba16 47 * value by the fuzzy comparison operators.
ykuroda 0:13a5d365ba16 48 * \li highest() and lowest() functions returning the highest and lowest possible values respectively.
ykuroda 0:13a5d365ba16 49 */
ykuroda 0:13a5d365ba16 50
ykuroda 0:13a5d365ba16 51 template<typename T> struct GenericNumTraits
ykuroda 0:13a5d365ba16 52 {
ykuroda 0:13a5d365ba16 53 enum {
ykuroda 0:13a5d365ba16 54 IsInteger = std::numeric_limits<T>::is_integer,
ykuroda 0:13a5d365ba16 55 IsSigned = std::numeric_limits<T>::is_signed,
ykuroda 0:13a5d365ba16 56 IsComplex = 0,
ykuroda 0:13a5d365ba16 57 RequireInitialization = internal::is_arithmetic<T>::value ? 0 : 1,
ykuroda 0:13a5d365ba16 58 ReadCost = 1,
ykuroda 0:13a5d365ba16 59 AddCost = 1,
ykuroda 0:13a5d365ba16 60 MulCost = 1
ykuroda 0:13a5d365ba16 61 };
ykuroda 0:13a5d365ba16 62
ykuroda 0:13a5d365ba16 63 typedef T Real;
ykuroda 0:13a5d365ba16 64 typedef typename internal::conditional<
ykuroda 0:13a5d365ba16 65 IsInteger,
ykuroda 0:13a5d365ba16 66 typename internal::conditional<sizeof(T)<=2, float, double>::type,
ykuroda 0:13a5d365ba16 67 T
ykuroda 0:13a5d365ba16 68 >::type NonInteger;
ykuroda 0:13a5d365ba16 69 typedef T Nested;
ykuroda 0:13a5d365ba16 70
ykuroda 0:13a5d365ba16 71 static inline Real epsilon() { return std::numeric_limits<T>::epsilon(); }
ykuroda 0:13a5d365ba16 72 static inline Real dummy_precision()
ykuroda 0:13a5d365ba16 73 {
ykuroda 0:13a5d365ba16 74 // make sure to override this for floating-point types
ykuroda 0:13a5d365ba16 75 return Real(0);
ykuroda 0:13a5d365ba16 76 }
ykuroda 0:13a5d365ba16 77 static inline T highest() { return (std::numeric_limits<T>::max)(); }
ykuroda 0:13a5d365ba16 78 static inline T lowest() { return IsInteger ? (std::numeric_limits<T>::min)() : (-(std::numeric_limits<T>::max)()); }
ykuroda 0:13a5d365ba16 79
ykuroda 0:13a5d365ba16 80 #ifdef EIGEN2_SUPPORT
ykuroda 0:13a5d365ba16 81 enum {
ykuroda 0:13a5d365ba16 82 HasFloatingPoint = !IsInteger
ykuroda 0:13a5d365ba16 83 };
ykuroda 0:13a5d365ba16 84 typedef NonInteger FloatingPoint;
ykuroda 0:13a5d365ba16 85 #endif
ykuroda 0:13a5d365ba16 86 };
ykuroda 0:13a5d365ba16 87
ykuroda 0:13a5d365ba16 88 template<typename T> struct NumTraits : GenericNumTraits<T>
ykuroda 0:13a5d365ba16 89 {};
ykuroda 0:13a5d365ba16 90
ykuroda 0:13a5d365ba16 91 template<> struct NumTraits<float>
ykuroda 0:13a5d365ba16 92 : GenericNumTraits<float>
ykuroda 0:13a5d365ba16 93 {
ykuroda 0:13a5d365ba16 94 static inline float dummy_precision() { return 1e-5f; }
ykuroda 0:13a5d365ba16 95 };
ykuroda 0:13a5d365ba16 96
ykuroda 0:13a5d365ba16 97 template<> struct NumTraits<double> : GenericNumTraits<double>
ykuroda 0:13a5d365ba16 98 {
ykuroda 0:13a5d365ba16 99 static inline double dummy_precision() { return 1e-12; }
ykuroda 0:13a5d365ba16 100 };
ykuroda 0:13a5d365ba16 101
ykuroda 0:13a5d365ba16 102 template<> struct NumTraits<long double>
ykuroda 0:13a5d365ba16 103 : GenericNumTraits<long double>
ykuroda 0:13a5d365ba16 104 {
ykuroda 0:13a5d365ba16 105 static inline long double dummy_precision() { return 1e-15l; }
ykuroda 0:13a5d365ba16 106 };
ykuroda 0:13a5d365ba16 107
ykuroda 0:13a5d365ba16 108 template<typename _Real> struct NumTraits<std::complex<_Real> >
ykuroda 0:13a5d365ba16 109 : GenericNumTraits<std::complex<_Real> >
ykuroda 0:13a5d365ba16 110 {
ykuroda 0:13a5d365ba16 111 typedef _Real Real;
ykuroda 0:13a5d365ba16 112 enum {
ykuroda 0:13a5d365ba16 113 IsComplex = 1,
ykuroda 0:13a5d365ba16 114 RequireInitialization = NumTraits<_Real>::RequireInitialization,
ykuroda 0:13a5d365ba16 115 ReadCost = 2 * NumTraits<_Real>::ReadCost,
ykuroda 0:13a5d365ba16 116 AddCost = 2 * NumTraits<Real>::AddCost,
ykuroda 0:13a5d365ba16 117 MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
ykuroda 0:13a5d365ba16 118 };
ykuroda 0:13a5d365ba16 119
ykuroda 0:13a5d365ba16 120 static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
ykuroda 0:13a5d365ba16 121 static inline Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
ykuroda 0:13a5d365ba16 122 };
ykuroda 0:13a5d365ba16 123
ykuroda 0:13a5d365ba16 124 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
ykuroda 0:13a5d365ba16 125 struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
ykuroda 0:13a5d365ba16 126 {
ykuroda 0:13a5d365ba16 127 typedef Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> ArrayType;
ykuroda 0:13a5d365ba16 128 typedef typename NumTraits<Scalar>::Real RealScalar;
ykuroda 0:13a5d365ba16 129 typedef Array<RealScalar, Rows, Cols, Options, MaxRows, MaxCols> Real;
ykuroda 0:13a5d365ba16 130 typedef typename NumTraits<Scalar>::NonInteger NonIntegerScalar;
ykuroda 0:13a5d365ba16 131 typedef Array<NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols> NonInteger;
ykuroda 0:13a5d365ba16 132 typedef ArrayType & Nested;
ykuroda 0:13a5d365ba16 133
ykuroda 0:13a5d365ba16 134 enum {
ykuroda 0:13a5d365ba16 135 IsComplex = NumTraits<Scalar>::IsComplex,
ykuroda 0:13a5d365ba16 136 IsInteger = NumTraits<Scalar>::IsInteger,
ykuroda 0:13a5d365ba16 137 IsSigned = NumTraits<Scalar>::IsSigned,
ykuroda 0:13a5d365ba16 138 RequireInitialization = 1,
ykuroda 0:13a5d365ba16 139 ReadCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::ReadCost,
ykuroda 0:13a5d365ba16 140 AddCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::AddCost,
ykuroda 0:13a5d365ba16 141 MulCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::MulCost
ykuroda 0:13a5d365ba16 142 };
ykuroda 0:13a5d365ba16 143
ykuroda 0:13a5d365ba16 144 static inline RealScalar epsilon() { return NumTraits<RealScalar>::epsilon(); }
ykuroda 0:13a5d365ba16 145 static inline RealScalar dummy_precision() { return NumTraits<RealScalar>::dummy_precision(); }
ykuroda 0:13a5d365ba16 146 };
ykuroda 0:13a5d365ba16 147
ykuroda 0:13a5d365ba16 148 } // end namespace Eigen
ykuroda 0:13a5d365ba16 149
ykuroda 0:13a5d365ba16 150 #endif // EIGEN_NUMTRAITS_H