simple_continued_fraction.hpp 5.6 KB

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