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  
    830830            sage: words.CharacteristicSturmianWord(1/golden_ratio^2, bits=30) 
    831831            word: 0100101001001010010100100101001001010010... 
    832832            sage: _.length() 
    833             28657 
     833            6765 
    834834 
    835835        :: 
    836836 
  • sage/rings/contfrac.py

    diff -r f1a647b0d257 -r cee8274a2fb3 sage/rings/contfrac.py
    a b  
    963963def continued_fraction(x, bits=None, nterms=None): 
    964964    """ 
    965965    Return the truncated continued fraction expansion of the real number 
    966     `x`, computed with a floating point approximation of `x` to the 
    967     given number of bits of precision. The returned continued 
     966    `x`, computed with an interval floating point approximation of `x`  
     967    to the given number of bits of precision. The returned continued 
    968968    fraction is a list-like object, with a value method and partial 
    969969    convergents method. 
    970970 
    971971    If bits is not given, then use the number of valid bits of 
    972972    precision of `x`, if `x` is a floating point number, or 53 bits 
    973973    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. 
    975975 
    976976    INPUT: 
    977977 
     
    10221022        [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8] 
    10231023        sage: continued_fraction(RealField(200)(e)) 
    10241024        [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] 
    10261032    """ 
    10271033    return CFF(x, bits=bits, nterms=nterms) 
    10281034 
  • sage/tests/book_stein_ent.py

    diff -r f1a647b0d257 -r cee8274a2fb3 sage/tests/book_stein_ent.py
    a b  
    387387303 
    388388sage: find_sqrt(5,389)         # see, it's random 
    38938986 
     390 
     391# Several of the examples below had to be changed due to improved 
     392# behavior of the continued_fraction function #8017. 
     393 
    390394sage: continued_fraction(17/23)  
    391395[0, 1, 2, 1, 5] 
    392396sage: reset('e') 
    393397sage: 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=20)   
     398[2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1] 
     399sage: continued_fraction(e, bits=21) 
    396400[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] 
     401sage: continued_fraction(e, bits=30) 
     402[2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8] 
    399403sage: a = continued_fraction(17/23); a 
    400404[0, 1, 2, 1, 5] 
    401405sage: a.value()  
     
    404408[0, 3, 1, 5] 
    405409sage: a + b  
    406410[1] 
    407 sage: c = continued_fraction(pi,bits=33); c 
    408 [3, 7, 15, 1, 292, 2] 
     411sage: c = continued_fraction(pi,bits=35); c 
     412[3, 7, 15, 1, 292, 1] 
    409413sage: 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] 
    411415sage: 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] 
    413417sage: for n in range(-1, len(c)): 
    414418...    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 1  
     4191 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 
    416420sage: for n in range(len(c)): 
    417421...    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 -3  
     4223 -7 15 -1 292 -1 1 -1 2 -1 3 -1 14 
    419423sage: c = continued_fraction([1,2,3,4,5])  
    420424sage: c.convergents()  
    421425[1, 3/2, 10/7, 43/30, 225/157] 
     
    434438...   x = (1 + sqrt(RealField(bits)(5))) / 2 
    435439...   return continued_fraction(x) 
    436440sage: cf(10) 
    437 [1, 1, 1, 1, 1, 1, 1, 3] 
     441[1, 1, 1, 1, 1, 1, 1] 
    438442sage: 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] 
    441444sage: 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] 
    444446sage: def cf_sqrt_d(d, bits): 
    445447...   x = sqrt(RealField(bits)(d)) 
    446448...   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] 
     449sage: 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] 
     451sage: 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] 
    453453sage: def newton_root(f, iterates=2, x0=0, prec=53): 
    454454...    x = RealField(prec)(x0) 
    455455...    R = PolynomialRing(ZZ,'x') 
     
    462462sage: a = newton_root(3847*x^2 - 14808904*x + 36527265); a 
    4634632.46815700480740 
    464464sage: 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] 
    466466sage: c = cf[:12]; c 
    467467[2, 2, 7, 2, 1, 5, 1, 1, 1, 1, 1, 1] 
    468468sage: c.value()