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.

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