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,
21  bool is_integer = NumTraits<T>::IsInteger>
23 {
25  static int run() { return std::numeric_limits<T>::digits10; }
26 };
27 
28 template<typename T>
29 struct default_digits10_impl<T,false,false> // Floating point
30 {
32  static int run() {
33  using std::log10;
34  using std::ceil;
35  typedef typename NumTraits<T>::Real Real;
37  }
38 };
39 
40 template<typename T>
41 struct default_digits10_impl<T,false,true> // Integer
42 {
44  static int run() { return 0; }
45 };
46 
47 
48 // default implementation of digits(), based on numeric_limits if specialized,
49 // 0 for integer types, and log2(epsilon()) otherwise.
50 template< typename T,
51  bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
52  bool is_integer = NumTraits<T>::IsInteger>
54 {
56  static int run() { return std::numeric_limits<T>::digits; }
57 };
58 
59 template<typename T>
60 struct default_digits_impl<T,false,false> // Floating point
61 {
63  static int run() {
64  using std::log;
65  using std::ceil;
66  typedef typename NumTraits<T>::Real Real;
67  return int(ceil(-log(NumTraits<Real>::epsilon())/log(static_cast<Real>(2))));
68  }
69 };
70 
71 template<typename T>
72 struct default_digits_impl<T,false,true> // Integer
73 {
75  static int run() { return 0; }
76 };
77 
78 } // end namespace internal
79 
80 namespace numext {
83 // TODO: Replace by std::bit_cast (available in C++20)
84 template <typename Tgt, typename Src>
86 #if EIGEN_HAS_TYPE_TRAITS
87  // The behaviour of memcpy is not specified for non-trivially copyable types
90  THIS_TYPE_IS_NOT_SUPPORTED);
91 #endif
92 
93  EIGEN_STATIC_ASSERT(sizeof(Src) == sizeof(Tgt), THIS_TYPE_IS_NOT_SUPPORTED);
94  Tgt tgt;
95  EIGEN_USING_STD(memcpy)
96  memcpy(&tgt, &src, sizeof(Tgt));
97  return tgt;
98 }
99 } // namespace numext
100 
152 template<typename T> struct GenericNumTraits
153 {
154  enum {
155  IsInteger = std::numeric_limits<T>::is_integer,
156  IsSigned = std::numeric_limits<T>::is_signed,
159  ReadCost = 1,
160  AddCost = 1,
162  };
163 
164  typedef T Real;
165  typedef typename internal::conditional<
166  IsInteger,
167  typename internal::conditional<sizeof(T)<=2, float, double>::type,
168  T
169  >::type NonInteger;
170  typedef T Nested;
171  typedef T Literal;
172 
174  static inline Real epsilon()
175  {
177  }
178 
180  static inline int digits10()
181  {
183  }
184 
186  static inline int digits()
187  {
189  }
190 
192  static inline int min_exponent()
193  {
194  return numext::numeric_limits<T>::min_exponent;
195  }
196 
198  static inline int max_exponent()
199  {
200  return numext::numeric_limits<T>::max_exponent;
201  }
202 
204  static inline Real dummy_precision()
205  {
206  // make sure to override this for floating-point types
207  return Real(0);
208  }
209 
211  static inline T highest() {
213  }
214 
216  static inline T lowest() {
218  : static_cast<T>(-(numext::numeric_limits<T>::max)());
219  }
220 
222  static inline T infinity() {
223  return numext::numeric_limits<T>::infinity();
224  }
225 
227  static inline T quiet_NaN() {
228  return numext::numeric_limits<T>::quiet_NaN();
229  }
230 };
231 
232 template<typename T> struct NumTraits : GenericNumTraits<T>
233 {};
234 
235 template<> struct NumTraits<float>
236  : GenericNumTraits<float>
237 {
239  static inline float dummy_precision() { return 1e-5f; }
240 };
241 
242 template<> struct NumTraits<double> : GenericNumTraits<double>
243 {
245  static inline double dummy_precision() { return 1e-12; }
246 };
247 
248 template<> struct NumTraits<long double>
249  : GenericNumTraits<long double>
250 {
252  static inline long double dummy_precision() { return 1e-15l; }
253 };
254 
255 template<typename _Real> struct NumTraits<std::complex<_Real> >
256  : GenericNumTraits<std::complex<_Real> >
257 {
258  typedef _Real Real;
260  enum {
266  };
267 
269  static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
273  static inline int digits10() { return NumTraits<Real>::digits10(); }
274 };
275 
276 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
277 struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
278 {
284  typedef ArrayType & Nested;
286 
287  enum {
292  ReadCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::ReadCost),
293  AddCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::AddCost),
294  MulCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::MulCost)
295  };
296 
298  static inline RealScalar epsilon() { return NumTraits<RealScalar>::epsilon(); }
301 
303  static inline int digits10() { return NumTraits<Scalar>::digits10(); }
304 };
305 
306 template<> struct NumTraits<std::string>
307  : GenericNumTraits<std::string>
308 {
309  enum {
314  };
315 
317  static inline int digits10() { return 0; }
318 
319 private:
320  static inline std::string epsilon();
321  static inline std::string dummy_precision();
322  static inline std::string lowest();
323  static inline std::string highest();
324  static inline std::string infinity();
325  static inline std::string quiet_NaN();
326 };
327 
328 // Empty specialization for void to allow template specialization based on NumTraits<T>::Real with T==void and SFINAE.
329 template<> struct NumTraits<void> {};
330 
331 template<> struct NumTraits<bool> : GenericNumTraits<bool> {};
332 
333 } // end namespace Eigen
334 
335 #endif // EIGEN_NUMTRAITS_H
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
Eigen::NumTraits< Array< Scalar, Rows, Cols, Options, MaxRows, MaxCols > >::Real
Array< RealScalar, Rows, Cols, Options, MaxRows, MaxCols > Real
Definition: NumTraits.h:281
Eigen::HugeCost
const int HugeCost
Definition: Constants.h:44
EIGEN_DEVICE_FUNC
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
Eigen::NumTraits< double >::dummy_precision
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR double dummy_precision()
Definition: NumTraits.h:245
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::max
CleanedUpDerType< DerType >::type() max(const AutoDiffScalar< DerType > &x, const T &y)
Definition: AutoDiffScalar.h:585
EIGEN_USING_STD
#define EIGEN_USING_STD(FUNC)
Definition: Macros.h:1185
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Eigen::NumTraits< std::complex< _Real > >::dummy_precision
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR Real dummy_precision()
Definition: NumTraits.h:271
Eigen::internal::default_digits10_impl< T, false, false >::run
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR int run()
Definition: NumTraits.h:32
EIGEN_CONSTEXPR
#define EIGEN_CONSTEXPR
Definition: Macros.h:787
Eigen::internal::default_digits_impl::run
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR int run()
Definition: NumTraits.h:56
Eigen::Array
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:45
type
Definition: pytypes.h:1525
Eigen::GenericNumTraits::IsSigned
@ IsSigned
Definition: NumTraits.h:156
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
log
const EIGEN_DEVICE_FUNC LogReturnType log() const
Definition: ArrayCwiseUnaryOps.h:128
Eigen::NumTraits< Array< Scalar, Rows, Cols, Options, MaxRows, MaxCols > >::epsilon
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR RealScalar epsilon()
Definition: NumTraits.h:298
Eigen::NumTraits< Array< Scalar, Rows, Cols, Options, MaxRows, MaxCols > >::ArrayType
Array< Scalar, Rows, Cols, Options, MaxRows, MaxCols > ArrayType
Definition: NumTraits.h:279
Eigen::GenericNumTraits
Definition: NumTraits.h:152
epsilon
static double epsilon
Definition: testRot3.cpp:37
Eigen::NumTraits< Array< Scalar, Rows, Cols, Options, MaxRows, MaxCols > >::Literal
NumTraits< Scalar >::Literal Literal
Definition: NumTraits.h:285
Eigen::numext::bit_cast
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Tgt bit_cast(const Src &src)
Definition: NumTraits.h:85
Eigen::internal::default_digits10_impl< T, false, true >::run
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR int run()
Definition: NumTraits.h:44
Eigen::GenericNumTraits::MulCost
@ MulCost
Definition: NumTraits.h:161
Eigen::NumTraits< Array< Scalar, Rows, Cols, Options, MaxRows, MaxCols > >::NonInteger
Array< NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols > NonInteger
Definition: NumTraits.h:283
Eigen::NumTraits< Array< Scalar, Rows, Cols, Options, MaxRows, MaxCols > >::RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: NumTraits.h:280
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
l
static const Line3 l(Rot3(), 1, 1)
Eigen::GenericNumTraits::ReadCost
@ ReadCost
Definition: NumTraits.h:159
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
Eigen::NumTraits< long double >::dummy_precision
static EIGEN_CONSTEXPR long double dummy_precision()
Definition: NumTraits.h:252
Eigen::internal::default_digits_impl< T, false, true >::run
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR int run()
Definition: NumTraits.h:75
Eigen::NumTraits< Array< Scalar, Rows, Cols, Options, MaxRows, MaxCols > >::Nested
ArrayType & Nested
Definition: NumTraits.h:284
Eigen::internal::default_digits10_impl
Definition: NumTraits.h:22
Eigen::Triplet< double >
Eigen::internal::default_digits_impl
Definition: NumTraits.h:53
Eigen::NumTraits< std::complex< _Real > >::Literal
NumTraits< _Real >::Literal Literal
Definition: NumTraits.h:259
Eigen::GenericNumTraits::IsInteger
@ IsInteger
Definition: NumTraits.h:155
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
Eigen::GenericNumTraits::RequireInitialization
@ RequireInitialization
Definition: NumTraits.h:158
Eigen::internal::default_digits_impl< T, false, false >::run
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR int run()
Definition: NumTraits.h:63
EIGEN_STATIC_ASSERT
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:127
Eigen::GenericNumTraits::AddCost
@ AddCost
Definition: NumTraits.h:160
std
Definition: BFloat16.h:88
Eigen::internal::conditional
Definition: Meta.h:109
Real
mp::number< mp::cpp_dec_float< 100 >, mp::et_on > Real
Definition: boostmultiprec.cpp:78
min
#define min(a, b)
Definition: datatypes.h:19
Eigen::NumTraits< std::complex< _Real > >::digits10
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR int digits10()
Definition: NumTraits.h:273
ceil
const EIGEN_DEVICE_FUNC CeilReturnType ceil() const
Definition: ArrayCwiseUnaryOps.h:495
gtsam.examples.DogLegOptimizerExample.float
float
Definition: DogLegOptimizerExample.py:113
internal
Definition: BandTriangularSolver.h:13
Eigen::NumTraits< Array< Scalar, Rows, Cols, Options, MaxRows, MaxCols > >::NonIntegerScalar
NumTraits< Scalar >::NonInteger NonIntegerScalar
Definition: NumTraits.h:282
Eigen::NumTraits< Array< Scalar, Rows, Cols, Options, MaxRows, MaxCols > >::dummy_precision
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR RealScalar dummy_precision()
Definition: NumTraits.h:300
Eigen::GenericNumTraits::IsComplex
@ IsComplex
Definition: NumTraits.h:157
Eigen::NumTraits< Array< Scalar, Rows, Cols, Options, MaxRows, MaxCols > >::digits10
static EIGEN_CONSTEXPR int digits10()
Definition: NumTraits.h:303
Eigen::internal::default_digits10_impl::run
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR int run()
Definition: NumTraits.h:25
max
#define max(a, b)
Definition: datatypes.h:20
Eigen::NumTraits< std::string >::digits10
static EIGEN_CONSTEXPR int digits10()
Definition: NumTraits.h:317
Eigen::NumTraits< float >::dummy_precision
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR float dummy_precision()
Definition: NumTraits.h:239
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
test_callbacks.value
value
Definition: test_callbacks.py:160
log10
const EIGEN_DEVICE_FUNC Log10ReturnType log10() const
Definition: ArrayCwiseUnaryOps.h:158
Eigen::GenericNumTraits::Real
T Real
Definition: NumTraits.h:164
Eigen::NumTraits< std::complex< _Real > >::Real
_Real Real
Definition: NumTraits.h:258
complex
Definition: datatypes.h:12
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::NumTraits< std::complex< _Real > >::epsilon
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR Real epsilon()
Definition: NumTraits.h:269
Eigen::internal::is_arithmetic
Definition: Meta.h:133
Cols
static const int Cols
Definition: testCallRecord.cpp:32


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:33:52