Skip to main content

Modular arithmetic in Python

Project description

Modu is a mathematical module for doing modular arithmetic in Python. Using operator overloading, it provides an experience similar to a domain-specific language (DSL). Modu is best used as an interactive calculator in Python terminal sessions (REPL) or in Jupyter Notebooks with instantaneous rendering as LaTeX formulas. 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.

In a nutshell, 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 using LaTeX formula rendering. New sets can be computed from existing ones using set operations (union, intersection, complement), as well as arithmetic operations (addition, negation, multiplication, division, exponentiation). Samples of integers can be obtained, displayed as tables (aligning same residues in same columns) and possibly transforming the elements by user-defined functions.

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 belong to the previous set (note: any prime number 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, changing the display format or providing samples using table layout.

All those features are demonstrated in an interactive tutorial, the "Modutorial" Jupyter Notebook (modutorial.ipynb)**. This can be opened directly online by clicking here (binder):

Binder

(be patient: you have to wait a bit...)

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.1.0.tar.gz (8.0 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.1.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for modu-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b7843f276a384247c16b0e70ecfe8800917ec84dd2b590fd22fe50611bdf249b
MD5 b7e5920e8b794c1750d27235ba4ae738
BLAKE2b-256 9a9d0e90e4cc3de5cbc867d78592b88b7534a744700b57c4da22c5be5a99f0d2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modu-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 194feec28e0529a6c49b0dc9b6b1c1e9d6714c909fa79cffa05e393de1a6ef8d
MD5 3e17ec3d167d6b8f4aa7b03bc73f1174
BLAKE2b-256 ce19fb7f1e8acabd062e0e8ac3ac0eb6de05c667d7d4419ad9d911f695c4bee9

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