Skip to content

Commit

Permalink
update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
asanchezyali committed Mar 1, 2024
1 parent 174fdc8 commit 3feddd8
Show file tree
Hide file tree
Showing 9 changed files with 611 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
"editor.defaultFormatter": "charliermarsh.ruff"
},
"python.formatting.provider": "none",
"cSpell.words": [
Expand Down
7 changes: 7 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
.. py:module:: zmodn
Modular Arithmetic
==================

.. python-apigen-group:: modular-arithmetic


11 changes: 1 addition & 10 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,6 @@
autodoc_typehints_description_target = "documented"
autodoc_typehints_format = "short"

autodoc_type_aliases = {
"ElementLike": "~typing.ElementLike",
"IterableLike": "~typing.IterableLike",
"ArrayLike": "~typing.ArrayLike",
"ShapeLike": "~typing.ShapeLike",
"DTypeLike": "~typing.DTypeLike",
"PolyLike": "~typing.PolyLike",
}

ipython_execlines = ["import math", "import numpy as np", "import zmodn"]

Expand All @@ -220,7 +212,7 @@
# -- Sphinx Immaterial configs -------------------------------------------------

python_apigen_modules = {
"zmodn": "api/zmodn.",
"zmodn": "api/",
}

python_apigen_default_groups = [
Expand Down Expand Up @@ -254,7 +246,6 @@
python_apigen_show_base_classes = True

# Python domain directive configuration
python_type_aliases = autodoc_type_aliases
python_module_names_to_strip_from_xrefs = ["collections.abc"]

# General API configuration
Expand Down
4 changes: 2 additions & 2 deletions docs/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ The last stable release is available on PyPI and can be installed with pip:
pip install zmodn
zmodn.__version__
Create a :obj:`Zmodn` instance
Create a :obj:`~.zmodn.Zmodn` instance
------------------------------

The :obj:`Zmodn` class is the main interface to the library. It represents a set of integers modulo a given modulus. The
The :obj:`zmodn` class is the main interface to the library. It represents a set of integers modulo a given modulus. The
modulus must be a positive integer. The representatives of the set are given as a list of integers or a single integer.
In this example, we create a :obj:`Zmodn` instance representing the set of integers modulo 5 with representatives 1, 2,
3, 4, and 5 (which are equivalent to 1, 2, 3, 4, and 0 modulo 5, respectively):
Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,6 @@ If this library was useful to you in your research, please cite us. Following th
.. toctree::
:caption: API Reference
:hidden:
:maxdepth: 2

api.rst
558 changes: 552 additions & 6 deletions poetry.lock

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
numpy = { version = "^1.26.1", python = ">=3.11,<3.13" }


[tool.poetry.group.dev.dependencies]
pre-commit = "^3.5.0"
pytest = "^7.4.3"
pytest = "^8.0.2"
pymdown-extensions = "^10.7"
clang = "^17.0.6"
sphinx-jinja = "^2.0.2"

[tool.black]
line-length = 120
Expand Down
2 changes: 1 addition & 1 deletion zmodn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sys
from .zmodn import Zmodn
from ._zmodn import Zmodn

sys.modules["Zmodn"] = Zmodn
54 changes: 39 additions & 15 deletions zmodn/zmodn.py → zmodn/_zmodn.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@


class Zmodn:
r"""
Does not work for matrices.Computes the modular inverse of the Zmodn object using the extended Euclidean algorithm.
Group:
Modular Arithmetic
"""

def __init__(self, matrix_integers, module):
validated_matrix = validate_matrix(matrix_integers)
if not validated_matrix:
Expand Down Expand Up @@ -65,23 +72,26 @@ def _check_invertible_matrix(self, matrix):

@property
def classes(self):
return [self.__class__(int(element), self.module) for element in self.representatives]
r"""
Returns the representatives of the Zmodn object as a list.
Returns:
list: List of integers
"""
return [
self.__class__(int(element), self.module)
for element in self.representatives
]

def mod_inv(self):
r"""
Does not work for matrices
Arguments:
self (Zmodn): Zmodn object
Does not work for matrices.Computes the modular inverse of the Zmodn object using the extended Euclidean algorithm.
Returns:
Zmodn: Zmodn object
Raises:
ValueError: If the Zmodn object has more than one representative
Group:
Modular arithmetic
"""
integers_array = np.array(self.representatives).astype(int)
repr_inverse = vectorize_modular_inverse(integers_array, self.module)
Expand All @@ -94,38 +104,50 @@ def inv(self):
self._check_square_matrix(matrix)
determinant = self._check_invertible_matrix(matrix)
adjoint = adjoint_matrix(matrix).astype(int)
multiplier = int(self.__class__(1, self.module) / self.__class__(determinant, self.module))
multiplier = int(
self.__class__(1, self.module) / self.__class__(determinant, self.module)
)
inverse_matrix = multiplier * adjoint
return self.__class__(inverse_matrix.tolist(), self.module)

@implements(np.add)
def __add__(self, other):
self._check_module_and_type(other)
repr_sum = (np.array(self.representatives) + np.array(other.representatives)) % self.module
repr_sum = (
np.array(self.representatives) + np.array(other.representatives)
) % self.module
return self.__class__(repr_sum.tolist(), self.module)

@implements(np.subtract)
def __sub__(self, other):
self._check_module_and_type(other)
repr_sub = (np.array(self.representatives) - np.array(other.representatives)) % self.module
repr_sub = (
np.array(self.representatives) - np.array(other.representatives)
) % self.module
return self.__class__(repr_sub.tolist(), self.module)

@implements(np.multiply)
def __mul__(self, other):
self._check_module_and_type(other)
repr_mul = (np.array(self.representatives) * np.array(other.representatives)) % self.module
repr_mul = (
np.array(self.representatives) * np.array(other.representatives)
) % self.module
return self.__class__(repr_mul.tolist(), self.module)

@implements(np.dot)
def __matmul__(self, other):
self._check_module_and_type(other)
repr_mul = (np.array(self.representatives) @ np.array(other.representatives)) % self.module
repr_mul = (
np.array(self.representatives) @ np.array(other.representatives)
) % self.module
return self.__class__(repr_mul.tolist(), self.module)

@implements(np.divide)
def __truediv__(self, other):
self._check_module_and_type(other)
repr_div = (np.array(self.representatives) * np.array(other.mod_inv().representatives)) % self.module
repr_div = (
np.array(self.representatives) * np.array(other.mod_inv().representatives)
) % self.module
return self.__class__(repr_div.tolist(), self.module)

@implements(np.power)
Expand Down Expand Up @@ -204,5 +226,7 @@ def __bool__(self):

def __int__(self):
if self.representatives.size != 1:
raise ValueError("Cannot convert Zmodn object with more than one representative to an integer")
raise ValueError(
"Cannot convert Zmodn object with more than one representative to an integer"
)
return int(self.representatives[0])

0 comments on commit 3feddd8

Please sign in to comment.