You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.0 KiB

  1. #include "main.h"
  2. #include <Eigen/MPRealSupport>
  3. #include <Eigen/LU>
  4. #include <Eigen/Eigenvalues>
  5. #include <sstream>
  6. using namespace mpfr;
  7. using namespace std;
  8. using namespace Eigen;
  9. void test_mpreal_support()
  10. {
  11. // set precision to 256 bits (double has only 53 bits)
  12. mpreal::set_default_prec(256);
  13. typedef Matrix<mpreal,Eigen::Dynamic,Eigen::Dynamic> MatrixXmp;
  14. std::cerr << "epsilon = " << NumTraits<mpreal>::epsilon() << "\n";
  15. std::cerr << "dummy_precision = " << NumTraits<mpreal>::dummy_precision() << "\n";
  16. std::cerr << "highest = " << NumTraits<mpreal>::highest() << "\n";
  17. std::cerr << "lowest = " << NumTraits<mpreal>::lowest() << "\n";
  18. for(int i = 0; i < g_repeat; i++) {
  19. int s = Eigen::internal::random<int>(1,100);
  20. MatrixXmp A = MatrixXmp::Random(s,s);
  21. MatrixXmp B = MatrixXmp::Random(s,s);
  22. MatrixXmp S = A.adjoint() * A;
  23. MatrixXmp X;
  24. // Basic stuffs
  25. VERIFY_IS_APPROX(A.real(), A);
  26. VERIFY(Eigen::internal::isApprox(A.array().abs2().sum(), A.squaredNorm()));
  27. VERIFY_IS_APPROX(A.array().exp(), exp(A.array()));
  28. VERIFY_IS_APPROX(A.array().abs2().sqrt(), A.array().abs());
  29. VERIFY_IS_APPROX(A.array().sin(), sin(A.array()));
  30. VERIFY_IS_APPROX(A.array().cos(), cos(A.array()));
  31. // Cholesky
  32. X = S.selfadjointView<Lower>().llt().solve(B);
  33. VERIFY_IS_APPROX((S.selfadjointView<Lower>()*X).eval(),B);
  34. // partial LU
  35. X = A.lu().solve(B);
  36. VERIFY_IS_APPROX((A*X).eval(),B);
  37. // symmetric eigenvalues
  38. SelfAdjointEigenSolver<MatrixXmp> eig(S);
  39. VERIFY_IS_EQUAL(eig.info(), Success);
  40. VERIFY_IS_APPROX((S.selfadjointView<Lower>() * eig.eigenvectors()),
  41. eig.eigenvectors() * eig.eigenvalues().asDiagonal());
  42. }
  43. {
  44. MatrixXmp A(8,3); A.setRandom();
  45. // test output (interesting things happen in this code)
  46. std::stringstream stream;
  47. stream << A;
  48. }
  49. }
  50. extern "C" {
  51. #include "mpreal/dlmalloc.c"
  52. }
  53. #include "mpreal/mpreal.cpp"