mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
* move dummy_precision and epsilon to NumTraits
* make NumTraits inherits std::numeric_limits
This commit is contained in:
@@ -381,17 +381,17 @@ template<typename Derived> class DenseBase
|
||||
|
||||
template<typename OtherDerived>
|
||||
bool isApprox(const DenseBase<OtherDerived>& other,
|
||||
RealScalar prec = dummy_precision<Scalar>()) const;
|
||||
RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||
bool isMuchSmallerThan(const RealScalar& other,
|
||||
RealScalar prec = dummy_precision<Scalar>()) const;
|
||||
RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||
template<typename OtherDerived>
|
||||
bool isMuchSmallerThan(const DenseBase<OtherDerived>& other,
|
||||
RealScalar prec = dummy_precision<Scalar>()) const;
|
||||
RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||
|
||||
bool isApproxToConstant(const Scalar& value, RealScalar prec = dummy_precision<Scalar>()) const;
|
||||
bool isConstant(const Scalar& value, RealScalar prec = dummy_precision<Scalar>()) const;
|
||||
bool isZero(RealScalar prec = dummy_precision<Scalar>()) const;
|
||||
bool isOnes(RealScalar prec = dummy_precision<Scalar>()) const;
|
||||
bool isApproxToConstant(const Scalar& value, RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||
bool isConstant(const Scalar& value, RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||
bool isZero(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||
bool isOnes(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||
|
||||
inline Derived& operator*=(const Scalar& other);
|
||||
inline Derived& operator/=(const Scalar& other);
|
||||
|
||||
@@ -146,7 +146,7 @@ std::ostream & ei_print_matrix(std::ostream & s, const Derived& _m, const IOForm
|
||||
if (NumTraits<Scalar>::HasFloatingPoint)
|
||||
{
|
||||
typedef typename NumTraits<Scalar>::Real RealScalar;
|
||||
RealScalar explicit_precision_fp = std::ceil(-ei_log(epsilon<Scalar>())/ei_log(10.0));
|
||||
RealScalar explicit_precision_fp = std::ceil(-ei_log(NumTraits<Scalar>::epsilon())/ei_log(10.0));
|
||||
explicit_precision = static_cast<std::streamsize>(explicit_precision_fp);
|
||||
}
|
||||
else
|
||||
|
||||
@@ -25,13 +25,6 @@
|
||||
#ifndef EIGEN_MATHFUNCTIONS_H
|
||||
#define EIGEN_MATHFUNCTIONS_H
|
||||
|
||||
template<typename T> inline typename NumTraits<T>::Real epsilon()
|
||||
{
|
||||
return std::numeric_limits<typename NumTraits<T>::Real>::epsilon();
|
||||
}
|
||||
|
||||
template<typename T> inline typename NumTraits<T>::Real dummy_precision();
|
||||
|
||||
template<typename T> inline T ei_random(T a, T b);
|
||||
template<typename T> inline T ei_random();
|
||||
template<typename T> inline T ei_random_amplitude()
|
||||
@@ -55,7 +48,6 @@ template<typename T> inline typename NumTraits<T>::Real ei_hypot(T x, T y)
|
||||
*** int ***
|
||||
**************/
|
||||
|
||||
template<> inline int dummy_precision<int>() { return 0; }
|
||||
inline int ei_real(int x) { return x; }
|
||||
inline int& ei_real_ref(int& x) { return x; }
|
||||
inline int ei_imag(int) { return 0; }
|
||||
@@ -92,15 +84,15 @@ template<> inline int ei_random()
|
||||
{
|
||||
return ei_random<int>(-ei_random_amplitude<int>(), ei_random_amplitude<int>());
|
||||
}
|
||||
inline bool ei_isMuchSmallerThan(int a, int, int = dummy_precision<int>())
|
||||
inline bool ei_isMuchSmallerThan(int a, int, int = NumTraits<int>::dummy_precision())
|
||||
{
|
||||
return a == 0;
|
||||
}
|
||||
inline bool ei_isApprox(int a, int b, int = dummy_precision<int>())
|
||||
inline bool ei_isApprox(int a, int b, int = NumTraits<int>::dummy_precision())
|
||||
{
|
||||
return a == b;
|
||||
}
|
||||
inline bool ei_isApproxOrLessThan(int a, int b, int = dummy_precision<int>())
|
||||
inline bool ei_isApproxOrLessThan(int a, int b, int = NumTraits<int>::dummy_precision())
|
||||
{
|
||||
return a <= b;
|
||||
}
|
||||
@@ -109,7 +101,6 @@ inline bool ei_isApproxOrLessThan(int a, int b, int = dummy_precision<int>())
|
||||
*** float ***
|
||||
**************/
|
||||
|
||||
template<> inline float dummy_precision<float>() { return 1e-5f; }
|
||||
inline float ei_real(float x) { return x; }
|
||||
inline float& ei_real_ref(float& x) { return x; }
|
||||
inline float ei_imag(float) { return 0.f; }
|
||||
@@ -140,15 +131,15 @@ template<> inline float ei_random()
|
||||
{
|
||||
return ei_random<float>(-ei_random_amplitude<float>(), ei_random_amplitude<float>());
|
||||
}
|
||||
inline bool ei_isMuchSmallerThan(float a, float b, float prec = dummy_precision<float>())
|
||||
inline bool ei_isMuchSmallerThan(float a, float b, float prec = NumTraits<float>::dummy_precision())
|
||||
{
|
||||
return ei_abs(a) <= ei_abs(b) * prec;
|
||||
}
|
||||
inline bool ei_isApprox(float a, float b, float prec = dummy_precision<float>())
|
||||
inline bool ei_isApprox(float a, float b, float prec = NumTraits<float>::dummy_precision())
|
||||
{
|
||||
return ei_abs(a - b) <= std::min(ei_abs(a), ei_abs(b)) * prec;
|
||||
}
|
||||
inline bool ei_isApproxOrLessThan(float a, float b, float prec = dummy_precision<float>())
|
||||
inline bool ei_isApproxOrLessThan(float a, float b, float prec = NumTraits<float>::dummy_precision())
|
||||
{
|
||||
return a <= b || ei_isApprox(a, b, prec);
|
||||
}
|
||||
@@ -157,8 +148,6 @@ inline bool ei_isApproxOrLessThan(float a, float b, float prec = dummy_precision
|
||||
*** double ***
|
||||
**************/
|
||||
|
||||
template<> inline double dummy_precision<double>() { return 1e-12; }
|
||||
|
||||
inline double ei_real(double x) { return x; }
|
||||
inline double& ei_real_ref(double& x) { return x; }
|
||||
inline double ei_imag(double) { return 0.; }
|
||||
@@ -189,15 +178,15 @@ template<> inline double ei_random()
|
||||
{
|
||||
return ei_random<double>(-ei_random_amplitude<double>(), ei_random_amplitude<double>());
|
||||
}
|
||||
inline bool ei_isMuchSmallerThan(double a, double b, double prec = dummy_precision<double>())
|
||||
inline bool ei_isMuchSmallerThan(double a, double b, double prec = NumTraits<double>::dummy_precision())
|
||||
{
|
||||
return ei_abs(a) <= ei_abs(b) * prec;
|
||||
}
|
||||
inline bool ei_isApprox(double a, double b, double prec = dummy_precision<double>())
|
||||
inline bool ei_isApprox(double a, double b, double prec = NumTraits<double>::dummy_precision())
|
||||
{
|
||||
return ei_abs(a - b) <= std::min(ei_abs(a), ei_abs(b)) * prec;
|
||||
}
|
||||
inline bool ei_isApproxOrLessThan(double a, double b, double prec = dummy_precision<double>())
|
||||
inline bool ei_isApproxOrLessThan(double a, double b, double prec = NumTraits<double>::dummy_precision())
|
||||
{
|
||||
return a <= b || ei_isApprox(a, b, prec);
|
||||
}
|
||||
@@ -206,7 +195,6 @@ inline bool ei_isApproxOrLessThan(double a, double b, double prec = dummy_precis
|
||||
*** complex<float> ***
|
||||
*********************/
|
||||
|
||||
template<> inline float dummy_precision<std::complex<float> >() { return dummy_precision<float>(); }
|
||||
inline float ei_real(const std::complex<float>& x) { return std::real(x); }
|
||||
inline float ei_imag(const std::complex<float>& x) { return std::imag(x); }
|
||||
inline float& ei_real_ref(std::complex<float>& x) { return reinterpret_cast<float*>(&x)[0]; }
|
||||
@@ -225,15 +213,15 @@ template<> inline std::complex<float> ei_random()
|
||||
{
|
||||
return std::complex<float>(ei_random<float>(), ei_random<float>());
|
||||
}
|
||||
inline bool ei_isMuchSmallerThan(const std::complex<float>& a, const std::complex<float>& b, float prec = dummy_precision<float>())
|
||||
inline bool ei_isMuchSmallerThan(const std::complex<float>& a, const std::complex<float>& b, float prec = NumTraits<float>::dummy_precision())
|
||||
{
|
||||
return ei_abs2(a) <= ei_abs2(b) * prec * prec;
|
||||
}
|
||||
inline bool ei_isMuchSmallerThan(const std::complex<float>& a, float b, float prec = dummy_precision<float>())
|
||||
inline bool ei_isMuchSmallerThan(const std::complex<float>& a, float b, float prec = NumTraits<float>::dummy_precision())
|
||||
{
|
||||
return ei_abs2(a) <= ei_abs2(b) * prec * prec;
|
||||
}
|
||||
inline bool ei_isApprox(const std::complex<float>& a, const std::complex<float>& b, float prec = dummy_precision<float>())
|
||||
inline bool ei_isApprox(const std::complex<float>& a, const std::complex<float>& b, float prec = NumTraits<float>::dummy_precision())
|
||||
{
|
||||
return ei_isApprox(ei_real(a), ei_real(b), prec)
|
||||
&& ei_isApprox(ei_imag(a), ei_imag(b), prec);
|
||||
@@ -244,7 +232,6 @@ inline bool ei_isApprox(const std::complex<float>& a, const std::complex<float>&
|
||||
*** complex<double> ***
|
||||
**********************/
|
||||
|
||||
template<> inline double dummy_precision<std::complex<double> >() { return dummy_precision<double>(); }
|
||||
inline double ei_real(const std::complex<double>& x) { return std::real(x); }
|
||||
inline double ei_imag(const std::complex<double>& x) { return std::imag(x); }
|
||||
inline double& ei_real_ref(std::complex<double>& x) { return reinterpret_cast<double*>(&x)[0]; }
|
||||
@@ -263,15 +250,15 @@ template<> inline std::complex<double> ei_random()
|
||||
{
|
||||
return std::complex<double>(ei_random<double>(), ei_random<double>());
|
||||
}
|
||||
inline bool ei_isMuchSmallerThan(const std::complex<double>& a, const std::complex<double>& b, double prec = dummy_precision<double>())
|
||||
inline bool ei_isMuchSmallerThan(const std::complex<double>& a, const std::complex<double>& b, double prec = NumTraits<double>::dummy_precision())
|
||||
{
|
||||
return ei_abs2(a) <= ei_abs2(b) * prec * prec;
|
||||
}
|
||||
inline bool ei_isMuchSmallerThan(const std::complex<double>& a, double b, double prec = dummy_precision<double>())
|
||||
inline bool ei_isMuchSmallerThan(const std::complex<double>& a, double b, double prec = NumTraits<double>::dummy_precision())
|
||||
{
|
||||
return ei_abs2(a) <= ei_abs2(b) * prec * prec;
|
||||
}
|
||||
inline bool ei_isApprox(const std::complex<double>& a, const std::complex<double>& b, double prec = dummy_precision<double>())
|
||||
inline bool ei_isApprox(const std::complex<double>& a, const std::complex<double>& b, double prec = NumTraits<double>::dummy_precision())
|
||||
{
|
||||
return ei_isApprox(ei_real(a), ei_real(b), prec)
|
||||
&& ei_isApprox(ei_imag(a), ei_imag(b), prec);
|
||||
@@ -283,7 +270,6 @@ inline bool ei_isApprox(const std::complex<double>& a, const std::complex<double
|
||||
*** long double ***
|
||||
******************/
|
||||
|
||||
template<> inline long double dummy_precision<long double>() { return dummy_precision<double>(); }
|
||||
inline long double ei_real(long double x) { return x; }
|
||||
inline long double& ei_real_ref(long double& x) { return x; }
|
||||
inline long double ei_imag(long double) { return 0.; }
|
||||
@@ -306,15 +292,15 @@ template<> inline long double ei_random()
|
||||
{
|
||||
return ei_random<double>(-ei_random_amplitude<double>(), ei_random_amplitude<double>());
|
||||
}
|
||||
inline bool ei_isMuchSmallerThan(long double a, long double b, long double prec = dummy_precision<long double>())
|
||||
inline bool ei_isMuchSmallerThan(long double a, long double b, long double prec = NumTraits<long double>::dummy_precision())
|
||||
{
|
||||
return ei_abs(a) <= ei_abs(b) * prec;
|
||||
}
|
||||
inline bool ei_isApprox(long double a, long double b, long double prec = dummy_precision<long double>())
|
||||
inline bool ei_isApprox(long double a, long double b, long double prec = NumTraits<long double>::dummy_precision())
|
||||
{
|
||||
return ei_abs(a - b) <= std::min(ei_abs(a), ei_abs(b)) * prec;
|
||||
}
|
||||
inline bool ei_isApproxOrLessThan(long double a, long double b, long double prec = dummy_precision<long double>())
|
||||
inline bool ei_isApproxOrLessThan(long double a, long double b, long double prec = NumTraits<long double>::dummy_precision())
|
||||
{
|
||||
return a <= b || ei_isApprox(a, b, prec);
|
||||
}
|
||||
@@ -323,7 +309,6 @@ inline bool ei_isApproxOrLessThan(long double a, long double b, long double prec
|
||||
*** bool ***
|
||||
**************/
|
||||
|
||||
template<> inline bool dummy_precision<bool>() { return 0; }
|
||||
inline bool ei_real(bool x) { return x; }
|
||||
inline bool& ei_real_ref(bool& x) { return x; }
|
||||
inline bool ei_imag(bool) { return 0; }
|
||||
@@ -336,15 +321,15 @@ template<> inline bool ei_random()
|
||||
{
|
||||
return (ei_random<int>(0,1) == 1);
|
||||
}
|
||||
inline bool ei_isMuchSmallerThan(bool a, bool, bool = dummy_precision<bool>())
|
||||
inline bool ei_isMuchSmallerThan(bool a, bool, bool = NumTraits<bool>::dummy_precision())
|
||||
{
|
||||
return !a;
|
||||
}
|
||||
inline bool ei_isApprox(bool a, bool b, bool = dummy_precision<bool>())
|
||||
inline bool ei_isApprox(bool a, bool b, bool = NumTraits<bool>::dummy_precision())
|
||||
{
|
||||
return a == b;
|
||||
}
|
||||
inline bool ei_isApproxOrLessThan(bool a, bool b, bool = dummy_precision<bool>())
|
||||
inline bool ei_isApproxOrLessThan(bool a, bool b, bool = NumTraits<bool>::dummy_precision())
|
||||
{
|
||||
return int(a) <= int(b);
|
||||
}
|
||||
|
||||
@@ -253,16 +253,16 @@ template<typename Derived> class MatrixBase
|
||||
Derived& setIdentity();
|
||||
Derived& setIdentity(int rows, int cols);
|
||||
|
||||
bool isIdentity(RealScalar prec = dummy_precision<Scalar>()) const;
|
||||
bool isDiagonal(RealScalar prec = dummy_precision<Scalar>()) const;
|
||||
bool isIdentity(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||
bool isDiagonal(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||
|
||||
bool isUpperTriangular(RealScalar prec = dummy_precision<Scalar>()) const;
|
||||
bool isLowerTriangular(RealScalar prec = dummy_precision<Scalar>()) const;
|
||||
bool isUpperTriangular(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||
bool isLowerTriangular(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||
|
||||
template<typename OtherDerived>
|
||||
bool isOrthogonal(const MatrixBase<OtherDerived>& other,
|
||||
RealScalar prec = dummy_precision<Scalar>()) const;
|
||||
bool isUnitary(RealScalar prec = dummy_precision<Scalar>()) const;
|
||||
RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||
bool isUnitary(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||
|
||||
/** \returns true if each coefficients of \c *this and \a other are all exactly equal.
|
||||
* \warning When using floating point scalar values you probably should rather use a
|
||||
@@ -310,13 +310,13 @@ template<typename Derived> class MatrixBase
|
||||
ResultType& inverse,
|
||||
typename ResultType::Scalar& determinant,
|
||||
bool& invertible,
|
||||
const RealScalar& absDeterminantThreshold = dummy_precision<Scalar>()
|
||||
const RealScalar& absDeterminantThreshold = NumTraits<Scalar>::dummy_precision()
|
||||
) const;
|
||||
template<typename ResultType>
|
||||
void computeInverseWithCheck(
|
||||
ResultType& inverse,
|
||||
bool& invertible,
|
||||
const RealScalar& absDeterminantThreshold = dummy_precision<Scalar>()
|
||||
const RealScalar& absDeterminantThreshold = NumTraits<Scalar>::dummy_precision()
|
||||
) const;
|
||||
Scalar determinant() const;
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@
|
||||
template<typename T> struct NumTraits;
|
||||
|
||||
template<> struct NumTraits<int>
|
||||
: std::numeric_limits<int>
|
||||
{
|
||||
typedef int Real;
|
||||
typedef double FloatingPoint;
|
||||
@@ -60,9 +61,12 @@ template<> struct NumTraits<int>
|
||||
AddCost = 1,
|
||||
MulCost = 1
|
||||
};
|
||||
|
||||
inline static int dummy_precision() { return 0; }
|
||||
};
|
||||
|
||||
template<> struct NumTraits<float>
|
||||
: std::numeric_limits<float>
|
||||
{
|
||||
typedef float Real;
|
||||
typedef float FloatingPoint;
|
||||
@@ -74,9 +78,12 @@ template<> struct NumTraits<float>
|
||||
AddCost = 1,
|
||||
MulCost = 1
|
||||
};
|
||||
|
||||
inline static float dummy_precision() { return 1e-5f; }
|
||||
};
|
||||
|
||||
template<> struct NumTraits<double>
|
||||
: std::numeric_limits<double>
|
||||
{
|
||||
typedef double Real;
|
||||
typedef double FloatingPoint;
|
||||
@@ -88,9 +95,12 @@ template<> struct NumTraits<double>
|
||||
AddCost = 1,
|
||||
MulCost = 1
|
||||
};
|
||||
|
||||
inline static double dummy_precision() { return 1e-12; }
|
||||
};
|
||||
|
||||
template<typename _Real> struct NumTraits<std::complex<_Real> >
|
||||
: std::numeric_limits<std::complex<_Real> >
|
||||
{
|
||||
typedef _Real Real;
|
||||
typedef std::complex<_Real> FloatingPoint;
|
||||
@@ -102,9 +112,13 @@ template<typename _Real> struct NumTraits<std::complex<_Real> >
|
||||
AddCost = 2 * NumTraits<Real>::AddCost,
|
||||
MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
|
||||
};
|
||||
|
||||
inline static Real epsilon() { return std::numeric_limits<Real>::epsilon(); }
|
||||
inline static Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
|
||||
};
|
||||
|
||||
template<> struct NumTraits<long long int>
|
||||
: std::numeric_limits<long long int>
|
||||
{
|
||||
typedef long long int Real;
|
||||
typedef long double FloatingPoint;
|
||||
@@ -119,6 +133,7 @@ template<> struct NumTraits<long long int>
|
||||
};
|
||||
|
||||
template<> struct NumTraits<long double>
|
||||
: std::numeric_limits<long double>
|
||||
{
|
||||
typedef long double Real;
|
||||
typedef long double FloatingPoint;
|
||||
@@ -130,9 +145,12 @@ template<> struct NumTraits<long double>
|
||||
AddCost = 1,
|
||||
MulCost = 1
|
||||
};
|
||||
|
||||
static inline long double dummy_precision() { return NumTraits<double>::dummy_precision(); }
|
||||
};
|
||||
|
||||
template<> struct NumTraits<bool>
|
||||
: std::numeric_limits<bool>
|
||||
{
|
||||
typedef bool Real;
|
||||
typedef float FloatingPoint;
|
||||
@@ -144,6 +162,8 @@ template<> struct NumTraits<bool>
|
||||
AddCost = 1,
|
||||
MulCost = 1
|
||||
};
|
||||
|
||||
inline static bool dummy_precision() { return 0; }
|
||||
};
|
||||
|
||||
#endif // EIGEN_NUMTRAITS_H
|
||||
|
||||
Reference in New Issue
Block a user