Ticket #14567: trac_14567_rewiewtm.patch
File trac_14567_rewiewtm.patch, 7.3 KB (added by , 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). 17 17 18 18 It is quite remarkable that 19 19 20  finite expansions correspond to rationals 20  any real number admits a unique continued fraction expansion, 21  finite expansions correspond to rationals, 21 22  ultimately periodic expansions correspond to quadratic numbers (ie numbers of 22 23 the form `a + b \sqrt{D}` with `a` and `b` rationals and `D` square free 23 integer) 24 integer), 24 25  two real numbers `x` and `y` have the same tail (up to a shift) if and only if 25 26 there are integers `a,b,c,d` with `ad  bc = 1` and such that 26 27 `y = (ax + b) / (cx + d)`. 27 28 28 29 Moreover, the rational numbers obtained by truncation of the expansion of a real 29 number gives its socalled best approximations. For more information son30 number gives its socalled best approximations. For more information on 30 31 continued fractions, you may have a look at :wikipedia:`Continued_fraction`. 31 32 32 33 EXAMPLES: … … stands for real continued fraction) or t 53 54 sage: 8 + 1/(4 + 1/4) 54 55 132/17 55 56 56 It is also possible to create a continued fraction from a list of digits:: 57 It is also possible to create a continued fraction from a list of numbers 58 corresponding to its quotients:: 57 59 58 sage: cf = CFF([3, 1,2,3,4,1,2,1])60 sage: cf = CFF([3, 1, 2, 3, 4, 1, 2, 1]) 59 61 sage: cf.value() 60 62 465/202 61 63 … … represented as a pair of tuples, the pre 75 77 sage: (3*sqrt2 + 1/2).continued_fraction() 76 78 [4; (1, 2, 1, 7)*] 77 79 78 sage: cf = CFF([(1, 2,3),(1,4)]); cf80 sage: cf = CFF([(1, 2, 3), (1, 4)]); cf 79 81 [1; 2, 3, (1, 4)*] 80 82 sage: cf.value() 81 83 2/23*sqrt2 + 36/23 … … quadratic field:: 107 109 108 110 Nevertheless, the tail is preserved under invertible integer homographies:: 109 111 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]) 111 113 sage: m1 = SL2Z.random_element() 112 114 sage: m2 = SL2Z.random_element() 113 115 sage: a = sqrt2/3 … … Nevertheless, the tail is preserved unde 119 121 sage: c = apply_homography(m2, a) 120 122 sage: c.continued_fraction() 121 123 [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) 123 125 sage: d.continued_fraction() 124 126 [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)*] 125 127 126 128 It is possible to make arithmetic operations on continued fractions:: 127 129 128 sage: c1 = CFF([0, 3,3,2,1,4,2]); c1130 sage: c1 = CFF([0, 3, 3, 2, 1, 4, 2]); c1 129 131 [0; 3, 3, 2, 1, 4, 2] 130 sage: c2 = CFF([4, 2,1,3]); c2132 sage: c2 = CFF([4, 2, 1, 3]); c2 131 133 [4; 2, 1, 3] 132 134 sage: c3 = c1; c3 133 135 [1; 1, 2, 3, 2, 1, 4, 2] 134 sage: c1 +c2136 sage: c1 + c2 135 137 [4; 1, 2, 628, 2] 136 sage: c1 +c3138 sage: c1 + c3 137 139 [0] 138 140 139 141 sage: c1/c2 … … And they can be used to create matrices, 161 163 ([0; 2], [0; 1, 2], [0; 1, 3], [0; 1, 4]) 162 164 163 165 The unary operations (negative and inversion) are quite fast but binary 164 operations are quite slow. It is then not advi ced, if speed is needed, to use166 operations are quite slow. It is then not advised, if speed is needed, to use 165 167 them as the base class in a computation. 166 168 167 169 .. TODO:: … … them as the base class in a computation. 171 173 same method for an element of a number field) 172 174 173 175  Make a class for infinite precision real number built from an infinite 174 list (i ean infinite word)176 list (i.e. an infinite word) 175 177 176 178  Make a class for non standard continued fractions of the form `a_0 + 177 179 b_0/(a_1 + b_1/(...))` (the standard continued fractions are when all 178 `b_n = 1` while the HirzebruchJung continued fractions are the one for180 `b_n = 1` while the HirzebruchJung continued fractions are the one for 179 181 which `b_n = 1` for all `n`). See 180 182 :wikipedia:`Generalized_continued_fraction`. 181 183 … … def two_last_convergents(x): 217 219 (0, 1, 1, 0) 218 220 sage: two_last_convergents([0]) 219 221 (1, 0, 0, 1) 220 sage: two_last_convergents([1 ,1,3,2])222 sage: two_last_convergents([1 , 1, 3, 2]) 221 223 (1, 4, 2, 9) 222 224 """ 223 225 p0, p1 = 0, 1 224 226 q0, q1 = 1, 0 225 227 for a in x: 226 p0, p1 = p1, a*p1 +p0227 q0, q1 = q1, a*q1 +q0228 p0, p1 = p1, a*p1 + p0 229 q0, q1 = q1, a*q1 + q0 228 230 return p0, q0, p1, q1 229 231 230 232 … … class ContinuedFraction_generic(FieldEle 267 269 268 270 def __abs__(self): 269 271 """ 270 Return absolute value of self.272 Return the absolute value of ``self``. 271 273 272 274 EXAMPLES:: 273 275 … … class ContinuedFraction_generic(FieldEle 323 325 0.500000000000000 324 326 sage: continued_fraction([0,4]).n() 325 327 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) 327 329 12.76 328 330 329 331 sage: continued_fraction(12/7).n(digits=13) == (12/7).n(digits=13) … … class ContinuedFraction_generic(FieldEle 374 376 sage: for _ in xrange(100): 375 377 ....: a = QQ.random_element(num_bound=1<<64) 376 378 ....: 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': 379 381 ....: R = RealField(prec=prec, rnd=rnd) 380 382 ....: assert R(cf) == R(a) 381 383 """ … … def continued_fraction_list(x, type="std 1963 1965 1964 1966 OUTPUT: 1965 1967 1966 A li tsof integers, the coefficients in the continued fraction expansion of1968 A list of integers, the coefficients in the continued fraction expansion of 1967 1969 ``x``. If ``partial_convergents`` is set to ``True``, then return a pair 1968 1970 containing the coefficient list and the partial convergents list is 1969 1971 returned. … … def continued_fraction(x, bits=None, nte 2124 2126 sage: pi.n() 2125 2127 3.14159265358979 2126 2128 2127 When possible, it is advi ced to use anything else but the symbolic ring.2129 When possible, it is advised to use anything else but the symbolic ring. 2128 2130 Here we present an other way of dealing with the golden mean and the cube 2129 2131 root of 2:: 2130 2132 … … def Hirzebruch_Jung_continued_fraction_l 2211 2213 2212 2214 OUTPUT: 2213 2215 2214 A li tsof integers, the coefficients in the HirzebruchJung continued2216 A list of integers, the coefficients in the HirzebruchJung continued 2215 2217 fraction expansion of ``x``. 2216 2218 2217 2219 EXAMPLES:: … … def Hirzebruch_Jung_continued_fraction_l 2232 2234 # sage.rings.continued_fractions in #14567 2233 2235 2234 2236 from sage.structure.sage_object import register_unpickle_override 2235 register_unpickle_override('sage.rings.contfrac', 'ContinuedFractionField_class', ContinuedFractionField)2237 register_unpickle_override('sage.rings.contfrac', 'ContinuedFractionField_class', ContinuedFractionField)