# Ticket #8017: 8017-contfrac-referee2.patch

File 8017-contfrac-referee2.patch, 5.7 KB (added by robertwb, 3 years ago)
• ## sage/combinat/words/word_generators.py

# HG changeset patch
# Date 1280427006 25200
# Node ID cee8274a2fb31b43ed5cc564ae43ce6f57cdd8a8
# Parent  f1a647b0d257aa5bb01ccf38935fff6d706e04a0
#8017 - doctest fixes for more accurate continued fractions

diff -r f1a647b0d257 -r cee8274a2fb3 sage/combinat/words/word_generators.py
 a sage: words.CharacteristicSturmianWord(1/golden_ratio^2, bits=30) word: 0100101001001010010100100101001001010010... sage: _.length() 28657 6765 ::
• ## sage/rings/contfrac.py

diff -r f1a647b0d257 -r cee8274a2fb3 sage/rings/contfrac.py
 a def continued_fraction(x, bits=None, nterms=None): """ Return the truncated continued fraction expansion of the real number `x`, computed with a floating point approximation of `x` to the given number of bits of precision. The returned continued `x`, computed with an interval floating point approximation of `x` to the given number of bits of precision. The returned continued fraction is a list-like object, with a value method and partial convergents method. If bits is not given, then use the number of valid bits of precision of `x`, if `x` is a floating point number, or 53 bits otherwise. If nterms is given, the precision is increased until the specified number of terms can be computed. the specified number of terms can be computed, if possible. INPUT: [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8] sage: continued_fraction(RealField(200)(e)) [2, 1, 2, 1, 1, 4, 1, 1, 6, ...36, 1, 1, 38, 1, 1] Initial rounding can result in incorrect trailing digits:: sage: continued_fraction(RealField(39)(e)) [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 2] sage: continued_fraction(RealIntervalField(39)(e)) [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10] """ return CFF(x, bits=bits, nterms=nterms)
• ## sage/tests/book_stein_ent.py

diff -r f1a647b0d257 -r cee8274a2fb3 sage/tests/book_stein_ent.py
 a 303 sage: find_sqrt(5,389)         # see, it's random 86 # Several of the examples below had to be changed due to improved # behavior of the continued_fraction function #8017. sage: continued_fraction(17/23) [0, 1, 2, 1, 5] sage: reset('e') sage: continued_fraction(e) [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1, 11] sage: continued_fraction(e, bits=20) [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1] sage: continued_fraction(e, bits=21) [2, 1, 2, 1, 1, 4, 1, 1, 6] sage: continued_fraction(e, bits=30) [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1] sage: continued_fraction(e, bits=30) [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8] sage: a = continued_fraction(17/23); a [0, 1, 2, 1, 5] sage: a.value() [0, 3, 1, 5] sage: a + b [1] sage: c = continued_fraction(pi,bits=33); c [3, 7, 15, 1, 292, 2] sage: c = continued_fraction(pi,bits=35); c [3, 7, 15, 1, 292, 1] sage: c.convergents() [3, 22/7, 333/106, 355/113, 103993/33102, 208341/66317] [3, 22/7, 333/106, 355/113, 103993/33102, 104348/33215] sage: c = continued_fraction(pi); c [3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 3] [3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14] sage: for n in range(-1, len(c)): ...    print c.pn(n)*c.qn(n-1) - c.qn(n)*c.pn(n-1), 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 sage: for n in range(len(c)): ...    print c.pn(n)*c.qn(n-2) - c.qn(n)*c.pn(n-2), 3 -7 15 -1 292 -1 1 -1 2 -1 3 -1 14 -3 3 -7 15 -1 292 -1 1 -1 2 -1 3 -1 14 sage: c = continued_fraction([1,2,3,4,5]) sage: c.convergents() [1, 3/2, 10/7, 43/30, 225/157] ...   x = (1 + sqrt(RealField(bits)(5))) / 2 ...   return continued_fraction(x) sage: cf(10) [1, 1, 1, 1, 1, 1, 1, 3] [1, 1, 1, 1, 1, 1, 1] sage: cf(30) [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] sage: cf(50) [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] sage: def cf_sqrt_d(d, bits): ...   x = sqrt(RealField(bits)(d)) ...   return continued_fraction(x) sage: cf_sqrt_d(389,50) [19, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38] sage: cf_sqrt_d(389,100) [19, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1] sage: cf_sqrt_d(389,50) [19, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38, 2] sage: cf_sqrt_d(389,100) [19, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1] sage: def newton_root(f, iterates=2, x0=0, prec=53): ...    x = RealField(prec)(x0) ...    R = PolynomialRing(ZZ,'x') sage: a = newton_root(3847*x^2 - 14808904*x + 36527265); a 2.46815700480740 sage: cf = continued_fraction(a); cf [2, 2, 7, 2, 1, 5, 1, 1, 1, 1, 1, 1, 103, 8, 1, 2, 3, 1, 1] [2, 2, 7, 2, 1, 5, 1, 1, 1, 1, 1, 1, 103, 8, 1, 2, 3] sage: c = cf[:12]; c [2, 2, 7, 2, 1, 5, 1, 1, 1, 1, 1, 1] sage: c.value()