/boost/math/tools/centered_continued_fraction.hpp

https://bitbucket.org/bosp/external-boost · C++ Header · 166 lines · 146 code · 15 blank · 5 comment · 24 complexity · 8a049c30bf91e015fb302aa31ee3832a MD5 · raw file

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