Ticket #14567: trac_14567_rewiew-tm.patch

File trac_14567_rewiew-tm.patch, 7.3 KB (added by tmonteil, 6 years ago)
  • sage/rings/continued_fractions.py

    # HG changeset patch
    # User Thierry Monteil <sage at lma.metelu.net>
    # Date 1382189837 -7200
    # Node ID d465d0ce205213b018245603661e17f60d7a425e
    # Parent  54e74b4c72b9f5aee86a7c622db945d1021cdf3e
    #14567 tmonteil's review
    
    diff --git a/sage/rings/continued_fractions.py b/sage/rings/continued_fractions.py
    a b number). 
    1717
    1818It is quite remarkable that
    1919
    20 - finite expansions correspond to rationals
     20- any real number admits a unique continued fraction expansion,
     21- finite expansions correspond to rationals,
    2122- ultimately periodic expansions correspond to quadratic numbers (ie numbers of
    2223  the form `a + b \sqrt{D}` with `a` and `b` rationals and `D` square free
    23   integer)
     24  integer),
    2425- two real numbers `x` and `y` have the same tail (up to a shift) if and only if
    2526  there are integers `a,b,c,d` with `|ad - bc| = 1` and such that
    2627  `y = (ax + b) / (cx + d)`.
    2728
    2829Moreover, the rational numbers obtained by truncation of the expansion of a real
    29 number gives its so-called best approximations. For more informations on
     30number gives its so-called best approximations. For more information on
    3031continued fractions, you may have a look at :wikipedia:`Continued_fraction`.
    3132
    3233EXAMPLES:
    stands for real continued fraction) or t 
    5354    sage: -8 + 1/(4 + 1/4)
    5455    -132/17
    5556
    56 It is also possible to create a continued fraction from a list of digits::
     57It is also possible to create a continued fraction from a list of numbers
     58corresponding to its quotients::
    5759
    58     sage: cf = CFF([-3,1,2,3,4,1,2,1])
     60    sage: cf = CFF([-3, 1, 2, 3, 4, 1, 2, 1])
    5961    sage: cf.value()
    6062    -465/202
    6163
    represented as a pair of tuples, the pre 
    7577    sage: (3*sqrt2 + 1/2).continued_fraction()
    7678    [4; (1, 2, 1, 7)*]
    7779
    78     sage: cf = CFF([(1,2,3),(1,4)]); cf
     80    sage: cf = CFF([(1, 2, 3), (1, 4)]); cf
    7981    [1; 2, 3, (1, 4)*]
    8082    sage: cf.value()
    8183    -2/23*sqrt2 + 36/23
    quadratic field:: 
    107109
    108110Nevertheless, the tail is preserved under invertible integer homographies::
    109111
    110     sage: apply_homography =  lambda m,z: (m[0,0]*z + m[0,1]) / (m[1,0]*z+m[1,1])
     112    sage: apply_homography =  lambda m,z: (m[0,0]*z + m[0,1]) / (m[1,0]*z + m[1,1])
    111113    sage: m1 = SL2Z.random_element()
    112114    sage: m2 = SL2Z.random_element()
    113115    sage: a = sqrt2/3
    Nevertheless, the tail is preserved unde 
    119121    sage: c = apply_homography(m2, a)
    120122    sage: c.continued_fraction()
    121123    [0; 1, 26, 1, 2, 2, (8, 4)*]
    122     sage: d = apply_homography(m1**2*m2**3, a)
     124    sage: d = apply_homography(m1**2 * m2**3, a)
    123125    sage: d.continued_fraction()
    124126    [0; 1, 2, 1, 1, 1, 1, 5, 2, 1, 1, 1, 1, 5, 26, 1, 2, 1, 26, 1, 2, 1, 26, 1, 2, 2, (8, 4)*]
    125127
    126128It is possible to make arithmetic operations on continued fractions::
    127129
    128     sage: c1 = CFF([0,3,3,2,1,4,2]); c1
     130    sage: c1 = CFF([0, 3, 3, 2, 1, 4, 2]); c1
    129131    [0; 3, 3, 2, 1, 4, 2]
    130     sage: c2 = CFF([-4,2,1,3]); c2
     132    sage: c2 = CFF([-4, 2, 1, 3]); c2
    131133    [-4; 2, 1, 3]
    132134    sage: c3 = -c1; c3
    133135    [-1; 1, 2, 3, 2, 1, 4, 2]
    134     sage: c1+c2
     136    sage: c1 + c2
    135137    [-4; 1, 2, 628, 2]
    136     sage: c1+c3
     138    sage: c1 + c3
    137139    [0]
    138140
    139141    sage: c1/c2
    And they can be used to create matrices, 
    161163    ([0; 2], [0; 1, 2], [0; 1, 3], [0; 1, 4])
    162164
    163165The unary operations (negative and inversion) are quite fast but binary
    164 operations are quite slow. It is then not adviced, if speed is needed, to use
     166operations are quite slow. It is then not advised, if speed is needed, to use
    165167them as the base class in a computation.
    166168
    167169.. TODO::
    them as the base class in a computation. 
    171173      same method for an element of a number field)
    172174
    173175    - Make a class for infinite precision real number built from an infinite
    174       list (ie an infinite word)
     176      list (i.e. an infinite word)
    175177
    176178    - Make a class for non standard continued fractions of the form `a_0 +
    177179      b_0/(a_1 + b_1/(...))` (the standard continued fractions are when all
    178       `b_n= 1` while the Hirzebruch-Jung continued fractions are the one for
     180      `b_n = 1` while the Hirzebruch-Jung continued fractions are the one for
    179181      which `b_n = -1` for all `n`). See
    180182      :wikipedia:`Generalized_continued_fraction`.
    181183
    def two_last_convergents(x): 
    217219        (0, 1, 1, 0)
    218220        sage: two_last_convergents([0])
    219221        (1, 0, 0, 1)
    220         sage: two_last_convergents([-1,1,3,2])
     222        sage: two_last_convergents([-1 , 1, 3, 2])
    221223        (-1, 4, -2, 9)
    222224    """
    223225    p0, p1 = 0, 1
    224226    q0, q1 = 1, 0
    225227    for a in x:
    226         p0, p1 = p1, a*p1+p0
    227         q0, q1 = q1, a*q1+q0
     228        p0, p1 = p1, a*p1 + p0
     229        q0, q1 = q1, a*q1 + q0
    228230    return p0, q0, p1, q1
    229231
    230232
    class ContinuedFraction_generic(FieldEle 
    267269
    268270    def __abs__(self):
    269271        """
    270         Return absolute value of self.
     272        Return the absolute value of ``self``.
    271273
    272274        EXAMPLES::
    273275
    class ContinuedFraction_generic(FieldEle 
    323325            0.500000000000000
    324326            sage: continued_fraction([0,4]).n()
    325327            0.250000000000000
    326             sage: continued_fraction([12,1,3,4,2,2,3,1,2]).n(digits=4)
     328            sage: continued_fraction([12, 1, 3, 4, 2, 2, 3, 1, 2]).n(digits=4)
    327329            12.76
    328330
    329331            sage: continued_fraction(12/7).n(digits=13) == (12/7).n(digits=13)
    class ContinuedFraction_generic(FieldEle 
    374376            sage: for _ in xrange(100):
    375377            ....:     a = QQ.random_element(num_bound=1<<64)
    376378            ....:     cf = continued_fraction(a)
    377             ....:     for prec in 17,24,53,128,256:
    378             ....:         for rnd in 'RNDN','RNDD','RNDU','RNDZ':
     379            ....:     for prec in 17, 24, 53, 128, 256:
     380            ....:         for rnd in 'RNDN', 'RNDD', 'RNDU', 'RNDZ':
    379381            ....:             R = RealField(prec=prec, rnd=rnd)
    380382            ....:             assert R(cf) == R(a)
    381383        """
    def continued_fraction_list(x, type="std 
    19631965
    19641966    OUTPUT:
    19651967
    1966     A lits of integers, the coefficients in the continued fraction expansion of
     1968    A list of integers, the coefficients in the continued fraction expansion of
    19671969    ``x``. If ``partial_convergents`` is set to ``True``, then return a pair
    19681970    containing the coefficient list and the partial convergents list is
    19691971    returned.
    def continued_fraction(x, bits=None, nte 
    21242126        sage: pi.n()
    21252127        3.14159265358979
    21262128
    2127     When possible, it is adviced to use anything else but the symbolic ring.
     2129    When possible, it is advised to use anything else but the symbolic ring.
    21282130    Here we present an other way of dealing with the golden mean and the cube
    21292131    root of 2::
    21302132
    def Hirzebruch_Jung_continued_fraction_l 
    22112213
    22122214    OUTPUT:
    22132215
    2214     A lits of integers, the coefficients in the Hirzebruch-Jung continued
     2216    A list of integers, the coefficients in the Hirzebruch-Jung continued
    22152217    fraction expansion of ``x``.
    22162218
    22172219    EXAMPLES::
    def Hirzebruch_Jung_continued_fraction_l 
    22322234# sage.rings.continued_fractions in #14567
    22332235
    22342236from sage.structure.sage_object import register_unpickle_override
    2235 register_unpickle_override('sage.rings.contfrac', 'ContinuedFractionField_class',ContinuedFractionField)
     2237register_unpickle_override('sage.rings.contfrac', 'ContinuedFractionField_class', ContinuedFractionField)