luroth_expansion.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_LUROTH_EXPANSION_HPP
  6. #define BOOST_MATH_TOOLS_LUROTH_EXPANSION_HPP
  7. #include <vector>
  8. #include <ostream>
  9. #include <iomanip>
  10. #include <cmath>
  11. #include <limits>
  12. #include <stdexcept>
  13. #include <boost/math/tools/is_standalone.hpp>
  14. #ifndef BOOST_MATH_STANDALONE
  15. #include <boost/config.hpp>
  16. #ifdef BOOST_NO_CXX17_IF_CONSTEXPR
  17. #error "The header <boost/math/norms.hpp> can only be used in C++17 and later."
  18. #endif
  19. #endif
  20. namespace boost::math::tools {
  21. template<typename Real, typename Z = int64_t>
  22. class luroth_expansion {
  23. public:
  24. luroth_expansion(Real x) : x_{x}
  25. {
  26. using std::floor;
  27. using std::abs;
  28. using std::sqrt;
  29. using std::isfinite;
  30. if (!isfinite(x))
  31. {
  32. throw std::domain_error("Cannot convert non-finites into a Luroth representation.");
  33. }
  34. d_.reserve(50);
  35. Real dn1 = floor(x);
  36. d_.push_back(static_cast<Z>(dn1));
  37. if (dn1 == x)
  38. {
  39. d_.shrink_to_fit();
  40. return;
  41. }
  42. // This attempts to follow the notation of:
  43. // "Khinchine's constant for Luroth Representation", by Sophia Kalpazidou.
  44. x = x - dn1;
  45. Real computed = dn1;
  46. Real prod = 1;
  47. // Let the error bound grow by 1 ULP/iteration.
  48. // I haven't done the error analysis to show that this is an expected rate of error growth,
  49. // but if you don't do this, you can easily get into an infinite loop.
  50. Real i = 1;
  51. Real scale = std::numeric_limits<Real>::epsilon()*abs(x_)/2;
  52. while (abs(x_ - computed) > (i++)*scale)
  53. {
  54. Real recip = 1/x;
  55. Real dn = floor(recip);
  56. // x = n + 1/k => lur(x) = ((n; k - 1))
  57. // Note that this is a bit different than Kalpazidou (examine the half-open interval of definition carefully).
  58. // One way to examine this definition is better for rationals (it never happens for irrationals)
  59. // is to consider i + 1/3. If you follow Kalpazidou, then you get ((i, 3, 0)); a zero digit!
  60. // That's bad since it destroys uniqueness and also breaks the computation of the geometric mean.
  61. if (recip == dn) {
  62. d_.push_back(static_cast<Z>(dn - 1));
  63. break;
  64. }
  65. d_.push_back(static_cast<Z>(dn));
  66. Real tmp = 1/(dn+1);
  67. computed += prod*tmp;
  68. prod *= tmp/dn;
  69. x = dn*(dn+1)*(x - tmp);
  70. }
  71. for (size_t i = 1; i < d_.size(); ++i)
  72. {
  73. // Sanity check:
  74. if (d_[i] <= 0)
  75. {
  76. throw std::domain_error("Found a digit <= 0; this is an error.");
  77. }
  78. }
  79. d_.shrink_to_fit();
  80. }
  81. const std::vector<Z>& digits() const {
  82. return d_;
  83. }
  84. // Under the assumption of 'randomness', this mean converges to 2.2001610580.
  85. // See Finch, Mathematical Constants, section 1.8.1.
  86. Real digit_geometric_mean() const {
  87. if (d_.size() == 1) {
  88. return std::numeric_limits<Real>::quiet_NaN();
  89. }
  90. using std::log;
  91. using std::exp;
  92. Real g = 0;
  93. for (size_t i = 1; i < d_.size(); ++i) {
  94. g += log(static_cast<Real>(d_[i]));
  95. }
  96. return exp(g/(d_.size() - 1));
  97. }
  98. template<typename T, typename Z2>
  99. friend std::ostream& operator<<(std::ostream& out, luroth_expansion<T, Z2>& scf);
  100. private:
  101. const Real x_;
  102. std::vector<Z> d_;
  103. };
  104. template<typename Real, typename Z2>
  105. std::ostream& operator<<(std::ostream& out, luroth_expansion<Real, Z2>& luroth)
  106. {
  107. constexpr const int p = std::numeric_limits<Real>::max_digits10;
  108. if constexpr (p == 2147483647)
  109. {
  110. out << std::setprecision(luroth.x_.backend().precision());
  111. }
  112. else
  113. {
  114. out << std::setprecision(p);
  115. }
  116. out << "((" << luroth.d_.front();
  117. if (luroth.d_.size() > 1)
  118. {
  119. out << "; ";
  120. for (size_t i = 1; i < luroth.d_.size() -1; ++i)
  121. {
  122. out << luroth.d_[i] << ", ";
  123. }
  124. out << luroth.d_.back();
  125. }
  126. out << "))";
  127. return out;
  128. }
  129. }
  130. #endif