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.

148 lines
4.4 KiB

  1. // This file is part of a joint effort between Eigen, a lightweight C++ template library
  2. // for linear algebra, and MPFR C++, a C++ interface to MPFR library (http://www.holoborodko.com/pavel/)
  3. //
  4. // Copyright (C) 2010 Pavel Holoborodko <pavel@holoborodko.com>
  5. // Copyright (C) 2010 Konstantin Holoborodko <konstantin@holoborodko.com>
  6. // Copyright (C) 2010 Gael Guennebaud <gael.guennebaud@inria.fr>
  7. //
  8. // This Source Code Form is subject to the terms of the Mozilla
  9. // Public License v. 2.0. If a copy of the MPL was not distributed
  10. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  11. //
  12. // Contributors:
  13. // Brian Gladman, Helmut Jarausch, Fokko Beekhof, Ulrich Mutze, Heinz van Saanen, Pere Constans
  14. #ifndef EIGEN_MPREALSUPPORT_MODULE_H
  15. #define EIGEN_MPREALSUPPORT_MODULE_H
  16. #include <ctime>
  17. #include <mpreal.h>
  18. #include <Eigen/Core>
  19. namespace Eigen {
  20. /** \ingroup Unsupported_modules
  21. * \defgroup MPRealSupport_Module MPFRC++ Support module
  22. *
  23. * \code
  24. * #include <Eigen/MPRealSupport>
  25. * \endcode
  26. *
  27. * This module provides support for multi precision floating point numbers
  28. * via the <a href="http://www.holoborodko.com/pavel/mpfr">MPFR C++</a>
  29. * library which itself is built upon <a href="http://www.mpfr.org/">MPFR</a>/<a href="http://gmplib.org/">GMP</a>.
  30. *
  31. * You can find a copy of MPFR C++ that is known to be compatible in the unsupported/test/mpreal folder.
  32. *
  33. * Here is an example:
  34. *
  35. \code
  36. #include <iostream>
  37. #include <Eigen/MPRealSupport>
  38. #include <Eigen/LU>
  39. using namespace mpfr;
  40. using namespace Eigen;
  41. int main()
  42. {
  43. // set precision to 256 bits (double has only 53 bits)
  44. mpreal::set_default_prec(256);
  45. // Declare matrix and vector types with multi-precision scalar type
  46. typedef Matrix<mpreal,Dynamic,Dynamic> MatrixXmp;
  47. typedef Matrix<mpreal,Dynamic,1> VectorXmp;
  48. MatrixXmp A = MatrixXmp::Random(100,100);
  49. VectorXmp b = VectorXmp::Random(100);
  50. // Solve Ax=b using LU
  51. VectorXmp x = A.lu().solve(b);
  52. std::cout << "relative error: " << (A*x - b).norm() / b.norm() << std::endl;
  53. return 0;
  54. }
  55. \endcode
  56. *
  57. */
  58. template<> struct NumTraits<mpfr::mpreal>
  59. : GenericNumTraits<mpfr::mpreal>
  60. {
  61. enum {
  62. IsInteger = 0,
  63. IsSigned = 1,
  64. IsComplex = 0,
  65. RequireInitialization = 1,
  66. ReadCost = 10,
  67. AddCost = 10,
  68. MulCost = 40
  69. };
  70. typedef mpfr::mpreal Real;
  71. typedef mpfr::mpreal NonInteger;
  72. inline static mpfr::mpreal highest() { return mpfr::mpreal_max(mpfr::mpreal::get_default_prec()); }
  73. inline static mpfr::mpreal lowest() { return -mpfr::mpreal_max(mpfr::mpreal::get_default_prec()); }
  74. inline static Real epsilon()
  75. {
  76. return mpfr::machine_epsilon(mpfr::mpreal::get_default_prec());
  77. }
  78. inline static Real dummy_precision()
  79. {
  80. unsigned int weak_prec = ((mpfr::mpreal::get_default_prec()-1)*90)/100;
  81. return mpfr::machine_epsilon(weak_prec);
  82. }
  83. };
  84. namespace internal {
  85. template<> mpfr::mpreal random<mpfr::mpreal>()
  86. {
  87. #if (MPFR_VERSION >= MPFR_VERSION_NUM(3,0,0))
  88. static gmp_randstate_t state;
  89. static bool isFirstTime = true;
  90. if(isFirstTime)
  91. {
  92. gmp_randinit_default(state);
  93. gmp_randseed_ui(state,(unsigned)time(NULL));
  94. isFirstTime = false;
  95. }
  96. return mpfr::urandom(state)*2-1;
  97. #else
  98. return mpfr::mpreal(random<double>());
  99. #endif
  100. }
  101. template<> mpfr::mpreal random<mpfr::mpreal>(const mpfr::mpreal& a, const mpfr::mpreal& b)
  102. {
  103. return a + (b-a) * random<mpfr::mpreal>();
  104. }
  105. bool isMuchSmallerThan(const mpfr::mpreal& a, const mpfr::mpreal& b, const mpfr::mpreal& prec)
  106. {
  107. return mpfr::abs(a) <= mpfr::abs(b) * prec;
  108. }
  109. inline bool isApprox(const mpfr::mpreal& a, const mpfr::mpreal& b, const mpfr::mpreal& prec)
  110. {
  111. return mpfr::abs(a - b) <= (mpfr::min)(mpfr::abs(a), mpfr::abs(b)) * prec;
  112. }
  113. inline bool isApproxOrLessThan(const mpfr::mpreal& a, const mpfr::mpreal& b, const mpfr::mpreal& prec)
  114. {
  115. return a <= b || isApprox(a, b, prec);
  116. }
  117. template<> inline long double cast<mpfr::mpreal,long double>(const mpfr::mpreal& x)
  118. { return x.toLDouble(); }
  119. template<> inline double cast<mpfr::mpreal,double>(const mpfr::mpreal& x)
  120. { return x.toDouble(); }
  121. template<> inline long cast<mpfr::mpreal,long>(const mpfr::mpreal& x)
  122. { return x.toLong(); }
  123. template<> inline int cast<mpfr::mpreal,int>(const mpfr::mpreal& x)
  124. { return int(x.toLong()); }
  125. } // end namespace internal
  126. }
  127. #endif // EIGEN_MPREALSUPPORT_MODULE_H