2007-10-12 13:15:25 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-23 02:25:33 +08:00
|
|
|
// for linear algebra.
|
2007-10-07 20:44:42 +08:00
|
|
|
//
|
2010-04-29 06:51:38 +08:00
|
|
|
// Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2007-10-07 20:44:42 +08:00
|
|
|
//
|
2012-07-14 02:42:47 +08:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2007-10-07 20:44:42 +08:00
|
|
|
|
2007-12-03 02:32:59 +08:00
|
|
|
#ifndef EIGEN_NUMTRAITS_H
|
|
|
|
|
#define EIGEN_NUMTRAITS_H
|
2007-10-07 20:44:42 +08:00
|
|
|
|
2012-04-15 18:06:28 +08:00
|
|
|
namespace Eigen {
|
|
|
|
|
|
2008-01-07 17:34:21 +08:00
|
|
|
/** \class NumTraits
|
2010-07-06 20:10:08 +08:00
|
|
|
* \ingroup Core_Module
|
2008-01-07 17:34:21 +08:00
|
|
|
*
|
2010-04-29 06:51:38 +08:00
|
|
|
* \brief Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
|
2008-01-07 17:34:21 +08:00
|
|
|
*
|
2010-04-29 06:51:38 +08:00
|
|
|
* \param T the numeric type at hand
|
2008-01-07 17:34:21 +08:00
|
|
|
*
|
2010-04-29 06:51:38 +08:00
|
|
|
* This class stores enums, typedefs and static methods giving information about a numeric type.
|
|
|
|
|
*
|
|
|
|
|
* The provided data consists of:
|
2008-01-07 17:34:21 +08:00
|
|
|
* \li A typedef \a Real, giving the "real part" type of \a T. If \a T is already real,
|
|
|
|
|
* then \a Real is just a typedef to \a T. If \a T is \c std::complex<U> then \a Real
|
|
|
|
|
* is a typedef to \a U.
|
2010-04-29 06:51:38 +08:00
|
|
|
* \li A typedef \a NonInteger, giving the type that should be used for operations producing non-integral values,
|
|
|
|
|
* such as quotients, square roots, etc. If \a T is a floating-point type, then this typedef just gives
|
2010-10-25 22:15:22 +08:00
|
|
|
* \a T again. Note however that many Eigen functions such as internal::sqrt simply refuse to
|
2010-04-29 06:51:38 +08:00
|
|
|
* take integers. Outside of a few cases, Eigen doesn't do automatic type promotion. Thus, this typedef is
|
|
|
|
|
* only intended as a helper for code that needs to explicitly promote types.
|
|
|
|
|
* \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
|
|
|
|
|
* this means, just use \a T here.
|
2008-01-11 04:45:35 +08:00
|
|
|
* \li An enum value \a IsComplex. It is equal to 1 if \a T is a \c std::complex
|
|
|
|
|
* type, and to 0 otherwise.
|
2010-04-29 06:51:38 +08:00
|
|
|
* \li An enum value \a IsInteger. It is equal to \c 1 if \a T is an integer type such as \c int,
|
|
|
|
|
* and to \c 0 otherwise.
|
|
|
|
|
* \li Enum values ReadCost, AddCost and MulCost representing a rough estimate of the number of CPU cycles needed
|
|
|
|
|
* to by move / add / mul instructions respectively, assuming the data is already stored in CPU registers.
|
|
|
|
|
* Stay vague here. No need to do architecture-specific stuff.
|
|
|
|
|
* \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.
|
2011-01-27 00:56:49 +08:00
|
|
|
* \li An enum value \a RequireInitialization. It is equal to \c 1 if the constructor of the numeric type \a T must
|
|
|
|
|
* 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.
|
2010-02-10 18:11:21 +08:00
|
|
|
* \li An epsilon() function which, unlike std::numeric_limits::epsilon(), returns a \a Real instead of a \a T.
|
2010-04-29 06:51:38 +08:00
|
|
|
* \li A dummy_precision() function returning a weak epsilon value. It is mainly used as a default
|
|
|
|
|
* value by the fuzzy comparison operators.
|
|
|
|
|
* \li highest() and lowest() functions returning the highest and lowest possible values respectively.
|
2008-01-07 17:34:21 +08:00
|
|
|
*/
|
2010-02-10 18:11:21 +08:00
|
|
|
|
2010-04-29 06:51:38 +08:00
|
|
|
template<typename T> struct GenericNumTraits
|
2007-10-07 20:44:42 +08:00
|
|
|
{
|
2008-01-11 04:45:35 +08:00
|
|
|
enum {
|
2010-04-29 06:51:38 +08:00
|
|
|
IsInteger = std::numeric_limits<T>::is_integer,
|
|
|
|
|
IsSigned = std::numeric_limits<T>::is_signed,
|
2008-01-11 04:45:35 +08:00
|
|
|
IsComplex = 0,
|
2011-01-27 00:56:49 +08:00
|
|
|
RequireInitialization = internal::is_arithmetic<T>::value ? 0 : 1,
|
2008-04-03 19:10:17 +08:00
|
|
|
ReadCost = 1,
|
|
|
|
|
AddCost = 1,
|
2008-04-10 18:33:50 +08:00
|
|
|
MulCost = 1
|
2008-01-11 04:45:35 +08:00
|
|
|
};
|
2010-04-29 06:51:38 +08:00
|
|
|
|
|
|
|
|
typedef T Real;
|
2010-10-26 04:13:49 +08:00
|
|
|
typedef typename internal::conditional<
|
2010-04-29 06:51:38 +08:00
|
|
|
IsInteger,
|
2010-10-26 04:13:49 +08:00
|
|
|
typename internal::conditional<sizeof(T)<=2, float, double>::type,
|
2010-04-29 06:51:38 +08:00
|
|
|
T
|
2010-10-26 04:13:49 +08:00
|
|
|
>::type NonInteger;
|
2010-04-29 06:51:38 +08:00
|
|
|
typedef T Nested;
|
|
|
|
|
|
2013-08-01 22:26:57 +08:00
|
|
|
EIGEN_DEVICE_FUNC
|
|
|
|
|
static inline Real epsilon()
|
|
|
|
|
{
|
|
|
|
|
#if defined(__CUDA_ARCH__)
|
|
|
|
|
return internal::device::numeric_limits<T>::epsilon();
|
|
|
|
|
#else
|
|
|
|
|
return std::numeric_limits<T>::epsilon();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2013-06-05 21:38:33 +08:00
|
|
|
EIGEN_DEVICE_FUNC
|
2012-01-31 19:58:52 +08:00
|
|
|
static inline Real dummy_precision()
|
2010-04-29 06:51:38 +08:00
|
|
|
{
|
|
|
|
|
// make sure to override this for floating-point types
|
|
|
|
|
return Real(0);
|
|
|
|
|
}
|
2015-08-22 07:01:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
EIGEN_DEVICE_FUNC
|
|
|
|
|
static inline T highest() {
|
|
|
|
|
#if defined(__CUDA_ARCH__)
|
2015-08-22 21:03:16 +08:00
|
|
|
return (internal::device::numeric_limits<T>::max)();
|
2015-08-22 07:01:40 +08:00
|
|
|
#else
|
|
|
|
|
return (std::numeric_limits<T>::max)();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EIGEN_DEVICE_FUNC
|
|
|
|
|
static inline T lowest() {
|
|
|
|
|
#if defined(__CUDA_ARCH__)
|
|
|
|
|
return IsInteger ? (internal::device::numeric_limits<T>::min)() : (-(internal::device::numeric_limits<T>::max)());
|
|
|
|
|
#else
|
|
|
|
|
return IsInteger ? (std::numeric_limits<T>::min)() : (-(std::numeric_limits<T>::max)());
|
|
|
|
|
#endif
|
2015-08-22 21:03:16 +08:00
|
|
|
}
|
2007-10-07 20:44:42 +08:00
|
|
|
};
|
|
|
|
|
|
2010-04-29 06:51:38 +08:00
|
|
|
template<typename T> struct NumTraits : GenericNumTraits<T>
|
|
|
|
|
{};
|
|
|
|
|
|
2007-10-12 04:14:01 +08:00
|
|
|
template<> struct NumTraits<float>
|
2010-04-29 06:51:38 +08:00
|
|
|
: GenericNumTraits<float>
|
2007-10-07 20:44:42 +08:00
|
|
|
{
|
2013-06-05 21:38:33 +08:00
|
|
|
EIGEN_DEVICE_FUNC
|
2012-01-31 19:58:52 +08:00
|
|
|
static inline float dummy_precision() { return 1e-5f; }
|
2007-10-07 20:44:42 +08:00
|
|
|
};
|
|
|
|
|
|
2010-04-29 06:51:38 +08:00
|
|
|
template<> struct NumTraits<double> : GenericNumTraits<double>
|
2007-10-07 20:44:42 +08:00
|
|
|
{
|
2013-06-05 21:38:33 +08:00
|
|
|
EIGEN_DEVICE_FUNC
|
2012-01-31 19:58:52 +08:00
|
|
|
static inline double dummy_precision() { return 1e-12; }
|
2007-10-07 20:44:42 +08:00
|
|
|
};
|
|
|
|
|
|
2010-04-29 06:51:38 +08:00
|
|
|
template<> struct NumTraits<long double>
|
|
|
|
|
: GenericNumTraits<long double>
|
|
|
|
|
{
|
|
|
|
|
static inline long double dummy_precision() { return 1e-15l; }
|
|
|
|
|
};
|
|
|
|
|
|
2007-10-12 04:14:01 +08:00
|
|
|
template<typename _Real> struct NumTraits<std::complex<_Real> >
|
2010-04-29 06:51:38 +08:00
|
|
|
: GenericNumTraits<std::complex<_Real> >
|
2007-10-07 20:44:42 +08:00
|
|
|
{
|
|
|
|
|
typedef _Real Real;
|
2008-01-11 04:45:35 +08:00
|
|
|
enum {
|
|
|
|
|
IsComplex = 1,
|
2011-01-27 00:56:49 +08:00
|
|
|
RequireInitialization = NumTraits<_Real>::RequireInitialization,
|
2010-05-26 08:33:28 +08:00
|
|
|
ReadCost = 2 * NumTraits<_Real>::ReadCost,
|
2008-04-03 19:10:17 +08:00
|
|
|
AddCost = 2 * NumTraits<Real>::AddCost,
|
2008-04-10 18:33:50 +08:00
|
|
|
MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
|
2008-01-11 04:45:35 +08:00
|
|
|
};
|
2010-02-10 17:52:28 +08:00
|
|
|
|
2012-01-31 19:58:52 +08:00
|
|
|
static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
|
|
|
|
|
static inline Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
|
2007-10-07 20:44:42 +08:00
|
|
|
};
|
|
|
|
|
|
2010-04-29 06:51:38 +08:00
|
|
|
template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
|
|
|
|
|
struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
|
2008-02-29 21:20:44 +08:00
|
|
|
{
|
2010-04-29 06:51:38 +08:00
|
|
|
typedef Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> ArrayType;
|
|
|
|
|
typedef typename NumTraits<Scalar>::Real RealScalar;
|
|
|
|
|
typedef Array<RealScalar, Rows, Cols, Options, MaxRows, MaxCols> Real;
|
|
|
|
|
typedef typename NumTraits<Scalar>::NonInteger NonIntegerScalar;
|
|
|
|
|
typedef Array<NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols> NonInteger;
|
|
|
|
|
typedef ArrayType & Nested;
|
|
|
|
|
|
2008-02-29 21:20:44 +08:00
|
|
|
enum {
|
2010-04-29 06:51:38 +08:00
|
|
|
IsComplex = NumTraits<Scalar>::IsComplex,
|
|
|
|
|
IsInteger = NumTraits<Scalar>::IsInteger,
|
|
|
|
|
IsSigned = NumTraits<Scalar>::IsSigned,
|
2011-01-27 00:56:49 +08:00
|
|
|
RequireInitialization = 1,
|
2010-04-29 06:51:38 +08:00
|
|
|
ReadCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::ReadCost,
|
|
|
|
|
AddCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::AddCost,
|
|
|
|
|
MulCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::MulCost
|
2008-02-29 21:20:44 +08:00
|
|
|
};
|
2013-04-09 15:31:26 +08:00
|
|
|
|
|
|
|
|
static inline RealScalar epsilon() { return NumTraits<RealScalar>::epsilon(); }
|
|
|
|
|
static inline RealScalar dummy_precision() { return NumTraits<RealScalar>::dummy_precision(); }
|
2008-02-29 21:20:44 +08:00
|
|
|
};
|
|
|
|
|
|
2012-04-15 18:06:28 +08:00
|
|
|
} // end namespace Eigen
|
2008-04-04 02:13:27 +08:00
|
|
|
|
2007-12-03 02:32:59 +08:00
|
|
|
#endif // EIGEN_NUMTRAITS_H
|