Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:56:15

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_NUMTRAITS_H
0011 #define EIGEN_NUMTRAITS_H
0012 
0013 namespace Eigen {
0014 
0015 namespace internal {
0016 
0017 // default implementation of digits10(), based on numeric_limits if specialized,
0018 // 0 for integer types, and log10(epsilon()) otherwise.
0019 template< typename T,
0020           bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
0021           bool is_integer = NumTraits<T>::IsInteger>
0022 struct default_digits10_impl
0023 {
0024   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0025   static int run() { return std::numeric_limits<T>::digits10; }
0026 };
0027 
0028 template<typename T>
0029 struct default_digits10_impl<T,false,false> // Floating point
0030 {
0031   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0032   static int run() {
0033     using std::log10;
0034     using std::ceil;
0035     typedef typename NumTraits<T>::Real Real;
0036     return int(ceil(-log10(NumTraits<Real>::epsilon())));
0037   }
0038 };
0039 
0040 template<typename T>
0041 struct default_digits10_impl<T,false,true> // Integer
0042 {
0043   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0044   static int run() { return 0; }
0045 };
0046 
0047 
0048 // default implementation of digits(), based on numeric_limits if specialized,
0049 // 0 for integer types, and log2(epsilon()) otherwise.
0050 template< typename T,
0051           bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
0052           bool is_integer = NumTraits<T>::IsInteger>
0053 struct default_digits_impl
0054 {
0055   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0056   static int run() { return std::numeric_limits<T>::digits; }
0057 };
0058 
0059 template<typename T>
0060 struct default_digits_impl<T,false,false> // Floating point
0061 {
0062   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0063   static int run() {
0064     using std::log;
0065     using std::ceil;
0066     typedef typename NumTraits<T>::Real Real;
0067     return int(ceil(-log(NumTraits<Real>::epsilon())/log(static_cast<Real>(2))));
0068   }
0069 };
0070 
0071 template<typename T>
0072 struct default_digits_impl<T,false,true> // Integer
0073 {
0074   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0075   static int run() { return 0; }
0076 };
0077 
0078 } // end namespace internal
0079 
0080 namespace numext {
0081 /** \internal bit-wise cast without changing the underlying bit representation. */
0082 
0083 // TODO: Replace by std::bit_cast (available in C++20)
0084 template <typename Tgt, typename Src>
0085 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Tgt bit_cast(const Src& src) {
0086 #if EIGEN_HAS_TYPE_TRAITS
0087   // The behaviour of memcpy is not specified for non-trivially copyable types
0088   EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Src>::value, THIS_TYPE_IS_NOT_SUPPORTED);
0089   EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Tgt>::value && std::is_default_constructible<Tgt>::value,
0090                       THIS_TYPE_IS_NOT_SUPPORTED);
0091 #endif
0092 
0093   EIGEN_STATIC_ASSERT(sizeof(Src) == sizeof(Tgt), THIS_TYPE_IS_NOT_SUPPORTED);
0094   Tgt tgt;
0095   EIGEN_USING_STD(memcpy)
0096   memcpy(&tgt, &src, sizeof(Tgt));
0097   return tgt;
0098 }
0099 }  // namespace numext
0100 
0101 /** \class NumTraits
0102   * \ingroup Core_Module
0103   *
0104   * \brief Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
0105   *
0106   * \tparam T the numeric type at hand
0107   *
0108   * This class stores enums, typedefs and static methods giving information about a numeric type.
0109   *
0110   * The provided data consists of:
0111   * \li A typedef \c Real, giving the "real part" type of \a T. If \a T is already real,
0112   *     then \c Real is just a typedef to \a T. If \a T is \c std::complex<U> then \c Real
0113   *     is a typedef to \a U.
0114   * \li A typedef \c NonInteger, giving the type that should be used for operations producing non-integral values,
0115   *     such as quotients, square roots, etc. If \a T is a floating-point type, then this typedef just gives
0116   *     \a T again. Note however that many Eigen functions such as internal::sqrt simply refuse to
0117   *     take integers. Outside of a few cases, Eigen doesn't do automatic type promotion. Thus, this typedef is
0118   *     only intended as a helper for code that needs to explicitly promote types.
0119   * \li A typedef \c Literal giving the type to use for numeric literals such as "2" or "0.5". For instance, for \c std::complex<U>, Literal is defined as \c U.
0120   *     Of course, this type must be fully compatible with \a T. In doubt, just use \a T here.
0121   * \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
0122   *     this means, just use \a T here.
0123   * \li An enum value \a IsComplex. It is equal to 1 if \a T is a \c std::complex
0124   *     type, and to 0 otherwise.
0125   * \li An enum value \a IsInteger. It is equal to \c 1 if \a T is an integer type such as \c int,
0126   *     and to \c 0 otherwise.
0127   * \li Enum values ReadCost, AddCost and MulCost representing a rough estimate of the number of CPU cycles needed
0128   *     to by move / add / mul instructions respectively, assuming the data is already stored in CPU registers.
0129   *     Stay vague here. No need to do architecture-specific stuff. If you don't know what this means, just use \c Eigen::HugeCost.
0130   * \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.
0131   * \li An enum value \a RequireInitialization. It is equal to \c 1 if the constructor of the numeric type \a T must
0132   *     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.
0133   * \li An epsilon() function which, unlike <a href="http://en.cppreference.com/w/cpp/types/numeric_limits/epsilon">std::numeric_limits::epsilon()</a>,
0134   *     it returns a \a Real instead of a \a T.
0135   * \li A dummy_precision() function returning a weak epsilon value. It is mainly used as a default
0136   *     value by the fuzzy comparison operators.
0137   * \li highest() and lowest() functions returning the highest and lowest possible values respectively.
0138   * \li digits() function returning the number of radix digits (non-sign digits for integers, mantissa for floating-point). This is
0139   *     the analogue of <a href="http://en.cppreference.com/w/cpp/types/numeric_limits/digits">std::numeric_limits<T>::digits</a>
0140   *     which is used as the default implementation if specialized.
0141   * \li digits10() function returning the number of decimal digits that can be represented without change. This is
0142   *     the analogue of <a href="http://en.cppreference.com/w/cpp/types/numeric_limits/digits10">std::numeric_limits<T>::digits10</a>
0143   *     which is used as the default implementation if specialized.
0144   * \li min_exponent() and max_exponent() functions returning the highest and lowest possible values, respectively,
0145   *     such that the radix raised to the power exponent-1 is a normalized floating-point number.  These are equivalent to
0146   *     <a href="http://en.cppreference.com/w/cpp/types/numeric_limits/min_exponent">std::numeric_limits<T>::min_exponent</a>/
0147   *     <a href="http://en.cppreference.com/w/cpp/types/numeric_limits/max_exponent">std::numeric_limits<T>::max_exponent</a>.
0148   * \li infinity() function returning a representation of positive infinity, if available.
0149   * \li quiet_NaN function returning a non-signaling "not-a-number", if available.
0150   */
0151 
0152 template<typename T> struct GenericNumTraits
0153 {
0154   enum {
0155     IsInteger = std::numeric_limits<T>::is_integer,
0156     IsSigned = std::numeric_limits<T>::is_signed,
0157     IsComplex = 0,
0158     RequireInitialization = internal::is_arithmetic<T>::value ? 0 : 1,
0159     ReadCost = 1,
0160     AddCost = 1,
0161     MulCost = 1
0162   };
0163 
0164   typedef T Real;
0165   typedef typename internal::conditional<
0166                      IsInteger,
0167                      typename internal::conditional<sizeof(T)<=2, float, double>::type,
0168                      T
0169                    >::type NonInteger;
0170   typedef T Nested;
0171   typedef T Literal;
0172 
0173   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0174   static inline Real epsilon()
0175   {
0176     return numext::numeric_limits<T>::epsilon();
0177   }
0178 
0179   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0180   static inline int digits10()
0181   {
0182     return internal::default_digits10_impl<T>::run();
0183   }
0184 
0185   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0186   static inline int digits()
0187   {
0188     return internal::default_digits_impl<T>::run();
0189   }
0190 
0191   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0192   static inline int min_exponent()
0193   {
0194     return numext::numeric_limits<T>::min_exponent;
0195   }
0196 
0197   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0198   static inline int max_exponent()
0199   {
0200     return numext::numeric_limits<T>::max_exponent;
0201   }
0202 
0203   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0204   static inline Real dummy_precision()
0205   {
0206     // make sure to override this for floating-point types
0207     return Real(0);
0208   }
0209 
0210   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0211   static inline T highest() {
0212     return (numext::numeric_limits<T>::max)();
0213   }
0214 
0215   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0216   static inline T lowest()  {
0217     return IsInteger ? (numext::numeric_limits<T>::min)()
0218                      : static_cast<T>(-(numext::numeric_limits<T>::max)());
0219   }
0220 
0221   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0222   static inline T infinity() {
0223     return numext::numeric_limits<T>::infinity();
0224   }
0225 
0226   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0227   static inline T quiet_NaN() {
0228     return numext::numeric_limits<T>::quiet_NaN();
0229   }
0230 };
0231 
0232 template<typename T> struct NumTraits : GenericNumTraits<T>
0233 {};
0234 
0235 template<> struct NumTraits<float>
0236   : GenericNumTraits<float>
0237 {
0238   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0239   static inline float dummy_precision() { return 1e-5f; }
0240 };
0241 
0242 template<> struct NumTraits<double> : GenericNumTraits<double>
0243 {
0244   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0245   static inline double dummy_precision() { return 1e-12; }
0246 };
0247 
0248 template<> struct NumTraits<long double>
0249   : GenericNumTraits<long double>
0250 {
0251   EIGEN_CONSTEXPR
0252   static inline long double dummy_precision() { return 1e-15l; }
0253 };
0254 
0255 template<typename _Real> struct NumTraits<std::complex<_Real> >
0256   : GenericNumTraits<std::complex<_Real> >
0257 {
0258   typedef _Real Real;
0259   typedef typename NumTraits<_Real>::Literal Literal;
0260   enum {
0261     IsComplex = 1,
0262     RequireInitialization = NumTraits<_Real>::RequireInitialization,
0263     ReadCost = 2 * NumTraits<_Real>::ReadCost,
0264     AddCost = 2 * NumTraits<Real>::AddCost,
0265     MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
0266   };
0267 
0268   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0269   static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
0270   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0271   static inline Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
0272   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0273   static inline int digits10() { return NumTraits<Real>::digits10(); }
0274 };
0275 
0276 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
0277 struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
0278 {
0279   typedef Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> ArrayType;
0280   typedef typename NumTraits<Scalar>::Real RealScalar;
0281   typedef Array<RealScalar, Rows, Cols, Options, MaxRows, MaxCols> Real;
0282   typedef typename NumTraits<Scalar>::NonInteger NonIntegerScalar;
0283   typedef Array<NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols> NonInteger;
0284   typedef ArrayType & Nested;
0285   typedef typename NumTraits<Scalar>::Literal Literal;
0286 
0287   enum {
0288     IsComplex = NumTraits<Scalar>::IsComplex,
0289     IsInteger = NumTraits<Scalar>::IsInteger,
0290     IsSigned  = NumTraits<Scalar>::IsSigned,
0291     RequireInitialization = 1,
0292     ReadCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::ReadCost),
0293     AddCost  = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::AddCost),
0294     MulCost  = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::MulCost)
0295   };
0296 
0297   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0298   static inline RealScalar epsilon() { return NumTraits<RealScalar>::epsilon(); }
0299   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0300   static inline RealScalar dummy_precision() { return NumTraits<RealScalar>::dummy_precision(); }
0301 
0302   EIGEN_CONSTEXPR
0303   static inline int digits10() { return NumTraits<Scalar>::digits10(); }
0304 };
0305 
0306 template<> struct NumTraits<std::string>
0307   : GenericNumTraits<std::string>
0308 {
0309   enum {
0310     RequireInitialization = 1,
0311     ReadCost = HugeCost,
0312     AddCost  = HugeCost,
0313     MulCost  = HugeCost
0314   };
0315 
0316   EIGEN_CONSTEXPR
0317   static inline int digits10() { return 0; }
0318 
0319 private:
0320   static inline std::string epsilon();
0321   static inline std::string dummy_precision();
0322   static inline std::string lowest();
0323   static inline std::string highest();
0324   static inline std::string infinity();
0325   static inline std::string quiet_NaN();
0326 };
0327 
0328 // Empty specialization for void to allow template specialization based on NumTraits<T>::Real with T==void and SFINAE.
0329 template<> struct NumTraits<void> {};
0330 
0331 template<> struct NumTraits<bool> : GenericNumTraits<bool> {};
0332 
0333 } // end namespace Eigen
0334 
0335 #endif // EIGEN_NUMTRAITS_H