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