luroth_expansion.hpp 4.0 KB

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