prefix global functions with ei_ as previous solution was rather

fragile. also fix compilation with g++ 4.3.
This commit is contained in:
Benoit Jacob 2008-02-28 12:38:12 +00:00
parent c67e717404
commit 6907886a15
20 changed files with 177 additions and 174 deletions

View File

@ -1,3 +1,5 @@
#include <cstdlib>
#include <cmath>
#include <complex> #include <complex>
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>

View File

@ -63,7 +63,7 @@ template<typename MatrixType> class Conjugate : NoOperatorEquals,
Scalar _coeff(int row, int col) const Scalar _coeff(int row, int col) const
{ {
return conj(m_matrix.coeff(row, col)); return ei_conj(m_matrix.coeff(row, col));
} }
protected: protected:

View File

@ -109,14 +109,14 @@ bool MatrixBase<Scalar, Derived>::isDiagonal
RealScalar maxAbsOnDiagonal = static_cast<RealScalar>(-1); RealScalar maxAbsOnDiagonal = static_cast<RealScalar>(-1);
for(int j = 0; j < cols(); j++) for(int j = 0; j < cols(); j++)
{ {
RealScalar absOnDiagonal = abs(coeff(j,j)); RealScalar absOnDiagonal = ei_abs(coeff(j,j));
if(absOnDiagonal > maxAbsOnDiagonal) maxAbsOnDiagonal = absOnDiagonal; if(absOnDiagonal > maxAbsOnDiagonal) maxAbsOnDiagonal = absOnDiagonal;
} }
for(int j = 0; j < cols(); j++) for(int j = 0; j < cols(); j++)
for(int i = 0; i < j; i++) for(int i = 0; i < j; i++)
{ {
if(!Eigen::isMuchSmallerThan(coeff(i, j), maxAbsOnDiagonal, prec)) return false; if(!ei_isMuchSmallerThan(coeff(i, j), maxAbsOnDiagonal, prec)) return false;
if(!Eigen::isMuchSmallerThan(coeff(j, i), maxAbsOnDiagonal, prec)) return false; if(!ei_isMuchSmallerThan(coeff(j, i), maxAbsOnDiagonal, prec)) return false;
} }
return true; return true;
} }

View File

