Skip to main content

Generate Farey sequences and Farey ranges

Project description

farey

Tests PyPI version Python License: MIT

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:

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

farey-0.6.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

farey-0.6.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

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

Hashes for farey-0.6.0.tar.gz
Algorithm Hash digest
SHA256 84d9b65d826728e93de6eaf03d792cdaba134d64cfff17623cbe0e2267530f22
MD5 0c60d3c6ed770686eca71a3fa946b0a5
BLAKE2b-256 a1e6f3df55336164318e247de9ba308b459d677fe47892cdacc4acdf29c6a3c4

See more details on using hashes here.

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

Hashes for farey-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b1824f5c86fba7dcd212525ca66d0dbc2bdb8de0831cf5969e11b5bcd07847b
MD5 6df75df9ad64fb6cc30b45870bfcb177
BLAKE2b-256 266520f47fecb33aa6685a3c1201b9beebf75cf9bd4a3016ebb044806994098b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page