Getting Started#
This is a PyPI package which uses only Python standard libraries, and is supported on any Linux, Mac OS or Windows system supporting Python 3.10, 3.11, or 3.12. It is CI-tested on Ubuntu Linux (22.04.4 LTS), Mac OS (13.6.6) and Windows (Windows Server 2022 21H2), but should also install on any other platform supporting these Python versions.
Installation#
A standard pip
install is sufficient:
pip install continuedfractions
Potential contributors may want look at the project TOML, and then the contributions overview.
Package Structure#
The continuedfractions
package consists of three core libraries.
Note
The continuedfractions.utils
library contains internal utilities.
Each is summarised below. Or you can go straight to the API reference.
continuedfractions.lib
#
This is a library of standalone functions:
continued_fraction_rational()
- generates the (ordered) sequence of elements (coefficients) of the unique simple continued fraction of a given rational number, given as afractions.Fraction
instance.continued_fraction_real()
- generates the sequence of elements of a simple continued fraction of a real number, given as a singleint
,float
,str
, ordecimal.Decimal
value; the results forfloat
inputs may be approximate and not necessarily unique.fraction_from_elements()
- returns afractions.Fraction
instance of the rational number represented by a simple continued fraction with the given sequence of elements.convergent()
- returns the \(k\)-th convergent (for a positive integer \(k\)) from a sequence of elements of a (finite) simple continued fraction; the convergent is returned as afractions.Fraction
instance.mediant()
- returns the \(k\)-th left or right mediant of two rational numbers, given asfractions.Fraction
values; the mediant is returned as afractions.Fraction
instance.
Note
There are also two “wrapper” functions for computing left- and right-mediants - left_mediant()
and right_mediant()
- but these just call mediant()
.
continuedfractions.continuedfraction
#
This is a library containing a single main class:
ContinuedFraction
- a subclass offractions.Fraction
, designed to represent (finite) simple continued fractions as Python objects, which are fully operable as rational numbers.
continuedfractions.sequences
#
This is a library of functions and classes relating to ordered sequences and structures of integers and rational numbers, such as (currently) coprime integers, Farey sequences and (in the future) the Stern-Brocot tree:
coprime_integers()
- returns a sequence of integers coprime (or relatively prime) to a given positive integer.KSRMTree
- implements an implicit/generative class for the Kanga-Saunders-Randall-Mitchell (KSRM) ternary trees for representing and generating pairs of (positive) coprime integers.coprime_pairs()
- returns a sequence of all pairs of (positive) coprime integers less than or equal to a given positive integer. Uses the KSRM treesearch()
method to perform the search.farey_sequence()
- returns a sequence of rational numbers called a Farey sequence for a given positive integer. Uses thecoprime_integers()
andcoprime_pairs()
functions.