NumTraits.h
Go to the documentation of this file.
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 namespace internal {
16 
17 // default implementation of digits10(), based on numeric_limits if specialized,
18 // 0 for integer types, and log10(epsilon()) otherwise.
19 template< typename T,
20  bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
23 {
24  static int run() { return std::numeric_limits<T>::digits10; }
25 };
26 
27 template<typename T>
28 struct default_digits10_impl<T,false,false> // Floating point
29 {
30  static int run() {
31  using std::log10;
32  using std::ceil;
33  typedef typename NumTraits<T>::Real Real;
34  return int(ceil(-log10(NumTraits<Real>::epsilon())));
35  }
36 };
37 
38 template<typename T>
39 struct default_digits10_impl<T,false,true> // Integer
40 {
41  static int run() { return 0; }
42 };
43 
44 } // end namespace internal
45 
88 template<typename T> struct GenericNumTraits
89 {
90  enum {
91  IsInteger = std::numeric_limits<T>::is_integer,
92  IsSigned = std::numeric_limits<T>::is_signed,
93  IsComplex = 0,
94  RequireInitialization = internal::is_arithmetic<T>::value ? 0 : 1,
95  ReadCost = 1,
96  AddCost = 1,
97  MulCost = 1
98  };
99 
100  typedef T Real;
101  typedef typename internal::conditional<
102  IsInteger,
104  T
105  >::type NonInteger;
106  typedef T Nested;
107  typedef T Literal;
108 
109  EIGEN_DEVICE_FUNC
110  static inline Real epsilon()
111  {
112  return numext::numeric_limits<T>::epsilon();
113  }
114 
115  EIGEN_DEVICE_FUNC
116  static inline int digits10()
117  {
119  }
120 
121  EIGEN_DEVICE_FUNC
122  static inline Real dummy_precision()
123  {
124  // make sure to override this for floating-point types
125  return Real(0);
126  }
127 
128 
129  EIGEN_DEVICE_FUNC
130  static inline T highest() {
132  }
133 
134  EIGEN_DEVICE_FUNC
135  static inline T lowest() {
136  return IsInteger ? (numext::numeric_limits<T>::min)() : (-(numext::numeric_limits<T>::max)());
137  }
138 
139  EIGEN_DEVICE_FUNC
140  static inline T infinity() {
141  return numext::numeric_limits<T>::infinity();
142  }
143 
144  EIGEN_DEVICE_FUNC
145  static inline T quiet_NaN() {
146  return numext::numeric_limits<T>::quiet_NaN();
147  }
148 };
149 
150 template<typename T> struct NumTraits : GenericNumTraits<T>
151 {};
152 
153 template<> struct NumTraits<float>
154  : GenericNumTraits<float>
155 {
156  EIGEN_DEVICE_FUNC
157  static inline float dummy_precision() { return 1e-5f; }
158 };
159 
160 template<> struct NumTraits<double> : GenericNumTraits<double>
161 {
162  EIGEN_DEVICE_FUNC
163  static inline double dummy_precision() { return 1e-12; }
164 };
165 
166 template<> struct NumTraits<long double>
167  : GenericNumTraits<long double>
168 {
169  static inline long double dummy_precision() { return 1e-15l; }
170 };
171 
172 template<typename _Real> struct NumTraits<std::complex<_Real> >
173  : GenericNumTraits<std::complex<_Real> >
174 {
175  typedef _Real Real;
177  enum {
178  IsComplex = 1,
183  };
184 
185  EIGEN_DEVICE_FUNC
186  static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
187  EIGEN_DEVICE_FUNC
188  static inline Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
189  EIGEN_DEVICE_FUNC
190  static inline int digits10() { return NumTraits<Real>::digits10(); }
191 };
192 
193 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
194 struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
195 {
201  typedef ArrayType & Nested;
203 
204  enum {
208  RequireInitialization = 1,
209  ReadCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::ReadCost,
210  AddCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::AddCost,
211  MulCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::MulCost
212  };
213 
214  EIGEN_DEVICE_FUNC
215  static inline RealScalar epsilon() { return NumTraits<RealScalar>::epsilon(); }
216  EIGEN_DEVICE_FUNC
217  static inline RealScalar dummy_precision() { return NumTraits<RealScalar>::dummy_precision(); }
218 
219  static inline int digits10() { return NumTraits<Scalar>::digits10(); }
220 };
221 
222 template<> struct NumTraits<std::string>
223  : GenericNumTraits<std::string>
224 {
225  enum {
226  RequireInitialization = 1,
227  ReadCost = HugeCost,
228  AddCost = HugeCost,
229  MulCost = HugeCost
230  };
231 
232  static inline int digits10() { return 0; }
233 
234 private:
235  static inline std::string epsilon();
236  static inline std::string dummy_precision();
237  static inline std::string lowest();
238  static inline std::string highest();
239  static inline std::string infinity();
240  static inline std::string quiet_NaN();
241 };
242 
243 // Empty specialization for void to allow template specialization based on NumTraits<T>::Real with T==void and SFINAE.
244 template<> struct NumTraits<void> {};
245 
246 } // end namespace Eigen
247 
248 #endif // EIGEN_NUMTRAITS_H
static EIGEN_DEVICE_FUNC Real dummy_precision()
Definition: NumTraits.h:188
double epsilon
const int HugeCost
Definition: Constants.h:39
static int f(const TensorMap< Tensor< int, 3 > > &tensor)
Definition: LDLT.h:16
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
static EIGEN_DEVICE_FUNC double dummy_precision()
Definition: NumTraits.h:163
NumTraits< _Real >::Literal Literal
Definition: NumTraits.h:176
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half() max(const half &a, const half &b)
Definition: Half.h:438
EIGEN_DEVICE_FUNC const CeilReturnType ceil() const
Array< Scalar, Rows, Cols, Options, MaxRows, MaxCols > ArrayType
Definition: NumTraits.h:196
static EIGEN_DEVICE_FUNC Real epsilon()
Definition: NumTraits.h:186
def is_integer(t)
static long double dummy_precision()
Definition: NumTraits.h:169
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:45
Array< NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols > NonInteger
Definition: NumTraits.h:200
static EIGEN_DEVICE_FUNC float dummy_precision()
Definition: NumTraits.h:157
EIGEN_DEVICE_FUNC const Log10ReturnType log10() const
Array< RealScalar, Rows, Cols, Options, MaxRows, MaxCols > Real
Definition: NumTraits.h:198
const int Dynamic
Definition: Constants.h:21
static EIGEN_DEVICE_FUNC int digits10()
Definition: NumTraits.h:190


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:30