@ -32,7 +32,7 @@ struct DotUnroller
static void run(const Derived1 &v1, const Derived2& v2, typename Derived1::Scalar &dot) static void run(const Derived1 &v1, const Derived2& v2, typename Derived1::Scalar &dot)
{ {
DotUnroller<Index-1, Size, Derived1, Derived2>::run(v1, v2, dot); DotUnroller<Index-1, Size, Derived1, Derived2>::run(v1, v2, dot);
dot += v1.coeff(Index) * conj(v2.coeff(Index)); dot += v1.coeff(Index) * ei_conj(v2.coeff(Index));
} }
}; };
@ -41,7 +41,7 @@ struct DotUnroller<0, Size, Derived1, Derived2>
{ {
static void run(const Derived1 &v1, const Derived2& v2, typename Derived1::Scalar &dot) static void run(const Derived1 &v1, const Derived2& v2, typename Derived1::Scalar &dot)
{ {
dot = v1.coeff(0) * conj(v2.coeff(0)); dot = v1.coeff(0) * ei_conj(v2.coeff(0));
} }
}; };
@ -84,9 +84,9 @@ Scalar MatrixBase<Scalar, Derived>::dot(const OtherDerived& other) const
::run(*static_cast<const Derived*>(this), other, res); ::run(*static_cast<const Derived*>(this), other, res);
else else
{ {
res = (*this).coeff(0) * conj(other.coeff(0)); res = (*this).coeff(0) * ei_conj(other.coeff(0));
for(int i = 1; i < size(); i++) for(int i = 1; i < size(); i++)
res += (*this).coeff(i)* conj(other.coeff(i)); res += (*this).coeff(i)* ei_conj(other.coeff(i));
} }
return res; return res;
} }
@ -100,7 +100,7 @@ Scalar MatrixBase<Scalar, Derived>::dot(const OtherDerived& other) const
template<typename Scalar, typename Derived> template<typename Scalar, typename Derived>
typename NumTraits<Scalar>::Real MatrixBase<Scalar, Derived>::norm2() const typename NumTraits<Scalar>::Real MatrixBase<Scalar, Derived>::norm2() const
{ {
return real(dot(*this)); return ei_real(dot(*this));
} }
/** \returns the norm of *this, i.e. the square root of the dot product of *this with itself. /** \returns the norm of *this, i.e. the square root of the dot product of *this with itself.
@ -112,7 +112,7 @@ typename NumTraits<Scalar>::Real MatrixBase<Scalar, Derived>::norm2() const
template<typename Scalar, typename Derived> template<typename Scalar, typename Derived>
typename NumTraits<Scalar>::Real MatrixBase<Scalar, Derived>::norm() const typename NumTraits<Scalar>::Real MatrixBase<Scalar, Derived>::norm() const
{ {
return sqrt(norm2()); return ei_sqrt(norm2());
} }
/** \returns an expression of the quotient of *this by its own norm. /** \returns an expression of the quotient of *this by its own norm.
@ -140,7 +140,7 @@ bool MatrixBase<Scalar, Derived>::isOrtho
(const OtherDerived& other, (const OtherDerived& other,
typename NumTraits<Scalar>::Real prec) const typename NumTraits<Scalar>::Real prec) const
{ {
return abs2(dot(other)) <= prec * prec * norm2() * other.norm2(); return ei_abs2(dot(other)) <= prec * prec * norm2() * other.norm2();
} }
/** \returns true if *this is approximately an unitary matrix, /** \returns true if *this is approximately an unitary matrix,
@ -160,10 +160,10 @@ bool MatrixBase<Scalar, Derived>::isOrtho
{ {
for(int i = 0; i < cols(); i++) for(int i = 0; i < cols(); i++)
{ {
if(!Eigen::isApprox(col(i).norm2(), static_cast<Scalar>(1), prec)) if(!ei_isApprox(col(i).norm2(), static_cast<Scalar>(1), prec))
return false; return false;
for(int j = 0; j < i; j++) for(int j = 0; j < i; j++)
if(!Eigen::isMuchSmallerThan(col(i).dot(col(j)), static_cast<Scalar>(1), prec)) if(!ei_isMuchSmallerThan(col(i).dot(col(j)), static_cast<Scalar>(1), prec))
return false; return false;
} }
return true; return true;

View File

@ -37,10 +37,10 @@
* \note Because of the multiplicativeness of this comparison, one can't use this function * \note Because of the multiplicativeness of this comparison, one can't use this function
* to check whether \c *this is approximately equal to the zero matrix or vector. * to check whether \c *this is approximately equal to the zero matrix or vector.
* Indeed, \c isApprox(zero) returns false unless \c *this itself is exactly the zero matrix * Indeed, \c isApprox(zero) returns false unless \c *this itself is exactly the zero matrix
* or vector. If you want to test whether \c *this is zero, use isMuchSmallerThan(const * or vector. If you want to test whether \c *this is zero, use ei_isMuchSmallerThan(const
* RealScalar&, RealScalar) instead. * RealScalar&, RealScalar) instead.
* *
* \sa isMuchSmallerThan(const RealScalar&, RealScalar) const * \sa ei_isMuchSmallerThan(const RealScalar&, RealScalar) const
*/ */
template<typename Scalar, typename Derived> template<typename Scalar, typename Derived>
template<typename OtherDerived> template<typename OtherDerived>
@ -82,12 +82,12 @@ bool MatrixBase<Scalar, Derived>::isMuchSmallerThan(
{ {
if(Traits::IsVectorAtCompileTime) if(Traits::IsVectorAtCompileTime)
{ {
return(norm2() <= abs2(other * prec)); return(norm2() <= ei_abs2(other * prec));
} }
else else
{ {
for(int i = 0; i < cols(); i++) for(int i = 0; i < cols(); i++)
if(col(i).norm2() > abs2(other * prec)) if(col(i).norm2() > ei_abs2(other * prec))
return false; return false;
return true; return true;
} }

View File

@ -125,12 +125,12 @@ bool MatrixBase<Scalar, Derived>::isIdentity
{ {
if(i == j) if(i == j)
{ {
if(!Eigen::isApprox(coeff(i, j), static_cast<Scalar>(1), prec)) if(!ei_isApprox(coeff(i, j), static_cast<Scalar>(1), prec))
return false; return false;
} }
else else
{ {
if(!Eigen::isMuchSmallerThan(coeff(i, j), static_cast<RealScalar>(1), prec)) if(!ei_isMuchSmallerThan(coeff(i, j), static_cast<RealScalar>(1), prec))
return false; return false;
} }
} }

View File

@ -27,16 +27,16 @@
#define EIGEN_MATHFUNCTIONS_H #define EIGEN_MATHFUNCTIONS_H
template<typename T> inline typename NumTraits<T>::Real precision(); template<typename T> inline typename NumTraits<T>::Real precision();
template<typename T> inline T random(T a, T b); template<typename T> inline T ei_random(T a, T b);
template<typename T> inline T random(); template<typename T> inline T ei_random();
template<> inline int precision<int>() { return 0; } template<> inline int precision<int>() { return 0; }
inline int real(int x) { return x; } inline int ei_real(int x) { return x; }
inline int imag(int) { return 0; } inline int ei_imag(int) { return 0; }
inline int conj(int x) { return x; } inline int ei_conj(int x) { return x; }
inline int abs(int x) { return std::abs(x); } inline int ei_abs(int x) { return abs(x); }
inline int abs2(int x) { return x*x; } inline int ei_abs2(int x) { return x*x; }
inline int sqrt(int) inline int ei_sqrt(int)
{ {
// Taking the square root of integers is not allowed // Taking the square root of integers is not allowed
// (the square root does not always exist within the integers). // (the square root does not always exist within the integers).
@ -44,91 +44,91 @@ inline int sqrt(int)
assert(false); assert(false);
return 0; return 0;
} }
template<> inline int random(int a, int b) template<> inline int ei_random(int a, int b)
{ {
// We can't just do rand()%n as only the high-order bits are really random // We can't just do rand()%n as only the high-order bits are really random
return a + static_cast<int>((b-a+1) * (rand() / (RAND_MAX + 1.0))); return a + static_cast<int>((b-a+1) * (rand() / (RAND_MAX + 1.0)));
} }
template<> inline int random() template<> inline int ei_random()
{ {
return random<int>(-10, 10); return ei_random<int>(-10, 10);
} }
inline bool isMuchSmallerThan(int a, int, int = precision<int>()) inline bool ei_isMuchSmallerThan(int a, int, int = precision<int>())
{ {
return a == 0; return a == 0;
} }
inline bool isApprox(int a, int b, int = precision<int>()) inline bool ei_isApprox(int a, int b, int = precision<int>())
{ {
return a == b; return a == b;
} }
inline bool isApproxOrLessThan(int a, int b, int = precision<int>()) inline bool ei_isApproxOrLessThan(int a, int b, int = precision<int>())
{ {
return a <= b; return a <= b;
} }
template<> inline float precision<float>() { return 1e-5f; } template<> inline float precision<float>() { return 1e-5f; }
inline float real(float x) { return x; } inline float ei_real(float x) { return x; }
inline float imag(float) { return 0.f; } inline float ei_imag(float) { return 0.f; }
inline float conj(float x) { return x; } inline float ei_conj(float x) { return x; }
inline float abs(float x) { return std::abs(x); } inline float ei_abs(float x) { return std::abs(x); }
inline float abs2(float x) { return x*x; } inline float ei_abs2(float x) { return x*x; }
inline float sqrt(float x) { return std::sqrt(x); } inline float ei_sqrt(float x) { return std::sqrt(x); }
template<> inline float random(float a, float b) template<> inline float ei_random(float a, float b)
{ {
return a + (b-a) * std::rand() / RAND_MAX; return a + (b-a) * std::rand() / RAND_MAX;
} }
template<> inline float random() template<> inline float ei_random()
{ {
return random<float>(-10.0f, 10.0f); return ei_random<float>(-10.0f, 10.0f);
} }
inline bool isMuchSmallerThan(float a, float b, float prec = precision<float>()) inline bool ei_isMuchSmallerThan(float a, float b, float prec = precision<float>())
{ {
return std::abs(a) <= std::abs(b) * prec; return ei_abs(a) <= ei_abs(b) * prec;
} }
inline bool isApprox(float a, float b, float prec = precision<float>()) inline bool ei_isApprox(float a, float b, float prec = precision<float>())
{ {
return std::abs(a - b) <= std::min(std::abs(a), std::abs(b)) * prec; return ei_abs(a - b) <= std::min(ei_abs(a), ei_abs(b)) * prec;
} }
inline bool isApproxOrLessThan(float a, float b, float prec = precision<float>()) inline bool ei_isApproxOrLessThan(float a, float b, float prec = precision<float>())
{ {
return a <= b || isApprox(a, b, prec); return a <= b || ei_isApprox(a, b, prec);
} }
template<> inline double precision<double>() { return 1e-11; } template<> inline double precision<double>() { return 1e-11; }
inline double real(double x) { return x; } inline double ei_real(double x) { return x; }
inline double imag(double) { return 0.; } inline double ei_imag(double) { return 0.; }
inline double conj(double x) { return x; } inline double ei_conj(double x) { return x; }
inline double abs(double x) { return std::abs(x); } inline double ei_abs(double x) { return std::abs(x); }
inline double abs2(double x) { return x*x; } inline double ei_abs2(double x) { return x*x; }
inline double sqrt(double x) { return std::sqrt(x); } inline double ei_sqrt(double x) { return std::sqrt(x); }
template<> inline double random(double a, double b) template<> inline double ei_random(double a, double b)
{ {
return a + (b-a) * std::rand() / RAND_MAX; return a + (b-a) * std::rand() / RAND_MAX;
} }
template<> inline double random() template<> inline double ei_random()
{ {
return random<double>(-10.0, 10.0); return ei_random<double>(-10.0, 10.0);
} }
inline bool isMuchSmallerThan(double a, double b, double prec = precision<double>()) inline bool ei_isMuchSmallerThan(double a, double b, double prec = precision<double>())
{ {
return std::abs(a) <= std::abs(b) * prec; return ei_abs(a) <= ei_abs(b) * prec;
} }
inline bool isApprox(double a, double b, double prec = precision<double>()) inline bool ei_isApprox(double a, double b, double prec = precision<double>())
{ {
return std::abs(a - b) <= std::min(std::abs(a), std::abs(b)) * prec; return ei_abs(a - b) <= std::min(ei_abs(a), ei_abs(b)) * prec;
} }
inline bool isApproxOrLessThan(double a, double b, double prec = precision<double>()) inline bool ei_isApproxOrLessThan(double a, double b, double prec = precision<double>())
{ {
return a <= b || isApprox(a, b, prec); return a <= b || ei_isApprox(a, b, prec);
} }
template<> inline float precision<std::complex<float> >() { return precision<float>(); } template<> inline float precision<std::complex<float> >() { return precision<float>(); }
inline float real(const std::complex<float>& x) { return std::real(x); } inline float ei_real(const std::complex<float>& x) { return std::real(x); }
inline float imag(const std::complex<float>& x) { return std::imag(x); } inline float ei_imag(const std::complex<float>& x) { return std::imag(x); }
inline std::complex<float> conj(const std::complex<float>& x) { return std::conj(x); } inline std::complex<float> ei_conj(const std::complex<float>& x) { return std::conj(x); }
inline float abs(const std::complex<float>& x) { return std::abs(x); } inline float ei_abs(const std::complex<float>& x) { return std::abs(x); }
inline float abs2(const std::complex<float>& x) { return std::norm(x); } inline float ei_abs2(const std::complex<float>& x) { return std::norm(x); }
inline std::complex<float> sqrt(const std::complex<float>&) inline std::complex<float> ei_sqrt(const std::complex<float>&)
{ {
// Taking the square roots of complex numbers is not allowed, // Taking the square roots of complex numbers is not allowed,
// as this is ambiguous (there are two square roots). // as this is ambiguous (there are two square roots).
@ -136,49 +136,49 @@ inline std::complex<float> sqrt(const std::complex<float>&)
assert(false); assert(false);
return 0; return 0;
} }
template<> inline std::complex<float> random() template<> inline std::complex<float> ei_random()
{ {
return std::complex<float>(random<float>(), random<float>()); return std::complex<float>(ei_random<float>(), ei_random<float>());
} }
inline bool isMuchSmallerThan(const std::complex<float>& a, const std::complex<float>& b, float prec = precision<float>()) inline bool ei_isMuchSmallerThan(const std::complex<float>& a, const std::complex<float>& b, float prec = precision<float>())
{ {
return abs2(a) <= abs2(b) * prec * prec; return ei_abs2(a) <= ei_abs2(b) * prec * prec;
} }
inline bool isMuchSmallerThan(const std::complex<float>& a, float b, float prec = precision<float>()) inline bool ei_isMuchSmallerThan(const std::complex<float>& a, float b, float prec = precision<float>())
{ {
return abs2(a) <= abs2(b) * prec * prec; return ei_abs2(a) <= ei_abs2(b) * prec * prec;
} }
inline bool isApprox(const std::complex<float>& a, const std::complex<float>& b, float prec = precision<float>()) inline bool ei_isApprox(const std::complex<float>& a, const std::complex<float>& b, float prec = precision<float>())
{ {
return isApprox(std::real(a), std::real(b), prec) return ei_isApprox(ei_real(a), ei_real(b), prec)
&& isApprox(std::imag(a), std::imag(b), prec); && ei_isApprox(ei_imag(a), ei_imag(b), prec);
} }
// isApproxOrLessThan wouldn't make sense for complex numbers // ei_isApproxOrLessThan wouldn't make sense for complex numbers
template<> inline double precision<std::complex<double> >() { return precision<double>(); } template<> inline double precision<std::complex<double> >() { return precision<double>(); }
inline double real(const std::complex<double>& x) { return std::real(x); } inline double ei_real(const std::complex<double>& x) { return std::real(x); }
inline double imag(const std::complex<double>& x) { return std::imag(x); } inline double ei_imag(const std::complex<double>& x) { return std::imag(x); }
inline std::complex<double> conj(const std::complex<double>& x) { return std::conj(x); } inline std::complex<double> ei_conj(const std::complex<double>& x) { return std::conj(x); }
inline double abs(const std::complex<double>& x) { return std::abs(x); } inline double ei_abs(const std::complex<double>& x) { return std::abs(x); }
inline double abs2(const std::complex<double>& x) { return std::norm(x); } inline double ei_abs2(const std::complex<double>& x) { return std::norm(x); }
template<> inline std::complex<double> random() template<> inline std::complex<double> ei_random()
{ {
return std::complex<double>(random<double>(), random<double>()); return std::complex<double>(ei_random<double>(), ei_random<double>());
} }
inline bool isMuchSmallerThan(const std::complex<double>& a, const std::complex<double>& b, double prec = precision<double>()) inline bool ei_isMuchSmallerThan(const std::complex<double>& a, const std::complex<double>& b, double prec = precision<double>())
{ {
return abs2(a) <= abs2(b) * prec * prec; return ei_abs2(a) <= ei_abs2(b) * prec * prec;
} }
inline bool isMuchSmallerThan(const std::complex<double>& a, double b, double prec = precision<double>()) inline bool ei_isMuchSmallerThan(const std::complex<double>& a, double b, double prec = precision<double>())
{ {
return abs2(a) <= abs2(b) * prec * prec; return ei_abs2(a) <= ei_abs2(b) * prec * prec;
} }
inline bool isApprox(const std::complex<double>& a, const std::complex<double>& b, double prec = precision<double>()) inline bool ei_isApprox(const std::complex<double>& a, const std::complex<double>& b, double prec = precision<double>())
{ {
return isApprox(std::real(a), std::real(b), prec) return ei_isApprox(ei_real(a), ei_real(b), prec)
&& isApprox(std::imag(a), std::imag(b), prec); && ei_isApprox(ei_imag(a), ei_imag(b), prec);
} }
// isApproxOrLessThan wouldn't make sense for complex numbers // ei_isApproxOrLessThan wouldn't make sense for complex numbers
#define EIGEN_MAKE_MORE_OVERLOADED_COMPLEX_OPERATOR_STAR(T,U) \ #define EIGEN_MAKE_MORE_OVERLOADED_COMPLEX_OPERATOR_STAR(T,U) \
inline std::complex<T> operator*(U a, const std::complex<T>& b) \ inline std::complex<T> operator*(U a, const std::complex<T>& b) \

View File

@ -314,7 +314,7 @@ template<typename Scalar, typename Derived> class MatrixBase
for(int j = 0; j < cols(); j++) for(int j = 0; j < cols(); j++)
for(int i = 0; i < rows(); i++) for(int i = 0; i < rows(); i++)
{ {
RealScalar x = abs(coeff(i,j)); RealScalar x = ei_abs(coeff(i,j));
if(x > biggest) if(x > biggest)
{ {
biggest = x; biggest = x;

View File

@ -146,7 +146,7 @@ bool MatrixBase<Scalar, Derived>::isOnes
{ {
for(int j = 0; j < cols(); j++) for(int j = 0; j < cols(); j++)
for(int i = 0; i < rows(); i++) for(int i = 0; i < rows(); i++)
if(!Eigen::isApprox(coeff(i, j), static_cast<Scalar>(1), prec)) if(!ei_isApprox(coeff(i, j), static_cast<Scalar>(1), prec))
return false; return false;
return true; return true;
} }

View File

@ -55,7 +55,7 @@ template<typename MatrixType> class Random : NoOperatorEquals,
Scalar _coeff(int, int) const Scalar _coeff(int, int) const
{ {
return Eigen::random<Scalar>(); return ei_random<Scalar>();
} }
public: public:
@ -78,13 +78,13 @@ template<typename MatrixType> class Random : NoOperatorEquals,
* the returned matrix. Must be compatible with this MatrixBase type. * the returned matrix. Must be compatible with this MatrixBase type.
* *
* This variant is meant to be used for dynamic-size matrix types. For fixed-size types, * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
* it is redundant to pass \a rows and \a cols as arguments, so random() should be used * it is redundant to pass \a rows and \a cols as arguments, so ei_random() should be used
* instead. * instead.
* *
* Example: \include MatrixBase_random_int_int.cpp * Example: \include MatrixBase_random_int_int.cpp
* Output: \verbinclude MatrixBase_random_int_int.out * Output: \verbinclude MatrixBase_random_int_int.out
* *
* \sa random(), random(int) * \sa ei_random(), ei_random(int)
*/ */
template<typename Scalar, typename Derived> template<typename Scalar, typename Derived>
const Eval<Random<Derived> > const Eval<Random<Derived> >
@ -101,13 +101,13 @@ MatrixBase<Scalar, Derived>::random(int rows, int cols)
* \only_for_vectors * \only_for_vectors
* *
* This variant is meant to be used for dynamic-size vector types. For fixed-size types, * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
* it is redundant to pass \a size as argument, so random() should be used * it is redundant to pass \a size as argument, so ei_random() should be used
* instead. * instead.
* *
* Example: \include MatrixBase_random_int.cpp * Example: \include MatrixBase_random_int.cpp
* Output: \verbinclude MatrixBase_random_int.out * Output: \verbinclude MatrixBase_random_int.out
* *
* \sa random(), random(int,int) * \sa ei_random(), ei_random(int,int)
*/ */
template<typename Scalar, typename Derived> template<typename Scalar, typename Derived>
const Eval<Random<Derived> > const Eval<Random<Derived> >
@ -127,7 +127,7 @@ MatrixBase<Scalar, Derived>::random(int size)
* Example: \include MatrixBase_random.cpp * Example: \include MatrixBase_random.cpp
* Output: \verbinclude MatrixBase_random.out * Output: \verbinclude MatrixBase_random.out
* *
* \sa random(int), random(int,int) * \sa ei_random(int), ei_random(int,int)
*/ */
template<typename Scalar, typename Derived> template<typename Scalar, typename Derived>
const Eval<Random<Derived> > const Eval<Random<Derived> >
@ -141,7 +141,7 @@ MatrixBase<Scalar, Derived>::random()
* Example: \include MatrixBase_setRandom.cpp * Example: \include MatrixBase_setRandom.cpp
* Output: \verbinclude MatrixBase_setRandom.out * Output: \verbinclude MatrixBase_setRandom.out
* *
* \sa class Random, random() * \sa class Random, ei_random()
*/ */
template<typename Scalar, typename Derived> template<typename Scalar, typename Derived>
Derived& MatrixBase<Scalar, Derived>::setRandom() Derived& MatrixBase<Scalar, Derived>::setRandom()

View File

@ -146,7 +146,7 @@ bool MatrixBase<Scalar, Derived>::isZero
{ {
for(int j = 0; j < cols(); j++) for(int j = 0; j < cols(); j++)
for(int i = 0; i < rows(); i++) for(int i = 0; i < rows(); i++)
if(!Eigen::isMuchSmallerThan(coeff(i, j), static_cast<Scalar>(1), prec)) if(!ei_isMuchSmallerThan(coeff(i, j), static_cast<Scalar>(1), prec))
return false; return false;
return true; return true;
} }

View File

@ -1,8 +1,9 @@
// g++ -O3 -DNDEBUG benchmark.cpp -o benchmark && time ./benchmark // g++ -O3 -DNDEBUG benchmark.cpp -o benchmark && time ./benchmark
#include <cstdlib>
#include <cmath>
#include <Eigen/Core> #include <Eigen/Core>
using namespace std; //using namespace std;
USING_PART_OF_NAMESPACE_EIGEN USING_PART_OF_NAMESPACE_EIGEN
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -18,6 +19,6 @@ int main(int argc, char *argv[])
{ {
m = I + 0.00005 * (m + m*m); m = I + 0.00005 * (m + m*m);
} }
cout << m << endl; std::cout << m << std::endl;
return 0; return 0;
} }

View File

@ -51,8 +51,8 @@ template<typename MatrixType> void adjoint(const MatrixType& m)
v3 = VectorType::random(rows), v3 = VectorType::random(rows),
vzero = VectorType::zero(rows); vzero = VectorType::zero(rows);
Scalar s1 = random<Scalar>(), Scalar s1 = ei_random<Scalar>(),
s2 = random<Scalar>(); s2 = ei_random<Scalar>();
// check involutivity of adjoint, transpose, conjugate // check involutivity of adjoint, transpose, conjugate
VERIFY_IS_APPROX(m1.transpose().transpose(), m1); VERIFY_IS_APPROX(m1.transpose().transpose(), m1);
@ -70,18 +70,18 @@ template<typename MatrixType> void adjoint(const MatrixType& m)
VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1); VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1);
VERIFY_IS_APPROX((m1.transpose() * m2).conjugate(), m1.adjoint() * m2.conjugate()); VERIFY_IS_APPROX((m1.transpose() * m2).conjugate(), m1.adjoint() * m2.conjugate());
VERIFY_IS_APPROX((s1 * m1).transpose(), s1 * m1.transpose()); VERIFY_IS_APPROX((s1 * m1).transpose(), s1 * m1.transpose());
VERIFY_IS_APPROX((s1 * m1).conjugate(), conj(s1) * m1.conjugate()); VERIFY_IS_APPROX((s1 * m1).conjugate(), ei_conj(s1) * m1.conjugate());
VERIFY_IS_APPROX((s1 * m1).adjoint(), conj(s1) * m1.adjoint()); VERIFY_IS_APPROX((s1 * m1).adjoint(), ei_conj(s1) * m1.adjoint());
// check basic properties of dot, norm, norm2 // check basic properties of dot, norm, norm2
typedef typename NumTraits<Scalar>::Real RealScalar; typedef typename NumTraits<Scalar>::Real RealScalar;
VERIFY_IS_APPROX((s1 * v1 + s2 * v2).dot(v3), s1 * v1.dot(v3) + s2 * v2.dot(v3)); VERIFY_IS_APPROX((s1 * v1 + s2 * v2).dot(v3), s1 * v1.dot(v3) + s2 * v2.dot(v3));
VERIFY_IS_APPROX(v3.dot(s1 * v1 + s2 * v2), conj(s1)*v3.dot(v1)+conj(s2)*v3.dot(v2)); VERIFY_IS_APPROX(v3.dot(s1 * v1 + s2 * v2), ei_conj(s1)*v3.dot(v1)+ei_conj(s2)*v3.dot(v2));
VERIFY_IS_APPROX(conj(v1.dot(v2)), v2.dot(v1)); VERIFY_IS_APPROX(ei_conj(v1.dot(v2)), v2.dot(v1));
VERIFY_IS_APPROX(abs(v1.dot(v1)), v1.norm2()); VERIFY_IS_APPROX(ei_abs(v1.dot(v1)), v1.norm2());
if(NumTraits<Scalar>::HasFloatingPoint) if(NumTraits<Scalar>::HasFloatingPoint)
VERIFY_IS_APPROX(v1.norm2(), v1.norm() * v1.norm()); VERIFY_IS_APPROX(v1.norm2(), v1.norm() * v1.norm());
VERIFY_IS_MUCH_SMALLER_THAN(abs(vzero.dot(v1)), static_cast<RealScalar>(1)); VERIFY_IS_MUCH_SMALLER_THAN(ei_abs(vzero.dot(v1)), static_cast<RealScalar>(1));
if(NumTraits<Scalar>::HasFloatingPoint) if(NumTraits<Scalar>::HasFloatingPoint)
VERIFY_IS_MUCH_SMALLER_THAN(vzero.norm(), static_cast<RealScalar>(1)); VERIFY_IS_MUCH_SMALLER_THAN(vzero.norm(), static_cast<RealScalar>(1));
@ -89,10 +89,10 @@ template<typename MatrixType> void adjoint(const MatrixType& m)
VERIFY_IS_APPROX(v1.dot(square * v2), (square.adjoint() * v1).dot(v2)); VERIFY_IS_APPROX(v1.dot(square * v2), (square.adjoint() * v1).dot(v2));
// like in testBasicStuff, test operator() to check const-qualification // like in testBasicStuff, test operator() to check const-qualification
int r = random<int>(0, rows-1), int r = ei_random<int>(0, rows-1),
c = random<int>(0, cols-1); c = ei_random<int>(0, cols-1);
VERIFY_IS_APPROX(m1.conjugate()(r,c), conj(m1(r,c))); VERIFY_IS_APPROX(m1.conjugate()(r,c), ei_conj(m1(r,c)));
VERIFY_IS_APPROX(m1.adjoint()(c,r), conj(m1(r,c))); VERIFY_IS_APPROX(m1.adjoint()(c,r), ei_conj(m1(r,c)));
} }

View File

@ -49,8 +49,8 @@ template<typename MatrixType> void basicStuff(const MatrixType& m)
v2 = VectorType::random(rows), v2 = VectorType::random(rows),
vzero = VectorType::zero(rows); vzero = VectorType::zero(rows);
int r = random<int>(0, rows-1), int r = ei_random<int>(0, rows-1),
c = random<int>(0, cols-1); c = ei_random<int>(0, cols-1);
VERIFY_IS_APPROX( v1, v1); VERIFY_IS_APPROX( v1, v1);
VERIFY_IS_NOT_APPROX( v1, 2*v1); VERIFY_IS_NOT_APPROX( v1, 2*v1);

View File

@ -53,11 +53,11 @@ template<typename MatrixType> void linearStructure(const MatrixType& m)
v2 = VectorType::random(rows), v2 = VectorType::random(rows),
vzero = VectorType::zero(rows); vzero = VectorType::zero(rows);
Scalar s1 = random<Scalar>(), Scalar s1 = ei_random<Scalar>(),
s2 = random<Scalar>(); s2 = ei_random<Scalar>();
int r = random<int>(0, rows-1), int r = ei_random<int>(0, rows-1),
c = random<int>(0, cols-1); c = ei_random<int>(0, cols-1);
VERIFY_IS_APPROX(-(-m1), m1); VERIFY_IS_APPROX(-(-m1), m1);
VERIFY_IS_APPROX(m1+m1, 2*m1); VERIFY_IS_APPROX(m1+m1, 2*m1);

View File

@ -38,12 +38,12 @@
#define DEFAULT_REPEAT 50 #define DEFAULT_REPEAT 50
#define VERIFY(a) QVERIFY(a) #define VERIFY(a) QVERIFY(a)
#define VERIFY_IS_APPROX(a, b) QVERIFY(test_isApprox(a, b)) #define VERIFY_IS_APPROX(a, b) QVERIFY(test_ei_isApprox(a, b))
#define VERIFY_IS_NOT_APPROX(a, b) QVERIFY(!test_isApprox(a, b)) #define VERIFY_IS_NOT_APPROX(a, b) QVERIFY(!test_ei_isApprox(a, b))
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b) QVERIFY(test_isMuchSmallerThan(a, b)) #define VERIFY_IS_MUCH_SMALLER_THAN(a, b) QVERIFY(test_ei_isMuchSmallerThan(a, b))
#define VERIFY_IS_NOT_MUCH_SMALLER_THAN(a, b) QVERIFY(!test_isMuchSmallerThan(a, b)) #define VERIFY_IS_NOT_MUCH_SMALLER_THAN(a, b) QVERIFY(!test_ei_isMuchSmallerThan(a, b))
#define VERIFY_IS_APPROX_OR_LESS_THAN(a, b) QVERIFY(test_isApproxOrLessThan(a, b)) #define VERIFY_IS_APPROX_OR_LESS_THAN(a, b) QVERIFY(test_ei_isApproxOrLessThan(a, b))
#define VERIFY_IS_NOT_APPROX_OR_LESS_THAN(a, b) QVERIFY(!test_isApproxOrLessThan(a, b)) #define VERIFY_IS_NOT_APPROX_OR_LESS_THAN(a, b) QVERIFY(!test_ei_isApproxOrLessThan(a, b))
namespace Eigen { namespace Eigen {
@ -54,53 +54,53 @@ template<> inline double test_precision<double>() { return 1e-5; }
template<> inline float test_precision<std::complex<float> >() { return test_precision<float>(); } template<> inline float test_precision<std::complex<float> >() { return test_precision<float>(); }
template<> inline double test_precision<std::complex<double> >() { return test_precision<double>(); } template<> inline double test_precision<std::complex<double> >() { return test_precision<double>(); }
inline bool test_isApprox(const int& a, const int& b) inline bool test_ei_isApprox(const int& a, const int& b)
{ return isApprox(a, b, test_precision<int>()); } { return ei_isApprox(a, b, test_precision<int>()); }
inline bool test_isMuchSmallerThan(const int& a, const int& b) inline bool test_ei_isMuchSmallerThan(const int& a, const int& b)
{ return isMuchSmallerThan(a, b, test_precision<int>()); } { return ei_isMuchSmallerThan(a, b, test_precision<int>()); }
inline bool test_isApproxOrLessThan(const int& a, const int& b) inline bool test_ei_isApproxOrLessThan(const int& a, const int& b)
{ return isApproxOrLessThan(a, b, test_precision<int>()); } { return ei_isApproxOrLessThan(a, b, test_precision<int>()); }
inline bool test_isApprox(const float& a, const float& b) inline bool test_ei_isApprox(const float& a, const float& b)
{ return isApprox(a, b, test_precision<float>()); } { return ei_isApprox(a, b, test_precision<float>()); }
inline bool test_isMuchSmallerThan(const float& a, const float& b) inline bool test_ei_isMuchSmallerThan(const float& a, const float& b)
{ return isMuchSmallerThan(a, b, test_precision<float>()); } { return ei_isMuchSmallerThan(a, b, test_precision<float>()); }
inline bool test_isApproxOrLessThan(const float& a, const float& b) inline bool test_ei_isApproxOrLessThan(const float& a, const float& b)
{ return isApproxOrLessThan(a, b, test_precision<float>()); } { return ei_isApproxOrLessThan(a, b, test_precision<float>()); }
inline bool test_isApprox(const double& a, const double& b) inline bool test_ei_isApprox(const double& a, const double& b)
{ return isApprox(a, b, test_precision<double>()); } { return ei_isApprox(a, b, test_precision<double>()); }
inline bool test_isMuchSmallerThan(const double& a, const double& b) inline bool test_ei_isMuchSmallerThan(const double& a, const double& b)
{ return isMuchSmallerThan(a, b, test_precision<double>()); } { return ei_isMuchSmallerThan(a, b, test_precision<double>()); }
inline bool test_isApproxOrLessThan(const double& a, const double& b) inline bool test_ei_isApproxOrLessThan(const double& a, const double& b)
{ return isApproxOrLessThan(a, b, test_precision<double>()); } { return ei_isApproxOrLessThan(a, b, test_precision<double>()); }
inline bool test_isApprox(const std::complex<float>& a, const std::complex<float>& b) inline bool test_ei_isApprox(const std::complex<float>& a, const std::complex<float>& b)
{ return isApprox(a, b, test_precision<std::complex<float> >()); } { return ei_isApprox(a, b, test_precision<std::complex<float> >()); }
inline bool test_isMuchSmallerThan(const std::complex<float>& a, const std::complex<float>& b) inline bool test_ei_isMuchSmallerThan(const std::complex<float>& a, const std::complex<float>& b)
{ return isMuchSmallerThan(a, b, test_precision<std::complex<float> >()); } { return ei_isMuchSmallerThan(a, b, test_precision<std::complex<float> >()); }
inline bool test_isApprox(const std::complex<double>& a, const std::complex<double>& b) inline bool test_ei_isApprox(const std::complex<double>& a, const std::complex<double>& b)
{ return isApprox(a, b, test_precision<std::complex<double> >()); } { return ei_isApprox(a, b, test_precision<std::complex<double> >()); }
inline bool test_isMuchSmallerThan(const std::complex<double>& a, const std::complex<double>& b) inline bool test_ei_isMuchSmallerThan(const std::complex<double>& a, const std::complex<double>& b)
{ return isMuchSmallerThan(a, b, test_precision<std::complex<double> >()); } { return ei_isMuchSmallerThan(a, b, test_precision<std::complex<double> >()); }
template<typename Scalar, typename Derived1, typename Derived2> template<typename Scalar, typename Derived1, typename Derived2>
inline bool test_isApprox(const MatrixBase<Scalar, Derived1>& m1, inline bool test_ei_isApprox(const MatrixBase<Scalar, Derived1>& m1,
const MatrixBase<Scalar, Derived2>& m2) const MatrixBase<Scalar, Derived2>& m2)
{ {
return m1.isApprox(m2, test_precision<Scalar>()); return m1.isApprox(m2, test_precision<Scalar>());
} }
template<typename Scalar, typename Derived1, typename Derived2> template<typename Scalar, typename Derived1, typename Derived2>
inline bool test_isMuchSmallerThan(const MatrixBase<Scalar, Derived1>& m1, inline bool test_ei_isMuchSmallerThan(const MatrixBase<Scalar, Derived1>& m1,
const MatrixBase<Scalar, Derived2>& m2) const MatrixBase<Scalar, Derived2>& m2)
{ {
return m1.isMuchSmallerThan(m2, test_precision<Scalar>()); return m1.isMuchSmallerThan(m2, test_precision<Scalar>());
} }
template<typename Scalar, typename Derived> template<typename Scalar, typename Derived>
inline bool test_isMuchSmallerThan(const MatrixBase<Scalar, Derived>& m, inline bool test_ei_isMuchSmallerThan(const MatrixBase<Scalar, Derived>& m,
const typename NumTraits<Scalar>::Real& s) const typename NumTraits<Scalar>::Real& s)
{ {
return m.isMuchSmallerThan(s, test_precision<Scalar>()); return m.isMuchSmallerThan(s, test_precision<Scalar>());

View File

@ -39,7 +39,7 @@ template<typename MatrixType> void miscMatrices(const MatrixType& m)
int rows = m.rows(); int rows = m.rows();
int cols = m.cols(); int cols = m.cols();
int r = random<int>(0, rows-1), r2 = random<int>(0, rows-1), c = random<int>(0, cols-1); int r = ei_random<int>(0, rows-1), r2 = ei_random<int>(0, rows-1), c = ei_random<int>(0, cols-1);
VERIFY_IS_APPROX(MatrixType::ones(rows,cols)(r,c), static_cast<Scalar>(1)); VERIFY_IS_APPROX(MatrixType::ones(rows,cols)(r,c), static_cast<Scalar>(1));
MatrixType m1 = MatrixType::ones(rows,cols); MatrixType m1 = MatrixType::ones(rows,cols);
VERIFY_IS_APPROX(m1(r,c), static_cast<Scalar>(1)); VERIFY_IS_APPROX(m1(r,c), static_cast<Scalar>(1));

View File

@ -53,10 +53,10 @@ template<typename MatrixType> void product(const MatrixType& m)
v2 = VectorType::random(rows), v2 = VectorType::random(rows),
vzero = VectorType::zero(rows); vzero = VectorType::zero(rows);
Scalar s1 = random<Scalar>(); Scalar s1 = ei_random<Scalar>();
int r = random<int>(0, rows-1), int r = ei_random<int>(0, rows-1),
c = random<int>(0, cols-1); c = ei_random<int>(0, cols-1);
// begin testing Product.h: only associativity for now // begin testing Product.h: only associativity for now
// (we use Transpose.h but this doesn't count as a test for it) // (we use Transpose.h but this doesn't count as a test for it)

View File

@ -32,10 +32,10 @@ template<typename Scalar> void smallVectors()
typedef Matrix<Scalar, 1, 2> V2; typedef Matrix<Scalar, 1, 2> V2;
typedef Matrix<Scalar, 3, 1> V3; typedef Matrix<Scalar, 3, 1> V3;
typedef Matrix<Scalar, 1, 4> V4; typedef Matrix<Scalar, 1, 4> V4;
Scalar x1 = random<Scalar>(), Scalar x1 = ei_random<Scalar>(),
x2 = random<Scalar>(), x2 = ei_random<Scalar>(),
x3 = random<Scalar>(), x3 = ei_random<Scalar>(),
x4 = random<Scalar>(); x4 = ei_random<Scalar>();
V2 v2(x1, x2); V2 v2(x1, x2);
V3 v3(x1, x2, x3); V3 v3(x1, x2, x3);
V4 v4(x1, x2, x3, x4); V4 v4(x1, x2, x3, x4);

View File

@ -52,12 +52,12 @@ template<typename MatrixType> void submatrices(const MatrixType& m)
v3 = VectorType::random(rows), v3 = VectorType::random(rows),
vzero = VectorType::zero(rows); vzero = VectorType::zero(rows);
Scalar s1 = random<Scalar>(); Scalar s1 = ei_random<Scalar>();
int r1 = random<int>(0,rows-1); int r1 = ei_random<int>(0,rows-1);
int r2 = random<int>(r1,rows-1); int r2 = ei_random<int>(r1,rows-1);
int c1 = random<int>(0,cols-1); int c1 = ei_random<int>(0,cols-1);
int c2 = random<int>(c1,cols-1); int c2 = ei_random<int>(c1,cols-1);
//check row() and col() //check row() and col()
VERIFY_IS_APPROX(m1.col(c1).transpose(), m1.transpose().row(c1)); VERIFY_IS_APPROX(m1.col(c1).transpose(), m1.transpose().row(c1));
@ -108,7 +108,7 @@ void EigenTest::testSubmatrices()
// being called as a member of a class that is itself a template parameter // being called as a member of a class that is itself a template parameter
// (at least as of g++ 4.2) // (at least as of g++ 4.2)
Matrix<float, 6, 8> m = Matrix<float, 6, 8>::random(); Matrix<float, 6, 8> m = Matrix<float, 6, 8>::random();
float s = random<float>(); float s = ei_random<float>();
// test fixedBlock() as lvalue // test fixedBlock() as lvalue
m.fixedBlock<2,5>(1,1) *= s; m.fixedBlock<2,5>(1,1) *= s;
// test operator() on fixedBlock() both as constant and non-constant // test operator() on fixedBlock() both as constant and non-constant