The Install the package guide is intended to help you get started with using the zmodn library. If you are a developer who wants to contribute to the project or set up a development environment, please refer to the Code Style.

Install the package

First, you need to install the zmodn library.

The latest version of the library is available on GitHub. You can clone the repository and install the library using the following commands:

git clone
cd zmodn
pip install -e .

Basic Usage

To start using the zmodn library, import it in your Python script:

from zmodn import Zmodn

The zmodn library provides a Zmodn class that represents a set of integers modulo a given modulus. You can create a Zmodn instance by providing a list of representatives and a modulus. For example, to create a Zmodn instance representing the set of integers modulo 5 with representatives 1, 2, 3, 4, and 5, you can use the following code:

zmodn = Zmodn([1, 2, 3, 4, 5], 5)

You can create a Zmodn objects in several ways:

  1. Single element:

a = Zmodn(1, 5)
print(a)  # Output: [1] (mod 5)
  1. List of elements:

b = Zmodn([1, 2, 7], 5)
print(b)  # Output: [1 2 2] (mod 5)
  1. Matrix:

c = Zmodn([[1, 2], [3, 4]], 5)
print(c)  # Output: [[1 2] [3 4]] (mod 5)

Arithmetic Operations

You can perform arithmetic operations on Zmodn instances using standard element-wise array arithmetic operations.

  1. Addition:

a = Zmodn([1, 2, 7], 5)
b = Zmodn([3, 4, 2], 5)
print(a + b)  # Output: [4 1 4] (mod 5)
  1. Subtraction:

print(a - b)  # Output: [3 3 0] (mod 5)
  1. Multiplication:

print(a * b)  # Output: [3 3 4] (mod 5)
  1. Division:

print(a / b)  # Output: [2 3 4] (mod 5)
  1. Exponentiation:

print(a ** b)  # Output: [1 1 4] (mod 5)

Matrix Operations

The Zmodn class also supports matrix operations in modular arithmetic. You can perform matrix addition, subtraction, multiplication, exponentiation, and inversion.

  1. Matrix Addition:

a = Zmodn([[1, 2], [3, 4]], 5)
b = Zmodn([[2, 1], [4, 3]], 5)
print(a + b)  # Output: [[3 3] [2 2]] (mod 5)
  1. Matrix Subtraction:

print(a - b)  # Output: [[4 1] [4 1]] (mod 5)
  1. Matrix Multiplication:

print(a @ b)  # Output: [[10 7] [22 15]] (mod 5)
  1. Matrix Exponentiation:

print(a ** 2)  # Output: [[7 10] [15 22]] (mod 5)
  1. Matrix Inversion:

print(a.inv())  # Output: [[4 3] [2 1]] (mod 5)

Modular Inverse

For non-matrix elements, you can compute the modular inverse using the mod_inv method.

a = Zmodn(3, 5)
print(a.mod_inv())  # Output: 2 (mod 5)

Compare Elements

You can compare elements in modular arithmetic using standard comparison operators.

a = Zmodn(3, 5)
b = Zmodn(4, 5)
print(a < b)  # Output: True

Advanced Features

Indexing and Slicing:

You can access elements of a Zmodn instance using indexing and slicing.

a = Zmodn([1, 2, 3, 4, 5], 5)
print(a[0])  # Output: 1
print(a[1:3])  # Output: [2 3]

You can also iterate over the elements of a Zmodn instance using a loop.

for element in a:
    print(element)

Error Handling

The zmodn library provides error handling for common arithmetic errors, such as division by zero and invalid modulus.

a = Zmodn(3, 0)  # Raises ZeroDivisionError
b = Zmodn(3, 1)  # Raises ValueError

Best Practices

When working with the zmodn library, it is recommended to follow these best practices:

  1. Always ensure that Zmodn objects that you’re operating on have the same modulus.

  2. For matrix operations, make sure your matrices are square and invertible when necessary.


Last update: Sep 10, 2024