==================================================================== Continued Fraction ==================================================================== Continued fractions have been a fascinating and useful tool in mathematics for well over three hundred years. FriCAS implements continued fractions for fractions of any Euclidean domain. In practice, this usually means rational numbers. In this section we demonstrate some of the operations available for manipulating both finite and infinite continued fractions. The ContinuedFraction domain is a field and therefore you can add, subtract, multiply and divide the fractions. The continuedFraction operation converts its fractional argument to a continued fraction. :: c := continuedFraction(314159/100000) 1 | 1 | 1 | 1 | 1 | 1 | 1 | 3 + +---+ + +----+ + +---+ + +----+ + +---+ + +---+ + +---+ | 7 | 15 | 1 | 25 | 1 | 7 | 4 Type: ContinuedFraction Integer This display is a compact form of the bulkier :: 3 + 1 ------------------------------- 7 + 1 --------------------------- 15 + 1 ---------------------- 1 + 1 ------------------ 25 + 1 ------------- 1 + 1 --------- 7 + 1 ----- 4 You can write any rational number in a similar form. The fraction will be finite and you can always take the "numerators" to be 1. That is, any rational number can be written as a simple, finite continued fraction of the form :: a(1) + 1 ------------------------- a(2) + 1 -------------------- a(3) + . . . 1 ------------- a(n-1) + 1 ---- a(n) The a(i) are called partial quotients and the operation partialQuotients creates a stream of them. :: partialQuotients c [3,7,15,1,25,1,7,4] Type: Stream Integer By considering more and more of the fraction, you get the convergents. For example, the first convergent is a(1), the second is a(1) + 1/a(2) and so on. :: convergents c 22 333 355 9208 9563 76149 314159 [3,--,---,---,----,----,-----,------] 7 106 113 2931 3044 24239 100000 Type: Stream Fraction Integer Since this is a finite continued fraction, the last convergent is the original rational number, in reduced form. The result of approximants is always an infinite stream, though it may just repeat the "last" value. :: approximants c ______ 22 333 355 9208 9563 76149 314159 [3,--,---,---,----,----,-----,------] 7 106 113 2931 3044 24239 100000 Type: Stream Fraction Integer Inverting c only changes the partial quotients of its fraction by inserting a 0 at the beginning of the list. :: pq := partialQuotients(1/c) [0,3,7,15,1,25,1,7,4] Type: Stream Integer Do this to recover the original continued fraction from this list of partial quotients. The three-argument form of the continuedFraction operation takes an element which is the whole part of the fraction, a stream of elements which are the numerators of the fraction, and a stream of elements which are the denominators of the fraction. :: continuedFraction(first pq,repeating [1],rest pq) 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | +---+ + +---+ + +----+ + +---+ + +----+ + +---+ + +---+ + +---+ | 3 | 7 | 15 | 1 | 25 | 1 | 7 | 4 Type: ContinuedFraction Integer The streams need not be finite for continuedFraction. Can you guess which irrational number has the following continued fraction? See the end of this section for the answer. :: z:=continuedFraction(3,repeating [1],repeating [3,6]) 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 3 + +---+ + +---+ + +---+ + +---+ + +---+ + +---+ + +---+ + +---+ + +---+ | 3 | 6 | 3 | 6 | 3 | 6 | 3 | 6 | 3 + 1 | +---+ + ... | 6 Type: ContinuedFraction Integer In 1737 Euler discovered the infinite continued fraction expansion :: e - 1 1 ----- = --------------------- 2 1 + 1 ----------------- 6 + 1 ------------- 10 + 1 -------- 14 + ... We use this expansion to compute rational and floating point approximations of e. For this and other interesting expansions, see [Olds]_ By looking at the above expansion, we see that the whole part is 0 and the numerators are all equal to 1. This constructs the stream of denominators. :: dens:Stream Integer := cons(1,generate((x+->x+4),6)) [1,6,10,14,18,22,26,30,34,38,...] Type: Stream Integer Therefore this is the continued fraction expansion for (e - 1) / 2. :: cf := continuedFraction(0,repeating [1],dens) 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | +---+ + +---+ + +----+ + +----+ + +----+ + +----+ + +----+ + +----+ | 1 | 6 | 10 | 14 | 18 | 22 | 26 | 30 + 1 | 1 | +----+ + +----+ + ... | 34 | 38 Type: ContinuedFraction Integer These are the rational number convergents. :: ccf := convergents cf 6 61 860 15541 342762 8927353 268163352 9126481321 [0,1,-,--,----,-----,------,--------,---------,-----------,...] 7 71 1001 18089 398959 10391023 312129649 10622799089 Type: Stream Fraction Integer You can get rational convergents for e by multiplying by 2 and adding 1. :: eConvergents := [2*e + 1 for e in ccf] 19 193 2721 49171 1084483 28245729 848456353 28875761731 [1,3,--,---,----,-----,-------,--------,---------,-----------,...] 7 71 1001 18089 398959 10391023 312129649 10622799089 Type: Stream Fraction Integer You can also compute the floating point approximations to these convergents. :: eConvergents :: Stream Float [1.0, 3.0, 2.7142857142 857142857, 2.7183098591 549295775, 2.7182817182 817182817, 2.7182818287 356957267, 2.7182818284 585634113, 2.7182818284 590458514, 2.7182818284 590452348, 2.7182818284 590452354, ...] Type: Stream Float Compare this to the value of e computed by the exp operation in Float. :: exp 1.0 2.7182818284 590452354 Type: Float In about 1658, Lord Brouncker established the following expansion for 4 / pi, :: 1 + 1 ----------------------- 2 + 9 ------------------- 2 + 25 --------------- 2 + 49 ----------- 2 + 81 ------- 2 + ... Let's use this expansion to compute rational and floating point approximations for pi. :: cf := continuedFraction(1,[(2*i+1)**2 for i in 0..],repeating [2]) 1 | 9 | 25 | 49 | 81 | 121 | 169 | 225 | 1 + +---+ + +---+ + +----+ + +----+ + +----+ + +-----+ + +-----+ + +-----+ | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 + 289 | 361 | +-----+ + +-----+ + ... | 2 | 2 Type: ContinuedFraction Integer ccf := convergents cf 3 15 105 315 3465 45045 45045 765765 14549535 [1,-,--,---,---,----,-----,-----,------,--------,...] 2 13 76 263 2578 36979 33976 622637 11064338 Type: Stream Fraction Integer piConvergents := [4/p for p in ccf] 8 52 304 1052 10312 147916 135904 2490548 44257352 [4,-,--,---,----,-----,------,------,-------,--------,...] 3 15 105 315 3465 45045 45045 765765 14549535 Type: Stream Fraction Integer As you can see, the values are converging to ``pi = 3.14159265358979323846...``, but not very quickly. :: piConvergents :: Stream Float [4.0, 2.6666666666 666666667, 3.4666666666 666666667, 2.8952380952 380952381, 3.3396825396 825396825, 2.9760461760 461760462, 3.2837384837 384837385, 3.0170718170 718170718, 3.2523659347 188758953, 3.0418396189 294022111, ...] Type: Stream Float You need not restrict yourself to continued fractions of integers. Here is an expansion for a quotient of Gaussian integers. :: continuedFraction((- 122 + 597*%i)/(4 - 4*%i)) 1 | 1 | - 90 + 59%i + +---------+ + +-----------+ | 1 - 2%i | - 1 + 2%i Type: ContinuedFraction Complex Integer This is an expansion for a quotient of polynomials in one variable with rational number coefficients. :: r : Fraction UnivariatePolynomial(x,Fraction Integer) Type: Void r := ((x - 1) * (x - 2)) / ((x-3) * (x-4)) 2 x - 3x + 2 ------------ 2 x - 7x + 12 Type: Fraction UnivariatePolynomial(x,Fraction Integer) continuedFraction r 1 | 1 | 1 + +---------+ + +-----------+ | 1 9 | 16 40 | - x - - | -- x - -- | 4 8 | 3 3 Type: ContinuedFraction UnivariatePolynomial(x,Fraction Integer) To conclude this section, we give you evidence that :: z = 3 + 1 ----------------------- 3 + 1 ------------------- 6 + 1 --------------- 3 + 1 ----------- 6 + 1 ------- 3 + ... is the expansion of ``sqrt(11)``. :: [i*i for i in convergents(z) :: Stream Float] [9.0, 11.1111111111 11111111, 10.9944598337 9501385, 11.0002777777 77777778, 10.9999860763 98799786, 11.0000006979 29731039, 10.9999999650 15834446, 11.0000000017 53603304, 10.9999999999 12099531, 11.0000000000 04406066, ...] Type: Stream Float .. [Olds] C. D. Olds, Continued Fractions, New Mathematical Library, (New York: Random House, 1963), pp. 134--139.} See Also: * ``)help Stream`` * ``)show ContinuedFraction``