p-adic-numbers/modp.py

40 lines
1.3 KiB
Python
Raw Normal View History

2017-05-14 16:27:28 +02:00
class ModP(int):
2017-05-14 16:54:12 +02:00
'Integers mod p, p a prime power.'
2017-05-14 16:27:28 +02:00
def __new__(cls, p, num):
self.p = p
2017-05-14 18:45:18 +02:00
return int.__new__(cls, int(num) % p)
2017-05-14 16:27:28 +02:00
# arithmetic
def __add__(self, other):
return ModP(self.p, int(self) + int(other))
def __radd__(self, other):
return ModP(self.p, int(other) + int(self))
def __sub__(self, other):
return ModP(self.p, int(self) - int(other))
def __rsub__(self, other):
return ModP(self.p, int(other) - int(self))
def __mul__(self, other):
return ModP(self.p, int(self) * int(other))
def __rmul__(self, other):
return ModP(self.p, int(other) * int(self))
def __div__(self, other):
return self * other._inv()
def __rdiv__(self, other):
return other * self._inv()
def _inv(self):
'Find multiplicative inverse of self in Z mod p.'
2017-05-14 18:55:09 +02:00
# extended Euclidean algorithm
rcurr = self.p
rnext = int(self)
tcurr = 0
tnext = 1
while rnext:
q = rcurr // rnext
rcurr, rnext = rnext, rcurr - q * rnext
tcurr, tnext = tnext, tcurr - q * tnext
if rcurr != 1:
raise ValueError("%d not a unit modulo %d" % (self, self.p))
return ModP(self.p, tcurr)