Skip to main content

Advanced modular arithmetic in Python

Project description

Modu is a module for doing modular arithmetic in Python. It is in essence a "domain-specific language" (DSL) dedicated to modular arithmetic. Modu is best used as an interactive calculator in Python terminal sessions (REPL) or in Jupyter Notebooks. Modu can be used as well in your Python scripts to ease calculations involving modular arithmetic. The prime target domain is research and education in number theory.

Modu allows you defining and manipulating sets of residue classes for given moduli. These sets are displayed with usual notations of modular arithmetic, using character strings, like n ≡ {0, ±2, +3} (mod 6) or rendered as LaTeX formula like $$ n \equiv \left{\begin{array}{l} 0\±2\+3 \end{array}\right. \pmod{6} $$ New sets can be computed from existing ones using set operations (union, intersection, complement), as well as arithmetic operations (addition, negation, multiplication, division, exponentiation).

Modu is an open-source module distributed under the MIT license.

Here are few examples.

  • integers having residue 0 modulo 2 (the even numbers):
>>> from modu import O
>>> O % 2
0 (mod 2)
  • integers multiples of 2 or 3, defined by set union:
>>> O%2 | O%3
{0, ±2, +3} (mod 6)
  • integers not multiples of 2 or 3, defined by complement of the previous set:
>>> ~(O%2 | O%3)
±1 (mod 6)
  • testing whether 15 and 23 belongs to the previous set (note: any prime numbers greater or equal to 5 belongs to this set):
>>> 15 in ~(O%2 | O%3)
False
>>> 23 in ~(O%2 | O%3)
True
  • integers multiples of 2 and 3 altogether, defined by set intersection--see Chinese remainder theorem:
>>> O%2 & O%3
0 (mod 6)
  • checking whether two sets are equivalent or not:
>>> O%2 | O%3 == (0, 2, 3, 4) + O%6
True
>>> O%2 & O%3 == O%6
True
>>> O%2 | O%3 != O%6
True

Modu provides more functions for normalizing the representation, tuning the display format or providing samples using table layout. Modu is much suited in REPL mode or in Jupyter Notebook, the latter performing instantaneous LaTex formula rendering.

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

modu-0.0.1.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

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

modu-0.0.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file modu-0.0.1.tar.gz.

File metadata

  • Download URL: modu-0.0.1.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for modu-0.0.1.tar.gz
Algorithm Hash digest
SHA256 4753ebe78408a880009a51a66aeefd1f63a914b296b9dda87ae3f5c22357852a
MD5 fc2c0944de692b29e9b3b6aacf4d90c4
BLAKE2b-256 a2a031176dbc055c4f844120467b112204386d36441d6e1421287f73e23a65ce

See more details on using hashes here.

File details

Details for the file modu-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: modu-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for modu-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bdaec998f0c29a256d5601fe0cb73c32691343b1b10cd04d6d01e800fe1f1fa7
MD5 7f6630ce186dd0e0ce84fda2dab46125
BLAKE2b-256 18b1298a74a23880881bd5ce83313366a74580e32b216966d905105ec58bb890

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