From 94fcf4a04909d4ab5df9254e861a82112602bc9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ata=20Deniz=20Ayd=C4=B1n?= Date: Sun, 14 May 2017 17:27:28 +0300 Subject: [PATCH] --- modp.py | 27 +++++++++++++++++++++++++++ padic.py | 38 +++++++++++++++++++++++++++----------- 2 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 modp.py diff --git a/modp.py b/modp.py new file mode 100644 index 0000000..d080135 --- /dev/null +++ b/modp.py @@ -0,0 +1,27 @@ +class ModP(int): + 'Integers mod p.' + def __new__(cls, p, num): + self.p = p + return int.__new__(cls, num % p) + + # 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.' + pass \ No newline at end of file diff --git a/padic.py b/padic.py index 689108c..b4f6755 100644 --- a/padic.py +++ b/padic.py @@ -1,8 +1,15 @@ from fractions import Fraction from sys import maxint +from modp import * + +def padic(prime): + 'Return p-adic class with given prime.' + class Res(PAdic): + p = prime + return Res class PAdic: - p = 2 + p = 2 # default def __init__(self): self.val = '' # current known value self.prec = 0 # current known precision @@ -11,6 +18,17 @@ class PAdic: def get(self, prec): 'Return value of number with given precision.' + if self.value == 0: + return '0' # * prec + + while self.prec < prec: + # update val based on value + self._nextdigit() + self.prec += 1 + return self.val # TODO add decimal point or trailing zeros + + def _nextdigit(self): + 'Calculate next digit of p-adic number.' raise NotImplementedError # return value with precision up to 32 bits @@ -69,13 +87,11 @@ class PAdicConst(PAdic): value.denominator /= p self.value = value - def get(self, prec): - 'Return value of constant with given precision.' - if self.value == 0: - return self.val - - while self.prec < prec: - # update val based on value - - self.prec += 1 - return self.val # TODO add decimal point or trailing zeros \ No newline at end of file + def _nextdigit(self): + 'Calculate next digit of p-adic number.' + rem = ModP(p, self.value.numerator) / ModP(p, self.value.denominator) + self.val = rem + self.val + self.value -= rem + self.value /= p + + \ No newline at end of file