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