Generate Farey sequences and Farey ranges
Project description
farey
Generate Farey sequences and Farey ranges.
Overview
The Farey sequence F_n is the sequence of all completely reduced fractions between 0 and 1, with denominators ≤ n, in ascending order.
A Farey range applies a Farey sequence to subdivide an arbitrary interval into rational points. In other words, it is the (sorted) union of all the rational ranges with (the reciprocal of their) steps equal to the elements of the Farey sequence.
farey_sequence
Usage
from farey import farey_sequence
farey_sequence(1)
# [Fraction(0, 1), Fraction(1, 1)]
farey_sequence(5)
# [Fraction(0, 1), Fraction(1, 5), Fraction(1, 4), Fraction(1, 3),
# Fraction(2, 5), Fraction(1, 2), Fraction(3, 5), Fraction(2, 3),
# Fraction(3, 4), Fraction(4, 5), Fraction(1, 1)]
farey_range
Usage
from farey import farey_range
from fractions import Fraction
farey_range(0, 1, 3)
# [Fraction(0, 1), Fraction(1, 3), Fraction(1, 2), Fraction(2, 3), Fraction(1, 1)]
farey_range(0, 2, 4)
# [Fraction(0, 1), Fraction(1, 4), Fraction(1, 3), Fraction(1, 2),
# Fraction(2, 3), Fraction(3, 4), Fraction(1, 1), Fraction(5, 4),
# Fraction(4, 3), Fraction(3, 2), Fraction(5, 3), Fraction(7, 4),
# Fraction(2, 1)]
# Reversed
farey_range(0, 1, -5)
# [Fraction(1, 1), Fraction(4, 5), ..., Fraction(0, 1)]
Parameters
| Parameter | Default | Description |
|---|---|---|
start |
(required) | Left endpoint of the interval |
end |
(required) | Right endpoint of the interval |
step |
None |
Controls the Farey order and direction (see below) |
Step conventions
The step parameter controls which Farey order is used and whether the result is ascending or descending:
| Step value | Order | Direction |
|---|---|---|
n (positive int) |
n | ascending |
-n (negative int) |
n | descending |
1/n (float or Fraction(1, n)) |
n | ascending |
-1/n (float or Fraction(-1, n)) |
n | descending |
None |
1 | ascending |
All equivalent forms produce the same result:
farey_range(0, 2, 3)
farey_range(0, 2, Fraction(1, 3))
farey_range(0, 2, 1/3)
# all return the same list
See also
This Python package has been inspired by the following Wolfram functions:
- FareySequence (built-in Wolfram Language function);
- FareyRange (resource function contributed by Jan Mangaldan).
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file farey-0.6.0.tar.gz.
File metadata
- Download URL: farey-0.6.0.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84d9b65d826728e93de6eaf03d792cdaba134d64cfff17623cbe0e2267530f22
|
|
| MD5 |
0c60d3c6ed770686eca71a3fa946b0a5
|
|
| BLAKE2b-256 |
a1e6f3df55336164318e247de9ba308b459d677fe47892cdacc4acdf29c6a3c4
|
File details
Details for the file farey-0.6.0-py3-none-any.whl.
File metadata
- Download URL: farey-0.6.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b1824f5c86fba7dcd212525ca66d0dbc2bdb8de0831cf5969e11b5bcd07847b
|
|
| MD5 |
6df75df9ad64fb6cc30b45870bfcb177
|
|
| BLAKE2b-256 |
266520f47fecb33aa6685a3c1201b9beebf75cf9bd4a3016ebb044806994098b
|