centered_continued_fraction.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_CENTERED_CONTINUED_FRACTION_HPP
  6. #define BOOST_MATH_TOOLS_CENTERED_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 centered_continued_fraction {
  17. public:
  18. centered_continued_fraction(Real x) : x_{x} {
  19. static_assert(std::is_integral_v<Z> && std::is_signed_v<Z>,
  20. "Centered continued fractions require signed integer types.");
  21. using std::round;
  22. using std::abs;
  23. using std::sqrt;
  24. using std::isfinite;
  25. if (!isfinite(x))
  26. {
  27. throw std::domain_error("Cannot convert non-finites into continued fractions.");
  28. }
  29. b_.reserve(50);
  30. Real bj = round(x);
  31. b_.push_back(static_cast<Z>(bj));
  32. if (bj == x)
  33. {
  34. b_.shrink_to_fit();
  35. return;
  36. }
  37. x = 1/(x-bj);
  38. Real f = bj;
  39. if (bj == 0)
  40. {
  41. f = 16*std::numeric_limits<Real>::min();
  42. }
  43. Real C = f;
  44. Real D = 0;
  45. int i = 0;
  46. while (abs(f - x_) >= (1 + i++)*std::numeric_limits<Real>::epsilon()*abs(x_))
  47. {
  48. bj = round(x);
  49. b_.push_back(static_cast<Z>(bj));
  50. x = 1/(x-bj);
  51. D += bj;
  52. if (D == 0) {
  53. D = 16*std::numeric_limits<Real>::min();
  54. }
  55. C = bj + 1/C;
  56. if (C==0)
  57. {
  58. C = 16*std::numeric_limits<Real>::min();
  59. }
  60. D = 1/D;
  61. f *= (C*D);
  62. }
  63. // Deal with non-uniqueness of continued fractions: [a0; a1, ..., an, 1] = a0; a1, ..., an + 1].
  64. if (b_.size() > 2 && b_.back() == 1)
  65. {
  66. b_[b_.size() - 2] += 1;
  67. b_.resize(b_.size() - 1);
  68. }
  69. b_.shrink_to_fit();
  70. for (size_t i = 1; i < b_.size(); ++i)
  71. {
  72. if (b_[i] == 0) {
  73. std::ostringstream oss;
  74. oss << "Found a zero partial denominator: b[" << i << "] = " << b_[i] << "."
  75. << " This means the integer type '" << boost::core::demangle(typeid(Z).name())
  76. << "' has overflowed and you need to use a wider type,"
  77. << " or there is a bug.";
  78. throw std::overflow_error(oss.str());
  79. }
  80. }
  81. }
  82. Real khinchin_geometric_mean() const {
  83. if (b_.size() == 1)
  84. {
  85. return std::numeric_limits<Real>::quiet_NaN();
  86. }
  87. using std::log;
  88. using std::exp;
  89. using std::abs;
  90. 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))};
  91. Real log_prod = 0;
  92. for (size_t i = 1; i < b_.size(); ++i)
  93. {
  94. if (abs(b_[i]) < static_cast<Z>(logs.size()))
  95. {
  96. log_prod += logs[abs(b_[i])];
  97. }
  98. else
  99. {
  100. log_prod += log(static_cast<Real>(abs(b_[i])));
  101. }
  102. }
  103. log_prod /= (b_.size()-1);
  104. return exp(log_prod);
  105. }
  106. const std::vector<Z>& partial_denominators() const {
  107. return b_;
  108. }
  109. template<typename T, typename Z2>
  110. friend std::ostream& operator<<(std::ostream& out, centered_continued_fraction<T, Z2>& ccf);
  111. private:
  112. const Real x_;
  113. std::vector<Z> b_;
  114. };
  115. template<typename Real, typename Z2>
  116. std::ostream& operator<<(std::ostream& out, centered_continued_fraction<Real, Z2>& scf) {
  117. constexpr const int p = std::numeric_limits<Real>::max_digits10;
  118. if constexpr (p == 2147483647)
  119. {
  120. out << std::setprecision(scf.x_.backend().precision());
  121. }
  122. else
  123. {
  124. out << std::setprecision(p);
  125. }
  126. out << "[" << scf.b_.front();
  127. if (scf.b_.size() > 1)
  128. {
  129. out << "; ";
  130. for (size_t i = 1; i < scf.b_.size() -1; ++i)
  131. {
  132. out << scf.b_[i] << ", ";
  133. }
  134. out << scf.b_.back();
  135. }
  136. out << "]";
  137. return out;
  138. }
  139. }
  140. #endif