centered_continued_fraction.hpp 5.0 KB

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