simple_continued_fraction.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // (C) Copyright Nick Thompson 2020.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_TOOLS_SIMPLE_CONTINUED_FRACTION_HPP
  6. #define BOOST_MATH_TOOLS_SIMPLE_CONTINUED_FRACTION_HPP
  7. #include <vector>
  8. #include <ostream>
  9. #include <iomanip>
  10. #include <cmath>
  11. #include <limits>
  12. #include <stdexcept>
  13. #include <boost/core/demangle.hpp>
  14. namespace boost::math::tools {
  15. template<typename Real, typename Z = int64_t>
  16. class simple_continued_fraction {
  17. public:
  18. simple_continued_fraction(Real x) : x_{x} {
  19. using std::floor;
  20. using std::abs;
  21. using std::sqrt;
  22. using std::isfinite;
  23. if (!isfinite(x)) {
  24. throw std::domain_error("Cannot convert non-finites into continued fractions.");
  25. }
  26. b_.reserve(50);
  27. Real bj = floor(x);
  28. b_.push_back(static_cast<Z>(bj));
  29. if (bj == x) {
  30. b_.shrink_to_fit();
  31. return;
  32. }
  33. x = 1/(x-bj);
  34. Real f = bj;
  35. if (bj == 0) {
  36. f = 16*std::numeric_limits<Real>::min();
  37. }
  38. Real C = f;
  39. Real D = 0;
  40. int i = 0;
  41. // the "1 + i++" lets the error bound grow slowly with the number of convergents.
  42. // I have not worked out the error propagation of the Modified Lentz's method to see if it does indeed grow at this rate.
  43. // Numerical Recipes claims that no one has worked out the error analysis of the modified Lentz's method.
  44. while (abs(f - x_) >= (1 + i++)*std::numeric_limits<Real>::epsilon()*abs(x_))
  45. {
  46. bj = floor(x);
  47. b_.push_back(static_cast<Z>(bj));
  48. x = 1/(x-bj);
  49. D += bj;
  50. if (D == 0) {
  51. D = 16*std::numeric_limits<Real>::min();
  52. }
  53. C = bj + 1/C;
  54. if (C==0) {
  55. C = 16*std::numeric_limits<Real>::min();
  56. }
  57. D = 1/D;
  58. f *= (C*D);
  59. }
  60. // Deal with non-uniqueness of continued fractions: [a0; a1, ..., an, 1] = a0; a1, ..., an + 1].
  61. // The shorter representation is considered the canonical representation,
  62. // so if we compute a non-canonical representation, change it to canonical:
  63. if (b_.size() > 2 && b_.back() == 1) {
  64. b_[b_.size() - 2] += 1;
  65. b_.resize(b_.size() - 1);
  66. }
  67. b_.shrink_to_fit();
  68. for (size_t i = 1; i < b_.size(); ++i) {
  69. if (b_[i] <= 0) {
  70. std::ostringstream oss;
  71. oss << "Found a negative partial denominator: b[" << i << "] = " << b_[i] << "."
  72. << " This means the integer type '" << boost::core::demangle(typeid(Z).name())
  73. << "' has overflowed and you need to use a wider type,"
  74. << " or there is a bug.";
  75. throw std::overflow_error(oss.str());
  76. }
  77. }
  78. }
  79. Real khinchin_geometric_mean() const {
  80. if (b_.size() == 1) {
  81. return std::numeric_limits<Real>::quiet_NaN();
  82. }
  83. using std::log;
  84. using std::exp;
  85. // Precompute the most probable logarithms. See the Gauss-Kuzmin distribution for details.
  86. // Example: b_i = 1 has probability -log_2(3/4) ≈ .415:
  87. // A random partial denominator has ~80% chance of being in this table:
  88. const std::array<Real, 7> logs{std::numeric_limits<Real>::quiet_NaN(), Real(0), log(static_cast<Real>(2)), log(static_cast<Real>(3)), log(static_cast<Real>(4)), log(static_cast<Real>(5)), log(static_cast<Real>(6))};
  89. Real log_prod = 0;
  90. for (size_t i = 1; i < b_.size(); ++i) {
  91. if (b_[i] < static_cast<Z>(logs.size())) {
  92. log_prod += logs[b_[i]];
  93. }
  94. else
  95. {
  96. log_prod += log(static_cast<Real>(b_[i]));
  97. }
  98. }
  99. log_prod /= (b_.size()-1);
  100. return exp(log_prod);
  101. }
  102. Real khinchin_harmonic_mean() const {
  103. if (b_.size() == 1) {
  104. return std::numeric_limits<Real>::quiet_NaN();
  105. }
  106. Real n = b_.size() - 1;
  107. Real denom = 0;
  108. for (size_t i = 1; i < b_.size(); ++i) {
  109. denom += 1/static_cast<Real>(b_[i]);
  110. }
  111. return n/denom;
  112. }
  113. const std::vector<Z>& partial_denominators() const {
  114. return b_;
  115. }
  116. template<typename T, typename Z2>
  117. friend std::ostream& operator<<(std::ostream& out, simple_continued_fraction<T, Z2>& scf);
  118. private:
  119. const Real x_;
  120. std::vector<Z> b_;
  121. };
  122. template<typename Real, typename Z2>
  123. std::ostream& operator<<(std::ostream& out, simple_continued_fraction<Real, Z2>& scf) {
  124. constexpr const int p = std::numeric_limits<Real>::max_digits10;
  125. if constexpr (p == 2147483647) {
  126. out << std::setprecision(scf.x_.backend().precision());
  127. } else {
  128. out << std::setprecision(p);
  129. }
  130. out << "[" << scf.b_.front();
  131. if (scf.b_.size() > 1)
  132. {
  133. out << "; ";
  134. for (size_t i = 1; i < scf.b_.size() -1; ++i)
  135. {
  136. out << scf.b_[i] << ", ";
  137. }
  138. out << scf.b_.back();
  139. }
  140. out << "]";
  141. return out;
  142. }
  143. }
  144. #endif