Skip to content

Commit 5d31f90

Browse files
committed
docs: improve docs for creating continued fractions
1 parent 10ba726 commit 5d31f90

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

docs/sources/creating-continued-fractions.rst

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,64 @@ Continued fractions can also be constructed from sequences of elements, using th
8787
>>> assert cf + (-cf) == cf_inverse + cf_negative_inverse == 0
8888
# True
8989
90+
.. _creating-continued-fractions.irrationals-from-elements:
91+
92+
Approximating Irrationals
93+
-------------------------
94+
95+
Using ``ContinuedFraction.from_elements()`` can be very useful when trying to approximate irrational numbers with (finite) continued fractions. We know, for example, that the square root :math:`sqrt(n)` of any non-square (positive) integer :math:`n` is irrational. This can be proved quite easily by writing :math:`n = a^2 + r`, for integers :math:`a, r > 0`, from which we have:
96+
97+
.. math::
98+
:nowrap:
99+
100+
\begin{alignat*}{1}
101+
& r &&= n - a^2 = \left(\sqrt{n} + a\right)\left(\sqrt{n} - a\right) \\
102+
& \sqrt{n} &&= a + \frac{r}{a + \sqrt{n}}
103+
\end{alignat*}
104+
105+
Expanding the expression for :math:`\sqrt{n}` recursively we have the following infinite periodic continued fraction representation for :math:`\sqrt{n}`:
106+
107+
.. math::
108+
109+
\sqrt{n} = a + \cfrac{r}{2a + \cfrac{r}{2a + \cfrac{r}{2a + \ddots}}}
110+
111+
With :math:`a = r = 1` we can represent :math:`\sqrt{2}` as the continued fraction:
112+
113+
.. math::
114+
115+
\sqrt{2} = 1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2 + \ddots}}}
116+
117+
written more compactly as :math:`[1; \bar{2}]`, where :math:`\bar{2}` represents an infinite sequence :math:`2, 2, 2, \ldots`.
118+
119+
We can start with a more precise representation of :math:`\sqrt{2}` in Python as a `decimal.Decimal` object:
120+
121+
.. code:: python
122+
123+
>>> Decimal(math.sqrt(2))
124+
>>> Decimal('1.4142135623730951454746218587388284504413604736328125')
125+
126+
Then we can iteratively construct more accurate ``ContinuedFraction`` approximations of :math:`\sqrt{n}` by taking more complete sequences of the elements of the completed continued fraction representation:
127+
128+
.. code:: python
129+
130+
>>> ContinuedFraction.from_elements(1, 2).as_float()
131+
>>> 1.5
132+
133+
>>> ContinuedFraction.from_elements(1, 2, 2).as_float()
134+
>>> 1.4
135+
136+
>>> ContinuedFraction.from_elements(1, 2, 2, 2, 2).as_float()
137+
>>> 1.4137931034482758
138+
139+
...
140+
141+
>>> ContinuedFraction.from_elements(1, 2, 2, 2, 2, 2, 2, 2, 2, 2).as_float()
142+
>>> 1.4142136248948696
143+
144+
...
145+
146+
With the first :math:`10` elements of the complete sequence of elements of the continued fraction representation of :math:`\sqrt{2}` we have obtained an approximation that is accurate to :math:`6` decimal places. We'd ideally like to have as few elements as possible in our ``ContinuedFraction`` approximation of :math:`\sqrt{2}` for a desired level of accuracy, but this partly depends on how fast the partial, finite continued fractions represented by the chosen sequences of elements in our approximations are converging to the true value of :math:`\sqrt{2}` - these partial, finite continued fractions in a continued fraction representation are called convergents, and will be discussed in more detail later on.
147+
90148
.. _creating-continued-fractions.validation:
91149

92150
Validation

0 commit comments

Comments
 (0)