p-adic-numbers/padic.py

137 lines
4.2 KiB
Python
Raw Normal View History

2017-05-14 16:07:53 +02:00
from fractions import Fraction
from sys import maxint
2017-05-14 16:27:28 +02:00
from modp import *
2017-05-14 16:07:53 +02:00
class PAdic:
2017-05-14 16:54:12 +02:00
def __init__(self, p):
self.p = p
2017-05-14 16:07:53 +02:00
self.val = '' # current known value
2017-05-14 18:09:52 +02:00
self.prec = 0 # current known precision, also containing leading zeros
2017-05-14 16:07:53 +02:00
self.order = 0 # order/valuation of number
pass # initialize object as subclass perhaps
def get(self, prec):
'Return value of number with given precision.'
2017-05-14 16:27:28 +02:00
while self.prec < prec:
# update val based on value
2017-05-14 16:54:12 +02:00
self.val = self._nextdigit() + self.val
2017-05-14 16:27:28 +02:00
self.prec += 1
return self.val # TODO add decimal point or trailing zeros
def _nextdigit(self):
'Calculate next digit of p-adic number.'
2017-05-14 16:07:53 +02:00
raise NotImplementedError
2017-05-14 16:54:12 +02:00
def getdigit(self, index):
'Return digit at given index.'
return int(self.get(index+1)[0])
2017-05-14 16:07:53 +02:00
# return value with precision up to 32 bits
def __int__(self):
return int(self.get(32), p)
def __str__(self):
return self.get(32)
# arithmetic operations
def __add__(self, other):
2017-05-14 16:54:12 +02:00
return PAdicAdd(self.p, self, other)
2017-05-14 16:07:53 +02:00
def __radd__(self, other):
2017-05-14 16:54:12 +02:00
return PAdicAdd(self.p, other, self)
2017-05-14 16:07:53 +02:00
def __sub__(self, other):
2017-05-14 16:54:12 +02:00
return PAdicAdd(self.p, self, PAdicNeg(self.p, other))
2017-05-14 16:07:53 +02:00
def __rsub__(self, other):
2017-05-14 16:54:12 +02:00
return PAdicAdd(self.p, other, PAdicNeg(self.p, self))
2017-05-14 16:07:53 +02:00
def __smul__(self, other):
2017-05-14 16:54:12 +02:00
return PAdicMul(self.p, self, other)
2017-05-14 16:07:53 +02:00
def __rsub__(self, other):
2017-05-14 16:54:12 +02:00
return PAdicMul(self.p, other, self)
2017-05-14 16:07:53 +02:00
# p-adic norm
def __abs__(self):
if self.order == maxint:
return 0
norm = Fraction(1)
if self.order > 0:
2017-05-14 16:54:12 +02:00
norm.numerator = self.p ** self.order
2017-05-14 16:07:53 +02:00
if self.order < 0:
2017-05-14 16:54:12 +02:00
norm.denominator = self.p ** self.order
2017-05-14 16:07:53 +02:00
return norm
class PAdicConst(PAdic):
2017-05-14 16:54:12 +02:00
def __init__(self, p, value):
PAdic.__init__(self, p)
2017-05-14 16:07:53 +02:00
value = Fraction(value)
# calculate valuation
if value == 0:
self.value = value
self.val = '0'
2017-05-14 18:09:52 +02:00
self.order = maxint
2017-05-14 16:07:53 +02:00
return
self.order = 0
2017-05-14 16:54:12 +02:00
while not value.numerator % self.p:
2017-05-14 16:07:53 +02:00
self.order += 1
2017-05-14 18:09:52 +02:00
self.prec += 1
2017-05-14 16:54:12 +02:00
value.numerator /= self.p
while not value.denominator % self.p:
2017-05-14 16:07:53 +02:00
valuation -= 1
2017-05-14 16:54:12 +02:00
value.denominator /= self.p
2017-05-14 16:07:53 +02:00
self.value = value
2017-05-14 18:09:52 +02:00
def get(self, prec):
if self.value == 0:
return '0' # * prec
return PAdic.get(self, prec)
2017-05-14 16:27:28 +02:00
def _nextdigit(self):
'Calculate next digit of p-adic number.'
2017-05-14 16:54:12 +02:00
rem = ModP(self.p, self.value.numerator) / ModP(self.p, self.value.denominator)
2017-05-14 16:27:28 +02:00
self.value -= rem
2017-05-14 16:54:12 +02:00
self.value /= self.p
return rem
class PAdicAdd(PAdic):
'Sum of two p-adic numbers.'
def __init__(self, p, arg1, arg2):
PAdic.__init__(self, p)
self.carry = 0
self.arg1 = arg1
self.arg2 = arg2
2017-05-14 18:09:52 +02:00
self.order = min(arg1.order, arg2.order) # might be larger than this
# loop until first nonzero digit is found
digit = self._nextdigit()
while digit == 0:
self.order += 1
self.prec += 1
digit = self._nextdigit()
2017-05-14 16:54:12 +02:00
def _nextdigit(self):
s = self.arg1.getdigit(self.prec) + self.arg2.getdigit(self.prec) + self.carry
self.carry = s // self.p
return s % self.p
2017-05-14 16:27:28 +02:00
2017-05-14 16:54:12 +02:00
class PAdicNeg(PAdic):
'Negation of a p-adic number.'
def __init__(self, p, arg):
PAdic.__init__(self, p)
self.arg = arg
2017-05-14 18:09:52 +02:00
self.order = arg.order
2017-05-14 16:54:12 +02:00
def _nextdigit(self):
if self.prec == 0:
2017-05-14 18:09:52 +02:00
return self.p - self.arg.getdigit(0) # cannot be p, 0th digit of arg must be nonzero
2017-05-14 16:54:12 +02:00
return self.p - 1 - self.arg.getdigit(0)
class PAdicMul(PAdic):
'Product of two p-adic numbers.'
def __init__(self, p, arg1, arg2):
PAdic.__init__(self, p)
self.carry = 0
self.arg1 = arg1
self.arg2 = arg2
def _nextdigit(self):
s = sum(self.arg1.getdigit(i) * self.arg2.getdigit(self.prec - i) for i in xrange(self.prec + 1)) + self.carry
self.carry = s // self.p
return s % self.p