Eigen  3.2.91
NumTraits.h
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 
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  EIGEN_DEVICE_FUNC
72  static inline Real epsilon()
73  {
74  #if defined(__CUDA_ARCH__)
75  return internal::device::numeric_limits<T>::epsilon();
76  #else
77  return std::numeric_limits<T>::epsilon();
78  #endif
79  }
80  EIGEN_DEVICE_FUNC
81  static inline Real dummy_precision()
82  {
83  // make sure to override this for floating-point types
84  return Real(0);
85  }
86 
87 
88  EIGEN_DEVICE_FUNC
89  static inline T highest() {
90 #if defined(__CUDA_ARCH__)
91  return (internal::device::numeric_limits<T>::max)();
92 #else
93  return (std::numeric_limits<T>::max)();
94 #endif
95  }
96 
97  EIGEN_DEVICE_FUNC
98  static inline T lowest() {
99 #if defined(__CUDA_ARCH__)
100  return IsInteger ? (internal::device::numeric_limits<T>::min)() : (-(internal::device::numeric_limits<T>::max)());
101 #else
102  return IsInteger ? (std::numeric_limits<T>::min)() : (-(std::numeric_limits<T>::max)());
103 #endif
104  }
105 };
106 
107 template<typename T> struct NumTraits : GenericNumTraits<T>
108 {};
109 
110 template<> struct NumTraits<float>
111  : GenericNumTraits<float>
112 {
113  EIGEN_DEVICE_FUNC
114  static inline float dummy_precision() { return 1e-5f; }
115 };
116 
117 template<> struct NumTraits<double> : GenericNumTraits<double>
118 {
119  EIGEN_DEVICE_FUNC
120  static inline double dummy_precision() { return 1e-12; }
121 };
122 
123 template<> struct NumTraits<long double>
124  : GenericNumTraits<long double>
125 {
126  static inline long double dummy_precision() { return 1e-15l; }
127 };
128 
129 template<typename _Real> struct NumTraits<std::complex<_Real> >
130  : GenericNumTraits<std::complex<_Real> >
131 {
132  typedef _Real Real;
133  enum {
134  IsComplex = 1,
135  RequireInitialization = NumTraits<_Real>::RequireInitialization,
136  ReadCost = 2 * NumTraits<_Real>::ReadCost,
137  AddCost = 2 * NumTraits<Real>::AddCost,
138  MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
139  };
140 
141  static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
142  static inline Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
143 };
144 
145 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
146 struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
147 {
148  typedef Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> ArrayType;
149  typedef typename NumTraits<Scalar>::Real RealScalar;
150  typedef Array<RealScalar, Rows, Cols, Options, MaxRows, MaxCols> Real;
151  typedef typename NumTraits<Scalar>::NonInteger NonIntegerScalar;
152  typedef Array<NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols> NonInteger;
153  typedef ArrayType & Nested;
154 
155  enum {
156  IsComplex = NumTraits<Scalar>::IsComplex,
157  IsInteger = NumTraits<Scalar>::IsInteger,
158  IsSigned = NumTraits<Scalar>::IsSigned,
159  RequireInitialization = 1,
160  ReadCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::ReadCost,
161  AddCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::AddCost,
162  MulCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::MulCost
163  };
164 
165  static inline RealScalar epsilon() { return NumTraits<RealScalar>::epsilon(); }
166  static inline RealScalar dummy_precision() { return NumTraits<RealScalar>::dummy_precision(); }
167 };
168 
169 } // end namespace Eigen
170 
171 #endif // EIGEN_NUMTRAITS_H
Definition: LDLT.h:16
Definition: StdDeque.h:58
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:107
Definition: Eigen_Colamd.h:54