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 # User Robert Bradshaw <robertwb@math.washington.edu> # 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 b 830 830 sage: words.CharacteristicSturmianWord(1/golden_ratio^2, bits=30) 831 831 word: 0100101001001010010100100101001001010010... 832 832 sage: _.length() 833 28657833 6765 834 834 835 835 :: 836 836 -
sage/rings/contfrac.py
diff -r f1a647b0d257 -r cee8274a2fb3 sage/rings/contfrac.py
a b 963 963 def continued_fraction(x, bits=None, nterms=None): 964 964 """ 965 965 Return the truncated continued fraction expansion of the real number 966 `x`, computed with a floating point approximation of `x` to the967 given number of bits of precision.The returned continued966 `x`, computed with an interval floating point approximation of `x` 967 to the given number of bits of precision. The returned continued 968 968 fraction is a list-like object, with a value method and partial 969 969 convergents method. 970 970 971 971 If bits is not given, then use the number of valid bits of 972 972 precision of `x`, if `x` is a floating point number, or 53 bits 973 973 otherwise. If nterms is given, the precision is increased until 974 the specified number of terms can be computed .974 the specified number of terms can be computed, if possible. 975 975 976 976 INPUT: 977 977 … … 1022 1022 [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8] 1023 1023 sage: continued_fraction(RealField(200)(e)) 1024 1024 [2, 1, 2, 1, 1, 4, 1, 1, 6, ...36, 1, 1, 38, 1, 1] 1025 1025 1026 Initial rounding can result in incorrect trailing digits:: 1027 1028 sage: continued_fraction(RealField(39)(e)) 1029 [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 2] 1030 sage: continued_fraction(RealIntervalField(39)(e)) 1031 [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10] 1026 1032 """ 1027 1033 return CFF(x, bits=bits, nterms=nterms) 1028 1034 -
sage/tests/book_stein_ent.py
diff -r f1a647b0d257 -r cee8274a2fb3 sage/tests/book_stein_ent.py
a b 387 387 303 388 388 sage: find_sqrt(5,389) # see, it's random 389 389 86 390 391 # Several of the examples below had to be changed due to improved 392 # behavior of the continued_fraction function #8017. 393 390 394 sage: continued_fraction(17/23) 391 395 [0, 1, 2, 1, 5] 392 396 sage: reset('e') 393 397 sage: continued_fraction(e) 394 [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1 , 11]395 sage: continued_fraction(e, bits=2 0)398 [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1] 399 sage: continued_fraction(e, bits=21) 396 400 [2, 1, 2, 1, 1, 4, 1, 1, 6] 397 sage: continued_fraction(e, bits=30) 398 [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8 , 1, 1]401 sage: continued_fraction(e, bits=30) 402 [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8] 399 403 sage: a = continued_fraction(17/23); a 400 404 [0, 1, 2, 1, 5] 401 405 sage: a.value() … … 404 408 [0, 3, 1, 5] 405 409 sage: a + b 406 410 [1] 407 sage: c = continued_fraction(pi,bits=3 3); c408 [3, 7, 15, 1, 292, 2]411 sage: c = continued_fraction(pi,bits=35); c 412 [3, 7, 15, 1, 292, 1] 409 413 sage: c.convergents() 410 [3, 22/7, 333/106, 355/113, 103993/33102, 208341/66317]414 [3, 22/7, 333/106, 355/113, 103993/33102, 104348/33215] 411 415 sage: c = continued_fraction(pi); c 412 [3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14 , 3]416 [3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14] 413 417 sage: for n in range(-1, len(c)): 414 418 ... print c.pn(n)*c.qn(n-1) - c.qn(n)*c.pn(n-1), 415 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1419 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 416 420 sage: for n in range(len(c)): 417 421 ... print c.pn(n)*c.qn(n-2) - c.qn(n)*c.pn(n-2), 418 3 -7 15 -1 292 -1 1 -1 2 -1 3 -1 14 -3422 3 -7 15 -1 292 -1 1 -1 2 -1 3 -1 14 419 423 sage: c = continued_fraction([1,2,3,4,5]) 420 424 sage: c.convergents() 421 425 [1, 3/2, 10/7, 43/30, 225/157] … … 434 438 ... x = (1 + sqrt(RealField(bits)(5))) / 2 435 439 ... return continued_fraction(x) 436 440 sage: cf(10) 437 [1, 1, 1, 1, 1, 1, 1 , 3]441 [1, 1, 1, 1, 1, 1, 1] 438 442 sage: cf(30) 439 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 440 1, 1, 1, 2] 443 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] 441 444 sage: cf(50) 442 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 443 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] 445 [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] 444 446 sage: def cf_sqrt_d(d, bits): 445 447 ... x = sqrt(RealField(bits)(d)) 446 448 ... return continued_fraction(x) 447 sage: cf_sqrt_d(389,50) 448 [19, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38] 449 sage: cf_sqrt_d(389,100) 450 [19, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38, 451 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 452 2, 1, 1] 449 sage: cf_sqrt_d(389,50) 450 [19, 1, 2, 1, 1, 1, 1, 2, 1, 38, 1, 2, 1, 1, 1, 1, 2, 1, 38, 2] 451 sage: cf_sqrt_d(389,100) 452 [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] 453 453 sage: def newton_root(f, iterates=2, x0=0, prec=53): 454 454 ... x = RealField(prec)(x0) 455 455 ... R = PolynomialRing(ZZ,'x') … … 462 462 sage: a = newton_root(3847*x^2 - 14808904*x + 36527265); a 463 463 2.46815700480740 464 464 sage: cf = continued_fraction(a); cf 465 [2, 2, 7, 2, 1, 5, 1, 1, 1, 1, 1, 1, 103, 8, 1, 2, 3 , 1, 1]465 [2, 2, 7, 2, 1, 5, 1, 1, 1, 1, 1, 1, 103, 8, 1, 2, 3] 466 466 sage: c = cf[:12]; c 467 467 [2, 2, 7, 2, 1, 5, 1, 1, 1, 1, 1, 1] 468 468 sage: c.value()
