From b547ac7ed05bbeb134911685003a232f76ca0c62 Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Tue, 4 Apr 2023 15:09:27 +0200 Subject: [PATCH] Update docs --- doc/internals/simple_continued_fraction.qbk | 42 ++++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/doc/internals/simple_continued_fraction.qbk b/doc/internals/simple_continued_fraction.qbk index 45896ffd74..e99b6eaefe 100644 --- a/doc/internals/simple_continued_fraction.qbk +++ b/doc/internals/simple_continued_fraction.qbk @@ -1,5 +1,6 @@ [/ Copyright Nick Thompson, 2020 + Copyright Matt Borland, 2023 Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). @@ -19,9 +20,17 @@ Real khinchin_harmonic_mean() const; - template - friend std::ostream& operator<<(std::ostream& out, simple_continued_fraction& scf); + const std::vector& partial_denominators() const; + + inline std::vector&& get_data() noexcept; + + template + friend std::ostream& operator<<(std::ostream& out, simple_continued_fraction& scf); }; + + template + inline std::vector simple_continued_fraction_coefficients(Real x); + } @@ -47,6 +56,35 @@ This is because when examining known values like π, it creates a large number o It may be the case the a few incorrect partial convergents is harmless, but we compute continued fractions because we would like to do something with them. One sensible thing to do it to ask whether the number is in some sense "random"; a question that can be partially answered by computing the Khinchin geometric mean +If you only require the coefficients of the simple continued fraction for example in the calculation of [@https://en.wikipedia.org/wiki/Continued_fraction#Best_rational_approximations best rational approximations] there is a free function for that. + +An example of this calculation follows: + + using boost::math::tools::simple_continued_fraction_coefficients; + + auto coefs1 = simple_continued_fraction_coefficients(static_cast(3.14155L)); // [3; 7, 15, 2, 7, 1, 4, 2] + auto coefs2 = simple_continued_fraction_coefficients(static_cast(3.14165L)); // [3; 7, 16, 1, 3, 4, 2, 4] + + const std::size_t max_size = (std::min)(coefs1.size(), coefs2.size()); + std::vector coefs; + coefs.reserve(max_size); + + for (std::size_t i = 0; i < max_size; ++i) + { + const auto c1 = coefs1[i]; + const auto c2 = coefs2[i]; + if (c1 == c2) + { + coefs.emplace_back(c1); + continue; + } + + coefs.emplace_back((std::min)(c1, c2) + 1); + break; + } + + // Result is [3; 7, 16] + [$../equations/khinchin_geometric.svg] and Khinchin harmonic mean