Commit 4f1fc23e authored by Martins Kalis's avatar Martins Kalis
Browse files

Barebones example of Z.

parent 0aa71117
%% Cell type:markdown id: tags:
# Chapter 1: Field axioms
The initial contact with different algebraic structures can seem abstract; however, it is in fact somewhat easier for programmers and computer scientists, since we often have to implement some of these aspects in code, which can feel more concrete.
Let's see a very simplified example of implementing integer addition and multiplication modulo some number $p$, using the [Python data model](https://docs.python.org/3.7/reference/datamodel.html#emulating-numeric-types). To see the full set of field axioms, please see the lecture notes.
%% Cell type:code id: tags:
``` python
class Z:
"""A class representing numbers modulo p."""
def __init__(self, n, p):
self.p = p
self.n = n % p
def __repr__(self):
return str(self.n)
def __add__(self, other):
return (self.n + other.n) % self.p
def __mul__(self, other):
return (self.n * other.n) % self.p
```
%% Cell type:code id: tags:
``` python
Z(9, 7) + Z(3, 7)
```
%% Output
5
%% Cell type:code id: tags:
``` python
Z(9, 7) * Z(3, 7)
```
%% Output
6
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment