p-adic-numbers/hensel.py
Ata Deniz Aydın 769375e69e
2017-05-14 19:45:18 +03:00

29 lines
No EOL
1.1 KiB
Python

# Finding roots of polynomials in p-adic integers using Hensel's lemma
from padic import *
from poly import *
class PAdicPoly(PAdic):
'Result of lifting a root of a polynomial in the integers mod p to the p-adic integers.'
def __init__(self, p, poly, root):
PAdic.__init__(self, p)
self.root = root
self.poly = poly
self.deriv = derivative(poly)
# argument checks for the algorithm to work
if poly(root) % p:
raise ValueError("%d is not a root of %s modulo %d" % (root, poly, p))
if self.deriv(root) % p == 0:
raise ValueError("Polynomial %s is not separable modulo %d" % (poly, p))
# take care of trailing zeros
digit = self.root
while digit == 0:
digit = self._nextdigit()
self.order += 1
self.prec += 1
def _nextdigit(self):
self.root = ModP(self.p ** (self.order + 2), self.root)
self.root = self.root - self.poly(self.root) / self.deriv(self.root) # coercions automatically taken care of