You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+42-12Lines changed: 42 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,7 @@ True
49
49
50
50
## Installation & Dependencies
51
51
52
-
The package does not use any 3rd party (production) dependencies, only Python standard libraries, and is supported on Python versions `3.10`-`3.12`. It is CI-tested on Ubuntu Linux (22.04.3 LTS), Mac OS (12.7.3) and Windows (Windows Server 2022), but should also install on any other platform supporting these Python versions.
52
+
The package uses only Python standard libraries, and is supported on Python versions `3.10`-`3.12`. It is CI-tested on Ubuntu Linux (22.04.3 LTS), Mac OS (12.7.3) and Windows (Windows Server 2022), but should also install on any other platform supporting these Python versions.
53
53
54
54
The simplest way of installing it is a standard `pip`/`pip3` install:
55
55
@@ -63,11 +63,15 @@ For contributors there are development requirements which are specified in the [
63
63
64
64
[Continued fractions](https://en.wikipedia.org/wiki/Continued_fraction) are beautiful and interesting mathematical objects, with many connections in [number theory](https://en.wikipedia.org/wiki/Number_theory) and also very useful practical applications, including the [rational approximation of real numbers](https://en.wikipedia.org/wiki/Continued_fraction#Best_rational_approximations).
65
65
66
-
The `continuedfractions` package is designed to make it easy to construct (finite) continued fractions as Python objects, and explore their key properties, such as elements/coefficients, convergents, segments, remainders, and others. They have been implemented as instances of the standard library [`fractions.Fraction`](https://docs.python.org/3/library/fractions.html#fractions.Fraction) class, of which they are automatically instances, and are thus fully operable as rational numbers.
66
+
The `continuedfractions` package is designed to:
67
+
68
+
* make it easy to construct (finite) continued fractions as Python objects
69
+
* explore their key properties, such as elements/coefficients, convergents, segments, remainders, and others
70
+
* operate on them as rationals and instances of the standard library [`fractions.Fraction`](https://docs.python.org/3/library/fractions.html#fractions.Fraction) class
67
71
68
72
### Package Structure
69
73
70
-
The `continuedfractions` package consists of two libraries:
74
+
The `continuedfractions` package consists of two core libraries:
71
75
72
76
*[`continuedfractions.lib`](https://github.com/sr-murthy/continuedfractions/blob/main/src/continuedfractions/lib.py) - this contains the core functionality of (1) generating continued fraction representations (as ordered element sequences) of any valid Python number, given as an integer, non-nan `float`, valid numeric string, a `fractions.Fraction` or `decimal.Decimal` object, or as a pair of integers and/or `fractions.Fraction` objects; and conversely (2) reconstructing rational fractions from continued fraction representations (again, given as ordered element sequences).
73
77
@@ -147,12 +151,25 @@ The **order** of a continued fraction is defined to be number of its elements **
147
151
3
148
152
```
149
153
154
+
The elements and order of `ContinuedFraction` objects are well behaved with respect to all rational operations supported by `fractions.Fraction`:
For an integer $k >= 0$ the $k$-th **convergent** $C_k$ of a continued fraction $[a_0; a_1,\ldots]$ of a real number $x$ is defined to be the rational number and finite continued fraction represented by $[a_0; a_1,\ldots,a_k]$, formed from the first $k + 1$ elements of the original.
Each convergent $C_k$ represents a rational approximation $\frac{p_k}{q_k}$ of $x$, and we can define an error term $\epsilon_k = x - C_k = x - \frac{p_k}{q_k}$. If we assume $x > 0$ then the convergents form a strictly increasing sequence of rational numbers converging to $x$ as $n \longrightarrow \infty$. So, formally:
@@ -254,7 +271,7 @@ $$
254
271
\frac{a + nc}{b + nd}
255
272
$$
256
273
257
-
For $k =1$ the left-and right-mediants are identical, but as$k \longrightarrow \infty$ they separate into two distinct strictly monotonic sequences with opposite limits: the left-mediants form a strictly decreasing sequence lower-bounded by $\frac{a}{b}$, thus converging to $\frac{a}{b}$:
274
+
For $k =1$ the left-and right-mediants are identical, but as$k \longrightarrow \infty$ they separate into two distinct, strictly monotonic, sequences converging to opposite limits[^3]: the left-mediants form a strictly decreasing sequence lower-bounded by $\frac{a}{b}$, converging to $\frac{a}{b}$:
We can illustrate this using `ContinuedFraction.mediant` method using the `dir` option to set the "direction" of the mediant - either `'left'`or`'right` (default) - and using `k` to set the mediant order, which defaults to `k=1`.
294
+
We can illustrate this using the `ContinuedFraction.mediant` method using the `dir` option to set the "direction" of the mediant - either `'left'`or`'right` (default) - and using `k` to set the mediant order, which defaults to `k=1`.
Mediants have other [interesting connections](https://en.wikipedia.org/wiki/Stern%E2%80%93Brocot_tree), including to sequences and orderings of rational numbers.
330
+
Mediants have very interesting connections to other areas of number, including sequences and orderings of rational numbers - for example, see the [Stern-Brocot tree](https://en.wikipedia.org/wiki/Stern%E2%80%93Brocot_tree).
314
331
315
332
### Constructing Continued Fractions from Element Sequences
316
333
@@ -340,7 +357,18 @@ ContinuedFraction(-200, 649)
340
357
341
358
### Continued Fractions with Negative Terms
342
359
343
-
Continued fractions representations with negative terms are valid, provided we use the [Euclidean integer division algorithm](https://en.wikipedia.org/wiki/Continued_fraction#Calculating_continued_fraction_representations) to calculate the successive quotients and remainders in each step. For example, $\frac{-415}{93} = \frac{-5 \times 93 + 50}{93}$ has the continued fraction representation $[-5; 1, 1, 6, 7]$. Compare this with $[4; 2, 6, 7]$, which is the continued fraction representation of $\frac{415}{93}$.
360
+
Continued fractions representations with negative terms are valid, provided we use the [Euclidean integer division algorithm](https://en.wikipedia.org/wiki/Continued_fraction#Calculating_continued_fraction_representations) to calculate the successive quotients and remainders in each step. For example, $\frac{-415}{93} = \frac{-5 \times 93 + 50}{93}$ has the continued fraction representation $[-5; 1, 1, 6, 7]$:
`ContinuedFraction` objects for negative numbers are constructed in the same way as with positive numbers, subject to the validation rules described above. And to avoid zero division problems if a fraction has a negative denominator the minus sign is "transferred" to the numerator. A few examples are given below.
346
374
@@ -489,7 +517,7 @@ The CI/CD pipelines are defined in the [CI YML](.github/workflows/ci.yml), and p
489
517
490
518
### Versioning & Package Publishing
491
519
492
-
The package is currently at version `0.0.9` ([semantic versioning](https://semver.org/) is used), and packages are published manually to [PyPI](https://pypi.org/project/continuedfractions/) using [twine](https://twine.readthedocs.io/en/stable/). There is currently no CI release pipeline - this will be added later.
520
+
The package is currently at version `0.10.0` ([semantic versioning](https://semver.org/) is used), and packages are published manually to [PyPI](https://pypi.org/project/continuedfractions/) using [twine](https://twine.readthedocs.io/en/stable/). There is currently no CI release pipeline - this will be added later.
493
521
494
522
## License
495
523
@@ -509,7 +537,9 @@ The project is [licensed](LICENSE) under the [Mozilla Public License 2.0](https:
509
537
510
538
[6] Python 3.12.2 Docs. "decimal - Decimal fixed point and floating point arithmetic." https://docs.python.org/3/library/decimal.html. Accessed 21 February 2024.
511
539
512
-
[7] Wikipedia. "Continued Fraction". https://en.wikipedia.org/wiki/Continued_fraction. Accessed 19 February 2024.
540
+
[7] Wikipedia. "Stern-Brocot Tree". https://en.wikipedia.org/wiki/Stern%E2%80%93Brocot_tree. Accessed 23 February 2024.
541
+
542
+
[8] Wikipedia. "Continued Fraction". https://en.wikipedia.org/wiki/Continued_fraction. Accessed 19 February 2024.
513
543
514
544
[^1]: Due to the nature of [binary floating point arithmetic](https://docs.python.org/3/tutorial/floatingpoint.html) it is not always possible to exactly represent a given [real number](https://en.wikipedia.org/wiki/Real_number). For the same reason, the continued fraction representations produced by the package will necessarily be [finite](https://en.wikipedia.org/wiki/Continued_fraction#Finite_continued_fractions).
0 commit comments