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
@@ -85,7 +85,7 @@ The functions in `continuedfractions.lib` are standalone and thus useful on thei
85
85
86
86
### A Simple Introduction with Examples
87
87
88
-
From a user perspective it is easiest to use the [`continuedfractions.continuedfraction.ContinuedFraction`](https://github.com/sr-murthy/continuedfractions/blob/main/src/continuedfraction.py) class. A simple introduction is given below with a variety of examples.
88
+
The starting point is the [`continuedfractions.continuedfraction.ContinuedFraction`](https://github.com/sr-murthy/continuedfractions/blob/main/src/continuedfraction.py) class. A simple introduction is given below of it can be used, with a variety of examples.
89
89
90
90
#### Importing the `ContinuedFraction` Class
91
91
@@ -221,32 +221,74 @@ A related concept is that of **remainders** of continued fractions, which are (p
Another feature which the package includes is [mediants](https://en.wikipedia.org/wiki/Mediant_(mathematics)). The mediant of two rational numbers $\frac{a}{b}$ and $\frac{c}{d}$, where $b, d \neq 0$, is given by the fraction:
224
+
#### Mediants
225
+
226
+
Another feature which the package includes is [mediants](https://en.wikipedia.org/wiki/Mediant_(mathematics)). The (simple) mediant of two rational numbers $\frac{a}{b}$ and $\frac{c}{d}$, where $b, d \neq 0$, is defined as the rational number:
225
227
226
228
$$
227
229
\frac{a + c}{b + d}
228
230
$$
229
231
230
-
and has the property that:
232
+
Assuming that $\frac{a}{b} < \frac{c}{d}$ and $cd > 0$ the mediant above has the property that:
231
233
232
234
$$
233
235
\frac{a}{b} < \frac{a + c}{b + d} < \frac{c}{d}
234
236
$$
235
237
236
-
assuming $\frac{a}{b} < \frac{c}{d}$ and $cd > 0$.
237
-
238
-
The `ContinuedFraction` class provides a `.mediant()` method for objects to compute their mediants with a given fraction, which could be another `ContinuedFraction` or `fractions.Fraction` object. The result is also a `ContinuedFraction` object. A few examples are given below.
238
+
Mediants can give good rational approximations to real numbers of interest.
239
239
240
+
The `ContinuedFraction` class provides a `.mediant()` method which can be used to calculate mediants with other `ContinuedFraction` or `fractions.Fraction` objects. The result is also a `ContinuedFraction` object. A few examples are given below of how to calculate mediants.
The concept of the simple mediant of two fractions $\frac{a}{b}$ and $\frac{c}{d}$ as given above can be generalised to $k$-th mediants, which can be defined as follows:
254
+
255
+
$$
256
+
\frac{a + kc}{b + kd}
257
+
$$
258
+
259
+
Assuming that $\frac{a}{b} < \frac{c}{d}$ and $cd > 0$ we have, as $k$ increases, a strictly increasing sequence of fractions bounded by $\frac{a}{b}$ and $\frac{c}{d}$ on either side:
We can illustrate this using `ContinuedFraction.mediant` using increasing values of the mediant order `k`, which defaults to `k=1`.
272
+
273
+
```python
274
+
>>> c1 = ContinuedFraction(1, 2)
275
+
>>> c2 = ContinuedFraction(3, 5)
276
+
>>> c1.mediant(c2)
277
+
ContinuedFraction(4, 7)
278
+
>>> c1.mediant(c2).as_float()
279
+
0.5714285714285714
280
+
>>> c1.mediant(c2, k=10)
281
+
0.5961538461538461
282
+
>>> c1.mediant(c2, k=100)
283
+
0.599601593625498
284
+
>>> c1.mediant(c2, k=10**6)
285
+
ContinuedFraction(3000001, 5000002)
286
+
>>> c1.mediant(c2, k=10**6).as_float()
287
+
0.599999960000016
288
+
```
289
+
290
+
Mediants have other [interesting connections](https://en.wikipedia.org/wiki/Stern%E2%80%93Brocot_tree), including to sequences and orderings of rational numbers.
291
+
250
292
### Constructing Continued Fractions from Element Sequences
251
293
252
294
Continued fractions can also be constructed from element sequences, using the `ContinuedFraction.from_elements()` class method. Because `ContinuedFraction` is a subclass of `fractions.Fraction` all `ContinuedFraction` objects are fully operable as rational numbers, including as negative rationals.
@@ -424,25 +466,30 @@ The CI/CD pipelines are defined in the [CI YML](.github/workflows/ci.yml), and p
424
466
425
467
### Versioning & Package Publishing
426
468
427
-
The package is currently at version `0.0.4`, and packages are published manually to [PyPI](https://pypi.org/project/continuedfractions/). There is currently no release pipeline - this will be added later.
469
+
The package is currently at version `0.0.6` ([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.
428
470
429
471
## License
430
472
431
473
The project is [licensed](LICENSE) under the [Mozilla Public License 2.0](https://opensource.org/licenses/MPL-2.0).
432
474
433
-
434
475
## References
435
476
436
-
[1] Barrow, John D. “Chaos in Numberland: The secret life of continued fractions.” plus.maths.org, 1 June 2000, https://plus.maths.org/content/chaos-numberland-secret-life-continued-fractionsURL.
477
+
[1] Baker, Alan. A concise introduction to the theory of numbers. Cambridge: Cambridge Univ. Pr., 2002.
478
+
479
+
[2] Barrow, John D. “Chaos in Numberland: The secret life of continued fractions.” plus.maths.org, 1 June 2000, https://plus.maths.org/content/chaos-numberland-secret-life-continued-fractionsURL.
480
+
481
+
[3] Emory University Math Center. “Continued Fractions.” The Department of Mathematics and Computer Science, https://mathcenter.oxford.emory.edu/site/math125/continuedFractions/. Accessed 19 Feb 2024.
482
+
483
+
[4] Python 3.12.2 Docs. "Floating Point Arithmetic: Issues and Limitations." https://docs.python.org/3/tutorial/floatingpoint.html. Accessed 20 February 2024.
437
484
438
-
[2] Emory University Math Center. “Continued Fractions.” The Department of Mathematics and Computer Science, https://mathcenter.oxford.emory.edu/site/math125/continuedFractions/. Accessed 19 Feb 2024.
[3] Python 3.12.2 Docs. "Floating Point Arithmetic: Issues and Limitations." https://docs.python.org/3/tutorial/floatingpoint.html. Accessed 20 February 2024.
487
+
[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.
441
488
442
-
[4] Wikipedia. "Continued Fraction". https://en.wikipedia.org/wiki/Continued_fraction. Accessed 19 February 2024.
489
+
[7] Wikipedia. "Continued Fraction". https://en.wikipedia.org/wiki/Continued_fraction. Accessed 19 February 2024.
443
490
444
491
[^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).
445
492
446
493
[^2]: The definition of "rational number" used here is standard: a ratio $\frac{a}{b}$ of integers $a$ and $b \neq 0$. If $a$ and $b$ have no common denominator then \frac{a}{b} is irreducible and unique.
447
494
448
-
[^3]: This is due to the [theorem](https://en.wikipedia.org/wiki/Monotone_convergence_theorem) that every bounded monotonic sequence of real numbers converges.
495
+
[^3]: This is due to the [theorem](https://en.wikipedia.org/wiki/Monotone_convergence_theorem) that every bounded monotonic sequence of real numbers converges.
0 commit comments