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.

204 lines
7.0 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-2012 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. #ifndef EIGEN_MPREALSUPPORT_MODULE_H
  12. #define EIGEN_MPREALSUPPORT_MODULE_H
  13. #include <Eigen/Core>
  14. #include <mpreal.h>
  15. namespace StormEigen {
  16. /**
  17. * \defgroup MPRealSupport_Module MPFRC++ Support module
  18. * \code
  19. * #include <Eigen/MPRealSupport>
  20. * \endcode
  21. *
  22. * This module provides support for multi precision floating point numbers
  23. * via the <a href="http://www.holoborodko.com/pavel/mpfr">MPFR C++</a>
  24. * library which itself is built upon <a href="http://www.mpfr.org/">MPFR</a>/<a href="http://gmplib.org/">GMP</a>.
  25. *
  26. * \warning MPFR C++ is licensed under the GPL.
  27. *
  28. * You can find a copy of MPFR C++ that is known to be compatible in the unsupported/test/mpreal folder.
  29. *
  30. * Here is an example:
  31. *
  32. \code
  33. #include <iostream>
  34. #include <Eigen/MPRealSupport>
  35. #include <Eigen/LU>
  36. using namespace mpfr;
  37. using namespace StormEigen;
  38. int main()
  39. {
  40. // set precision to 256 bits (double has only 53 bits)
  41. mpreal::set_default_prec(256);
  42. // Declare matrix and vector types with multi-precision scalar type
  43. typedef Matrix<mpreal,Dynamic,Dynamic> MatrixXmp;
  44. typedef Matrix<mpreal,Dynamic,1> VectorXmp;
  45. MatrixXmp A = MatrixXmp::Random(100,100);
  46. VectorXmp b = VectorXmp::Random(100);
  47. // Solve Ax=b using LU
  48. VectorXmp x = A.lu().solve(b);
  49. std::cout << "relative error: " << (A*x - b).norm() / b.norm() << std::endl;
  50. return 0;
  51. }
  52. \endcode
  53. *
  54. */
  55. template<> struct NumTraits<mpfr::mpreal>
  56. : GenericNumTraits<mpfr::mpreal>
  57. {
  58. enum {
  59. IsInteger = 0,
  60. IsSigned = 1,
  61. IsComplex = 0,
  62. RequireInitialization = 1,
  63. ReadCost = 10,
  64. AddCost = 10,
  65. MulCost = 40
  66. };
  67. typedef mpfr::mpreal Real;
  68. typedef mpfr::mpreal NonInteger;
  69. inline static Real highest (long Precision = mpfr::mpreal::get_default_prec()) { return mpfr::maxval(Precision); }
  70. inline static Real lowest (long Precision = mpfr::mpreal::get_default_prec()) { return -mpfr::maxval(Precision); }
  71. // Constants
  72. inline static Real Pi (long Precision = mpfr::mpreal::get_default_prec()) { return mpfr::const_pi(Precision); }
  73. inline static Real Euler (long Precision = mpfr::mpreal::get_default_prec()) { return mpfr::const_euler(Precision); }
  74. inline static Real Log2 (long Precision = mpfr::mpreal::get_default_prec()) { return mpfr::const_log2(Precision); }
  75. inline static Real Catalan (long Precision = mpfr::mpreal::get_default_prec()) { return mpfr::const_catalan(Precision); }
  76. inline static Real epsilon (long Precision = mpfr::mpreal::get_default_prec()) { return mpfr::machine_epsilon(Precision); }
  77. inline static Real epsilon (const Real& x) { return mpfr::machine_epsilon(x); }
  78. inline static Real dummy_precision()
  79. {
  80. mpfr_prec_t weak_prec = ((mpfr::mpreal::get_default_prec()-1) * 90) / 100;
  81. return mpfr::machine_epsilon(weak_prec);
  82. }
  83. };
  84. namespace internal {
  85. template<> inline mpfr::mpreal random<mpfr::mpreal>()
  86. {
  87. return mpfr::random();
  88. }
  89. template<> inline mpfr::mpreal random<mpfr::mpreal>(const mpfr::mpreal& a, const mpfr::mpreal& b)
  90. {
  91. return a + (b-a) * random<mpfr::mpreal>();
  92. }
  93. inline bool isMuchSmallerThan(const mpfr::mpreal& a, const mpfr::mpreal& b, const mpfr::mpreal& eps)
  94. {
  95. return mpfr::abs(a) <= mpfr::abs(b) * eps;
  96. }
  97. inline bool isApprox(const mpfr::mpreal& a, const mpfr::mpreal& b, const mpfr::mpreal& eps)
  98. {
  99. return mpfr::isEqualFuzzy(a,b,eps);
  100. }
  101. inline bool isApproxOrLessThan(const mpfr::mpreal& a, const mpfr::mpreal& b, const mpfr::mpreal& eps)
  102. {
  103. return a <= b || mpfr::isEqualFuzzy(a,b,eps);
  104. }
  105. template<> inline long double cast<mpfr::mpreal,long double>(const mpfr::mpreal& x)
  106. { return x.toLDouble(); }
  107. template<> inline double cast<mpfr::mpreal,double>(const mpfr::mpreal& x)
  108. { return x.toDouble(); }
  109. template<> inline long cast<mpfr::mpreal,long>(const mpfr::mpreal& x)
  110. { return x.toLong(); }
  111. template<> inline int cast<mpfr::mpreal,int>(const mpfr::mpreal& x)
  112. { return int(x.toLong()); }
  113. // Specialize GEBP kernel and traits for mpreal (no need for peeling, nor complicated stuff)
  114. // This also permits to directly call mpfr's routines and avoid many temporaries produced by mpreal
  115. template<>
  116. class gebp_traits<mpfr::mpreal, mpfr::mpreal, false, false>
  117. {
  118. public:
  119. typedef mpfr::mpreal ResScalar;
  120. enum {
  121. Vectorizable = false,
  122. LhsPacketSize = 1,
  123. RhsPacketSize = 1,
  124. ResPacketSize = 1,
  125. NumberOfRegisters = 1,
  126. nr = 1,
  127. mr = 1,
  128. LhsProgress = 1,
  129. RhsProgress = 1
  130. };
  131. typedef ResScalar LhsPacket;
  132. typedef ResScalar RhsPacket;
  133. typedef ResScalar ResPacket;
  134. };
  135. template<typename Index, typename DataMapper, bool ConjugateLhs, bool ConjugateRhs>
  136. struct gebp_kernel<mpfr::mpreal,mpfr::mpreal,Index,DataMapper,1,1,ConjugateLhs,ConjugateRhs>
  137. {
  138. typedef mpfr::mpreal mpreal;
  139. EIGEN_DONT_INLINE
  140. void operator()(const DataMapper& res, const mpreal* blockA, const mpreal* blockB,
  141. Index rows, Index depth, Index cols, const mpreal& alpha,
  142. Index strideA=-1, Index strideB=-1, Index offsetA=0, Index offsetB=0)
  143. {
  144. if(rows==0 || cols==0 || depth==0)
  145. return;
  146. mpreal acc1(0,mpfr_get_prec(blockA[0].mpfr_srcptr())),
  147. tmp (0,mpfr_get_prec(blockA[0].mpfr_srcptr()));
  148. if(strideA==-1) strideA = depth;
  149. if(strideB==-1) strideB = depth;
  150. for(Index i=0; i<rows; ++i)
  151. {
  152. for(Index j=0; j<cols; ++j)
  153. {
  154. const mpreal *A = blockA + i*strideA + offsetA;
  155. const mpreal *B = blockB + j*strideB + offsetB;
  156. acc1 = 0;
  157. for(Index k=0; k<depth; k++)
  158. {
  159. mpfr_mul(tmp.mpfr_ptr(), A[k].mpfr_srcptr(), B[k].mpfr_srcptr(), mpreal::get_default_rnd());
  160. mpfr_add(acc1.mpfr_ptr(), acc1.mpfr_ptr(), tmp.mpfr_ptr(), mpreal::get_default_rnd());
  161. }
  162. mpfr_mul(acc1.mpfr_ptr(), acc1.mpfr_srcptr(), alpha.mpfr_srcptr(), mpreal::get_default_rnd());
  163. mpfr_add(res(i,j).mpfr_ptr(), res(i,j).mpfr_srcptr(), acc1.mpfr_srcptr(), mpreal::get_default_rnd());
  164. }
  165. }
  166. }
  167. };
  168. } // end namespace internal
  169. }
  170. #endif // EIGEN_MPREALSUPPORT_MODULE_H