1% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved.
2% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk.
3\newcommand{\ContinuedFractionXmpTitle}{ContinuedFraction}
4\newcommand{\ContinuedFractionXmpNumber}{9.12}
5%
6% =====================================================================
7\begin{page}{ContinuedFractionXmpPage}{9.12 ContinuedFraction}
8% =====================================================================
9\beginscroll
10
11%
12%
13%
14%
15%
16
17
18
19Continued fractions have been a fascinating and useful tool in mathematics
20%-% \HDindex{fraction!continued}{ContinuedFractionXmpPage}{9.12}{ContinuedFraction}
21for well over three hundred years.
22\Language{} implements continued fractions for fractions of any Euclidean
23%-% \HDindex{continued fraction}{ContinuedFractionXmpPage}{9.12}{ContinuedFraction}
24domain.
25In practice, this usually means rational numbers.
26In this section we demonstrate some of the operations available for
27manipulating both finite and infinite continued fractions.
28It may be helpful if you review \downlink{`Stream'}{StreamXmpPage}\ignore{Stream} to remind yourself of some
29of the operations with streams.
30
31The \spadtype{ContinuedFraction} domain is a field and therefore you can
32add, subtract, multiply and divide the fractions.
33\xtc{
34The \spadfunFrom{continuedFraction}{ContinuedFraction} operation
35converts its fractional argument to a continued fraction.
36}{
37\spadpaste{c := continuedFraction(314159/100000) \bound{c}}
38}
39%
40This display is a compact form of the bulkier
41\begin{verbatim}
42        3 +                 1
43            -------------------------------
44            7 +               1
45                ---------------------------
46                15 +            1
47                     ----------------------
48                     1 +          1
49                         ------------------
50                         25 +       1
51                              -------------
52                              1 +     1
53                                  ---------
54                                  7 +   1
55                                      -----
56                                        4
57\end{verbatim}
58You can write any rational number in a similar form.
59The fraction will be finite and you can always take the ``numerators'' to
60be \spad{1}.
61That is, any rational number can be written as a simple, finite continued
62fraction of the form
63\begin{verbatim}
64        a(1) +           1
65               -------------------------
66               a(2) +          1
67                      --------------------
68                      a(3) +
69                             .
70                              .
71                               .
72                                     1
73                               -------------
74                               a(n-1) +  1
75                                        ----
76                                        a(n)
77\end{verbatim}
78\xtc{
79The \spad{a(i)} are called partial quotients and the operation
80\spadfunFrom{partialQuotients}{ContinuedFraction} creates a stream of them.
81}{
82\spadpaste{partialQuotients c \free{c}}
83}
84\xtc{
85By considering more and more of the fraction, you get the
86\spadfunFrom{convergents}{ContinuedFraction}.
87For example, the first convergent is \spad{a(1)},
88the second is
89\spad{a(1) + 1/a(2)} and so on.
90}{
91\spadpaste{convergents c \free{c}}
92}
93%
94\xtc{
95Since this is a finite continued fraction, the last convergent is
96the original rational number, in reduced form.
97The result of \spadfunFrom{approximants}{ContinuedFraction}
98is always an infinite stream, though it may just repeat the ``last''
99value.
100}{
101\spadpaste{approximants c \free{c}}
102}
103\xtc{
104Inverting \spad{c} only changes the partial quotients of its fraction
105by inserting a \spad{0} at the beginning of the list.
106}{
107\spadpaste{pq := partialQuotients(1/c) \free{c}\bound{pq}}
108}
109\xtc{
110Do this to recover the original continued fraction from this list of
111partial quotients.
112The three-argument form of the
113\spadfunFrom{continuedFraction}{ContinuedFraction} operation takes an
114element which is the whole part of the fraction, a stream of elements
115which are the numerators of the fraction, and a stream of elements which
116are the denominators of the fraction.
117}{
118\spadpaste{continuedFraction(first pq,repeating [1],rest pq) \free{pq}}
119}
120\xtc{
121The streams need not be finite for
122\spadfunFrom{continuedFraction}{ContinuedFraction}.
123Can you guess which irrational number has the following continued
124fraction?
125See the end of this section for the answer.
126}{
127\spadpaste{z:=continuedFraction(3,repeating [1],repeating [3,6]) \bound{z}}
128}
129%
130
131In 1737 Euler discovered the infinite continued fraction expansion
132\begin{verbatim}
133        e - 1             1
134        ----- = ---------------------
135          2     1 +         1
136                    -----------------
137                    6 +       1
138                        -------------
139                        10 +    1
140                             --------
141                             14 + ...
142\end{verbatim}
143We use this expansion to compute rational and floating point
144approximations of \spad{e}.\footnote{For this and other interesting
145expansions, see C. D. Olds, {\it Continued Fractions,}
146New Mathematical Library, (New York: Random House, 1963), pp.
147134--139.}
148
149\xtc{
150By looking at the above expansion, we see that the whole part is \spad{0}
151and the numerators are all equal to \spad{1}.
152This constructs the stream of denominators.
153}{
154\spadpaste{dens : Stream Integer := cons(1, stream((x +-> x + 4), 6)) \bound{dens}}
155}
156\xtc{
157Therefore this is the continued fraction expansion for
158\spad{(e-1)/2}.
159}{
160\spadpaste{cf := continuedFraction(0,repeating [1],dens) \free{dens}\bound{cf}}
161}
162\xtc{
163These are the rational number convergents.
164}{
165\spadpaste{ccf := convergents cf \free{cf}\bound{ccf}}
166}
167\xtc{
168You can get rational convergents for \spad{e} by multiplying by \spad{2} and
169adding \spad{1}.
170}{
171\spadpaste{eConvergents := [2*e + 1 for e in ccf] \bound{ec}\free{ccf}}
172}
173%
174\xtc{
175You can also compute the floating point approximations to these convergents.
176}{
177\spadpaste{eConvergents :: Stream Float \free{ec}}
178}
179\xtc{
180Compare this to the value of \spad{e} computed by the
181\spadfunFrom{exp}{Float} operation in \spadtype{Float}.
182}{
183\spadpaste{exp 1.0}
184}
185
186In about 1658, Lord Brouncker established the following expansion
187for \spad{4/pi}.
188\begin{verbatim}
189        1 +            1
190            -----------------------
191            2 +          9
192                -------------------
193                2 +        25
194                    ---------------
195                    2 +      49
196                        -----------
197                        2 +    81
198                            -------
199                            2 + ...
200\end{verbatim}
201\xtc{
202Let's use this expansion to compute rational and floating point
203approximations for \spad{pi}.
204}{
205\spadpaste{cf := continuedFraction(1,[(2*i+1)^2 for i in 0..],repeating [2])\bound{cf1}}
206}
207\xtc{
208}{
209\spadpaste{ccf := convergents cf \free{cf1}\bound{ccf1}}
210}
211\xtc{
212}{
213\spadpaste{piConvergents := [4/p for p in ccf] \bound{piConvergents}\free{ccf1}}
214}
215\xtc{
216As you can see, the values are converging to
217\spad{pi} = 3.14159265358979323846...,
218but not very quickly.
219}{
220\spadpaste{piConvergents :: Stream Float \free{piConvergents}}
221}
222
223\xtc{
224You need not restrict yourself to continued fractions of integers.
225Here is an expansion for a quotient of Gaussian integers.
226%-% \HDindex{Gaussian integer}{ContinuedFractionXmpPage}{9.12}{ContinuedFraction}
227}{
228\spadpaste{continuedFraction((- 122 + 597*\%i)/(4 - 4*\%i))}
229}
230\xtc{
231This is an expansion for a quotient of polynomials in one variable
232with rational number coefficients.
233}{
234\spadpaste{r : Fraction UnivariatePolynomial(x,Fraction Integer) \bound{rdec}}
235}
236\xtc{
237}{
238\spadpaste{r := ((x - 1) * (x - 2)) / ((x-3) * (x-4)) \free{rdec}\bound{r}}
239}
240\xtc{
241}{
242\spadpaste{continuedFraction r \free{r}}
243}
244
245\xtc{
246To conclude this section, we give you evidence that
247}{
248\spadpaste{z \free{z}}
249}
250is the expansion of the square root of \spad{11}.
251%
252\xtc{
253}{
254\spadpaste{[i*i for i in convergents(z) :: Stream Float] \free{z}}
255}
256\xtc{
257}{
258\spadpaste{continuedFraction sqrt 11.0}
259}
260\endscroll
261\autobuttons
262\end{page}
263%
264