p-adic-numbers/padic.py

272 lines
7.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
#
# License: GLP3+
# Copyright 2017 meagtan
# Copyright 2022 Beat Jäckle <beat@git.jdmweb2.ch>
2017-05-14 16:07:53 +02:00
from fractions import Fraction
2022-08-03 08:27:39 +02:00
from sys import maxsize
from modp import ModP
2017-05-14 16:27:28 +02:00
2017-05-14 16:07:53 +02:00
class PAdic:
def __init__(self, p, printInvers=False):
2017-05-14 16:54:12 +02:00
self.p = p
# current known value
self.digitP = [] # digitP[0]=c_0; digitP[i]=c_i
self.digitN = [] # digitN[0]=c_-1; digitN[i]=c_-1-i
2022-08-03 08:27:39 +02:00
self.prec = 0 # current known precision, not containing trailing zeros
2022-08-03 08:27:39 +02:00
self.order = 0 # order/valuation of number
self.printInvers = printInvers
return None
def calc(self, prec):
# First calculation
if len(self.digitN) + len(self.digitP) == 0:
if self.value == 0:
self.zero = True
self.digitP = [0]
return None
if self.order < 0:
absorder = abs(self.order)
self.digitN = [None]*absorder
for i in range(absorder):
self.digitN[absorder-i-1] = self._nextdigit()
if absorder >= prec:
self.prec = absorder
self.digitP.append(self._nextdigit())
elif self.order > 0:
self.digitP = [0]*self.order
while len(self.digitN) + len(self.digitP) < prec:
if self.value == 0:
break
self.digitP.append(self._nextdigit())
return None
2022-08-03 08:27:39 +02:00
def get(self, prec, decimal=True):
self.calc(prec)
2017-05-14 16:07:53 +02:00
'Return value of number with given precision.'
s = ''
for d in self.digitP[::-1]:
s += str(d)
if self.digitN:
if decimal:
s += '.'
for d in self.digitN:
s += str(d)
return s
2022-08-03 08:27:39 +02:00
2017-05-14 16:27:28 +02:00
def _nextdigit(self):
'Calculate next digit of p-adic number.'
2017-05-14 16:07:53 +02:00
raise NotImplementedError
2022-08-03 08:27:39 +02:00
2017-05-14 16:54:12 +02:00
def getdigit(self, index):
if index < self.order:
# print('not relevant 0')
return 0
# be sure to calculate the digits
prec = index+1
if self.order < 0:
prec += abs(self.order)
self.calc(prec)
# negative index
if index < 0:
try:
return self.digitN[abs(index)-1]
except IndexError:
print('Should not happen', index)
print('__dict__', self.__dict__())
raise IndexError(
f"Digit not Found\n"
f"index: {index}\n"
f"dict: {self.__dict__()}"
)
# positive index
else:
try:
return self.digitP[index]
except IndexError:
if self.value == 0:
# print('not significant',0)
return 0
else:
print('Logical Error')
print(f'Index = {index}')
print(f'pAdic = {self.__dict__()}')
raise ValueError('Logical Error')
2022-08-03 08:27:39 +02:00
2017-05-14 16:07:53 +02:00
# return value with precision up to 32 bits
def __int__(self):
2017-05-15 09:48:33 +02:00
return int(self.get(32), self.p)
2022-08-03 08:27:39 +02:00
def __getitem__(self, index):
return self.getdigit(index)
2017-05-14 16:07:53 +02:00
def __str__(self):
if self.printInvers:
return self.get(32)[::-1]
2017-05-14 16:07:53 +02:00
return self.get(32)
2022-08-03 08:27:39 +02:00
def __repr__(self):
return str(self)
2017-05-14 16:07:53 +02:00
# arithmetic operations
2017-05-15 09:48:33 +02:00
def __neg__(self):
return PAdicNeg(self.p, self)
2022-08-03 08:27:39 +02:00
2017-05-14 16:07:53 +02:00
def __add__(self, other):
2017-05-14 16:54:12 +02:00
return PAdicAdd(self.p, self, other)
2022-08-03 08:27:39 +02:00
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)
2022-08-03 08:27:39 +02:00
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))
2022-08-03 08:27:39 +02:00
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))
2022-08-03 08:27:39 +02:00
2017-05-15 09:48:33 +02:00
def __mul__(self, other):
2017-05-14 16:54:12 +02:00
return PAdicMul(self.p, self, other)
2022-08-03 08:27:39 +02:00
2017-05-15 09:48:33 +02:00
def __rmul__(self, other):
2017-05-14 16:54:12 +02:00
return PAdicMul(self.p, other, self)
2022-08-03 08:27:39 +02:00
2017-05-14 16:07:53 +02:00
# p-adic norm
def __abs__(self):
2022-08-03 08:27:39 +02:00
if self.order == maxsize:
2017-05-14 16:07:53 +02:00
return 0
2017-05-15 09:48:33 +02:00
numer = denom = 1
2017-05-14 16:07:53 +02:00
if self.order > 0:
2017-05-15 09:48:33 +02:00
numer = self.p ** self.order
2017-05-14 16:07:53 +02:00
if self.order < 0:
2017-05-15 09:48:33 +02:00
denom = self.p ** self.order
return Fraction(numer, denom)
2017-05-14 16:07:53 +02:00
# determines the p-value of an p-adic number
def pValue(self):
return self.order
def fractionvalue(self):
self.calc(1)
s = Fraction(0)
for (i, d) in enumerate([self.digitP[0]]+self.digitN):
s += Fraction(d, self.p**i)
return s
def getOffset(self):
if self.order >= 0:
return 0
else:
return -self.order
2022-08-03 08:27:39 +02:00
2017-05-14 16:07:53 +02:00
class PAdicConst(PAdic):
def __init__(self, p, value, printInvers=False):
super().__init__(p, printInvers)
2017-05-14 16:07:53 +02:00
value = Fraction(value)
2022-08-03 08:27:39 +02:00
2017-05-14 16:07:53 +02:00
# calculate valuation
if value == 0:
self.value = value
self.order = float('inf')
self.zero = True
return None
2022-08-03 08:27:39 +02:00
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-15 09:48:33 +02:00
value /= self.p
2017-05-14 16:54:12 +02:00
while not value.denominator % self.p:
2017-05-15 09:48:33 +02:00
self.order -= 1
value *= self.p
2017-05-14 16:07:53 +02:00
self.value = value
return None
2022-08-03 08:27:39 +02:00
def get(self, prec, decimal=True):
2017-05-15 09:48:33 +02:00
if self.zero:
return '0' * prec
return PAdic.get(self, prec, decimal)
2022-08-03 08:27:39 +02:00
2017-05-14 16:27:28 +02:00
def _nextdigit(self):
'Calculate next digit of p-adic number.'
rem = int(
ModP(self.p, self.value.numerator) *
ModP(self.p, self.value.denominator)._inv()
)
self.value -= rem
2017-05-14 16:54:12 +02:00
self.value /= self.p
return rem
2022-08-03 08:27:39 +02:00
2017-05-14 16:54:12 +02:00
class PAdicAdd(PAdic):
'Sum of two p-adic numbers.'
def __init__(self, p, arg1, arg2, printInvers=False):
super.__init__(self, p, printInvers)
2017-05-14 16:54:12 +02:00
self.carry = 0
_nextdigitCache = []
2017-05-14 16:54:12 +02:00
self.arg1 = arg1
self.arg2 = arg2
2022-08-03 08:27:39 +02:00
# might be larger than this
self.order = self.prec = min(arg1.order, arg2.order)
2017-05-15 09:48:33 +02:00
arg1.order -= self.order
arg2.order -= self.order
2017-05-14 18:09:52 +02:00
# loop until first nonzero digit is found
self.index = self.order
2017-05-14 18:09:52 +02:00
digit = self._nextdigit()
while digit == 0:
2017-05-15 09:48:33 +02:00
self.index += 1
2017-05-14 18:09:52 +02:00
digit = self._nextdigit()
self.order = self.index
_nextdigitCache.append(digit)
2022-08-03 08:27:39 +02:00
2017-05-14 16:54:12 +02:00
def _nextdigit(self):
if _nextdigitCache:
return _nextdigitCache.pop()
2022-08-03 08:27:39 +02:00
s = self.arg1.getdigit(self.index) + \
self.arg2.getdigit(self.index) + self.carry
2017-05-14 16:54:12 +02:00
self.carry = s // self.p
2017-05-15 09:48:33 +02:00
self.index += 1
2022-08-03 08:27:39 +02:00
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, printInvers=False):
super.__init__(self, p, printInvers)
2017-05-14 16:54:12 +02:00
self.arg = arg
2017-05-14 18:09:52 +02:00
self.order = arg.order
2022-08-03 08:27:39 +02:00
2017-05-14 16:54:12 +02:00
def _nextdigit(self):
if self.prec == 0:
2022-08-03 08:27:39 +02:00
# cannot be p, 0th digit of arg must be nonzero
return self.p - self.arg.getdigit(0 - self.getOffset())
return self.p - 1 - self.arg.getdigit(self.prec - self.getOffset())
2017-05-14 16:54:12 +02:00
2022-08-03 08:27:39 +02:00
2017-05-14 16:54:12 +02:00
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
2017-05-15 09:48:33 +02:00
self.order = arg1.order + arg2.order
2022-08-03 08:27:39 +02:00
self.arg1.order = self.arg2.order = 0 # TODO requires copy
2017-05-15 09:48:33 +02:00
self.index = 0
2022-08-03 08:27:39 +02:00
2017-05-14 16:54:12 +02:00
def _nextdigit(self):
2022-08-03 08:27:39 +02:00
s = sum(
self.arg1.getdigit(i) * self.arg2.getdigit(self.index - i)
for i in xrange(self.index + 1)
) + self.carry
2017-05-14 16:54:12 +02:00
self.carry = s // self.p
2017-05-15 09:48:33 +02:00
self.index += 1
2022-08-03 08:27:39 +02:00
return s % self.p