MPreal: extended unit test, remove useless internal overloads, add support for internal::cast (needed for printing)

This commit is contained in:
Gael Guennebaud 2012-06-21 10:02:32 +02:00
parent 7380592bc2
commit 5b5f3ecafa
2 changed files with 28 additions and 14 deletions

View File

@ -107,7 +107,7 @@ int main()
} }
}; };
namespace internal { namespace internal {
template<> mpfr::mpreal random<mpfr::mpreal>() template<> mpfr::mpreal random<mpfr::mpreal>()
{ {
@ -133,18 +133,6 @@ int main()
return a + (b-a) * random<mpfr::mpreal>(); return a + (b-a) * random<mpfr::mpreal>();
} }
template<> struct conj_impl<mpfr::mpreal> { inline static const mpfr::mpreal& run(const mpfr::mpreal& x) { return x; } };
template<> struct real_impl<mpfr::mpreal> { inline static const mpfr::mpreal& run(const mpfr::mpreal& x) { return x; } };
template<> struct imag_impl<mpfr::mpreal> { inline static const mpfr::mpreal run(const mpfr::mpreal&) { return mpfr::mpreal(0); } };
template<> struct abs_impl<mpfr::mpreal> { inline static const mpfr::mpreal run(const mpfr::mpreal& x) { return mpfr::fabs(x); } };
template<> struct abs2_impl<mpfr::mpreal> { inline static const mpfr::mpreal run(const mpfr::mpreal& x) { return x*x; } };
template<> struct sqrt_impl<mpfr::mpreal> { inline static const mpfr::mpreal run(const mpfr::mpreal& x) { return mpfr::sqrt(x); } };
template<> struct exp_impl<mpfr::mpreal> { inline static const mpfr::mpreal run(const mpfr::mpreal& x) { return mpfr::exp(x); } };
template<> struct log_impl<mpfr::mpreal> { inline static const mpfr::mpreal run(const mpfr::mpreal& x) { return mpfr::log(x); } };
template<> struct sin_impl<mpfr::mpreal> { inline static const mpfr::mpreal run(const mpfr::mpreal& x) { return mpfr::sin(x); } };
template<> struct cos_impl<mpfr::mpreal> { inline static const mpfr::mpreal run(const mpfr::mpreal& x) { return mpfr::cos(x); } };
template<> struct pow_impl<mpfr::mpreal> { inline static const mpfr::mpreal run(const mpfr::mpreal& x, const mpfr::mpreal& y) { return mpfr::pow(x, y); } };
bool isMuchSmallerThan(const mpfr::mpreal& a, const mpfr::mpreal& b, const mpfr::mpreal& prec) bool isMuchSmallerThan(const mpfr::mpreal& a, const mpfr::mpreal& b, const mpfr::mpreal& prec)
{ {
return mpfr::abs(a) <= mpfr::abs(b) * prec; return mpfr::abs(a) <= mpfr::abs(b) * prec;
@ -159,8 +147,17 @@ int main()
{ {
return a <= b || isApprox(a, b, prec); return a <= b || isApprox(a, b, prec);
} }
template<> inline long double cast<mpfr::mpreal,long double>(const mpfr::mpreal& x)
{ return x.toLDouble(); }
template<> inline double cast<mpfr::mpreal,double>(const mpfr::mpreal& x)
{ return x.toDouble(); }
template<> inline long cast<mpfr::mpreal,long>(const mpfr::mpreal& x)
{ return x.toLong(); }
template<> inline int cast<mpfr::mpreal,int>(const mpfr::mpreal& x)
{ return int(x.toLong()); }
} // end namespace internal } // end namespace internal
} }
#endif // EIGEN_MPREALSUPPORT_MODULE_H #endif // EIGEN_MPREALSUPPORT_MODULE_H

View File

@ -2,6 +2,7 @@
#include <Eigen/MPRealSupport> #include <Eigen/MPRealSupport>
#include <Eigen/LU> #include <Eigen/LU>
#include <Eigen/Eigenvalues> #include <Eigen/Eigenvalues>
#include <sstream>
using namespace mpfr; using namespace mpfr;
using namespace std; using namespace std;
@ -24,6 +25,15 @@ void test_mpreal_support()
MatrixXmp B = MatrixXmp::Random(s,s); MatrixXmp B = MatrixXmp::Random(s,s);
MatrixXmp S = A.adjoint() * A; MatrixXmp S = A.adjoint() * A;
MatrixXmp X; MatrixXmp X;
// Basic stuffs
VERIFY_IS_APPROX(A.real(), A);
VERIFY(Eigen::internal::isApprox(A.array().abs2().sum(), A.squaredNorm()));
VERIFY_IS_APPROX(A.array().exp(), exp(A.array()));
VERIFY_IS_APPROX(A.array().abs2().sqrt(), A.array().abs());
VERIFY_IS_APPROX(A.array().sin(), sin(A.array()));
VERIFY_IS_APPROX(A.array().cos(), cos(A.array()));
// Cholesky // Cholesky
X = S.selfadjointView<Lower>().llt().solve(B); X = S.selfadjointView<Lower>().llt().solve(B);
@ -39,6 +49,13 @@ void test_mpreal_support()
VERIFY_IS_APPROX((S.selfadjointView<Lower>() * eig.eigenvectors()), VERIFY_IS_APPROX((S.selfadjointView<Lower>() * eig.eigenvectors()),
eig.eigenvectors() * eig.eigenvalues().asDiagonal()); eig.eigenvectors() * eig.eigenvalues().asDiagonal());
} }
{
MatrixXmp A(8,3); A.setRandom();
// test output (interesting things happen in this code)
std::stringstream stream;
stream << A;
}
} }
extern "C" { extern "C" {