2018-06-26 02:05:11 +02:00
|
|
|
import itertools
|
|
|
|
|
|
|
|
|
2018-06-27 19:14:55 +02:00
|
|
|
class Poly:
|
|
|
|
|
2018-06-27 22:09:17 +02:00
|
|
|
def __init__(self, int_mod, elements):
|
2018-06-27 19:14:55 +02:00
|
|
|
self.elements = {}
|
2018-06-27 22:09:17 +02:00
|
|
|
self.int_mod = int_mod
|
2018-06-27 19:14:55 +02:00
|
|
|
|
|
|
|
i = len(elements) - 1
|
|
|
|
for e in reversed(elements):
|
2018-06-27 22:09:17 +02:00
|
|
|
self.elements[f"x{i}"] = e % self.int_mod
|
2018-06-27 19:14:55 +02:00
|
|
|
i -= 1
|
|
|
|
|
|
|
|
def __str__(self):
|
2018-06-27 22:09:17 +02:00
|
|
|
str_form = ""
|
|
|
|
deg = len(self.elements) - 1
|
|
|
|
|
|
|
|
for e in self.elements:
|
|
|
|
|
|
|
|
if self.elements[e] >= 0:
|
|
|
|
if e != f"x{deg}":
|
|
|
|
str_form += "+ "
|
|
|
|
else:
|
|
|
|
str_form += "- "
|
|
|
|
|
|
|
|
str_form += str(abs(self.elements[e]))
|
|
|
|
|
|
|
|
if e != "x0":
|
|
|
|
str_form += e[0] + "^" + e[1:] + " "
|
|
|
|
|
|
|
|
return str_form
|
2018-06-27 19:14:55 +02:00
|
|
|
|
|
|
|
def __mul__(self, other):
|
2018-06-27 22:09:17 +02:00
|
|
|
|
|
|
|
if self.int_mod != other.int_mod:
|
|
|
|
raise Exception("Different modulo")
|
|
|
|
|
2018-06-27 19:14:55 +02:00
|
|
|
elements = {}
|
|
|
|
|
|
|
|
for e in self.elements:
|
|
|
|
for f in other.elements:
|
|
|
|
coefficient = self.elements[e] * other.elements[f]
|
|
|
|
degree = f"x{int(e[1:])+int(f[1:])}"
|
|
|
|
|
|
|
|
if elements.get(f"{degree}") is None:
|
2018-06-27 22:09:17 +02:00
|
|
|
elements[degree] = coefficient % self.int_mod
|
2018-06-27 19:14:55 +02:00
|
|
|
else:
|
2018-06-27 22:09:17 +02:00
|
|
|
elements[degree] += coefficient % self.int_mod
|
2018-06-27 19:14:55 +02:00
|
|
|
|
2018-06-27 22:09:17 +02:00
|
|
|
return Poly(self.int_mod, list(reversed(list(elements.values()))))
|
2018-06-27 19:14:55 +02:00
|
|
|
|
|
|
|
def __mod__(self, other):
|
|
|
|
elements = {}
|
|
|
|
|
2018-06-27 22:09:17 +02:00
|
|
|
def __pow__(self, power, modulo=None):
|
|
|
|
poly = Poly(self.int_mod, list(reversed(list(self.elements.values()))))
|
|
|
|
|
|
|
|
for i in range(power - 1):
|
|
|
|
poly *= poly
|
|
|
|
|
|
|
|
return poly
|
|
|
|
|
|
|
|
def __add__(self, other):
|
|
|
|
|
|
|
|
if self.int_mod != other.int_mod:
|
|
|
|
raise Exception("Different modulo")
|
|
|
|
|
|
|
|
elements = self.elements.copy()
|
|
|
|
|
|
|
|
for f in other.elements:
|
|
|
|
if f in elements:
|
|
|
|
elements[f] += other.elements[f]
|
|
|
|
else:
|
|
|
|
elements[f] = other.elements[f]
|
|
|
|
|
|
|
|
return Poly(self.int_mod,
|
|
|
|
list(reversed(list(sorted(elements.values())))))
|
|
|
|
|
|
|
|
def __truediv__(self, other):
|
|
|
|
|
|
|
|
if self.int_mod != other.int_mod:
|
|
|
|
raise Exception("Different modulo")
|
|
|
|
|
|
|
|
if other.is_empty():
|
|
|
|
raise ZeroDivisionError("Polynomial is empty")
|
|
|
|
|
|
|
|
fdeg = len(self.elements) - 1
|
|
|
|
sdeg = len(other.elements) - 1
|
|
|
|
|
|
|
|
while not other.is_empty():
|
|
|
|
coefficient = self.elements[f"x{fdeg}"] / other.elements[f"x{sdeg}"]
|
|
|
|
coefficient %= self.int_mod
|
|
|
|
degree = fdeg - sdeg
|
|
|
|
|
|
|
|
# print(self.elements["x2"])
|
|
|
|
# break
|
|
|
|
|
|
|
|
def is_empty(self):
|
|
|
|
|
|
|
|
for e in self.elements:
|
|
|
|
if self.elements[e] != 0:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-06-26 02:05:11 +02:00
|
|
|
class PolyIntField:
|
|
|
|
|
2018-06-27 22:09:17 +02:00
|
|
|
def __init__(self, int_mod, poly_mod):
|
|
|
|
self.int_modulo = int_mod
|
|
|
|
self.poly_modulo = Poly(int_mod, poly_mod)
|
|
|
|
product = list(itertools.product([x for x in range(0, int_mod)],
|
|
|
|
repeat=len(poly_mod) - 1))
|
|
|
|
|
|
|
|
self.elements = []
|
|
|
|
for p in product:
|
|
|
|
p = list(p)
|
|
|
|
p = [x % int_mod for x in p]
|
|
|
|
self.elements.append(Poly(int_mod, p))
|
|
|
|
|
2018-06-26 02:05:11 +02:00
|
|
|
|
2018-06-27 19:14:55 +02:00
|
|
|
def get_nilpotents(self):
|
|
|
|
nilpotents = []
|
|
|
|
|
|
|
|
# for element in self.elements:
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-06-26 02:05:11 +02:00
|
|
|
if __name__ == "__main__":
|
2018-06-27 22:09:17 +02:00
|
|
|
p = Poly(5, [-4, 0, -2, 1])
|
|
|
|
# print(p)
|
|
|
|
# c = p * p
|
|
|
|
# print(c.elements)
|
|
|
|
# print(c)
|
|
|
|
# print(p ** 2)
|
|
|
|
d = Poly(5, [-3, 1])
|
|
|
|
# print(p)
|
|
|
|
# print(d)
|
|
|
|
# print(p)
|
|
|
|
# print(p + d)
|
|
|
|
|
|
|
|
g = Poly(5, [1, 2, 1])
|
|
|
|
# print()
|
|
|
|
# print(g ** 2)
|
|
|
|
print(d)
|
|
|
|
print(g)
|
|
|
|
# c = d * g
|
|
|
|
print(g * d)
|
|
|
|
print(d * g)
|
|
|
|
print(g + d)
|
|
|
|
print(d + g)
|
|
|
|
print(d)
|
|
|
|
print(g)
|
|
|
|
# print(g - d)
|
|
|
|
# print(p / d)
|
|
|
|
# print(p.elements[0])
|
2018-06-27 19:14:55 +02:00
|
|
|
# a = PolyIntField(3, [1, 1, 2, 2])
|
2018-06-27 22:09:17 +02:00
|
|
|
# for e in a.elements:
|
|
|
|
# print(e.elements)
|
|
|
|
|
|
|
|
# print()
|
|
|
|
# print(a.elements[4])
|
|
|
|
# print(a.elements[8])
|
|
|
|
# print(a.elements[4] * a.elements[8])
|
|
|
|
# a.elements[3] / a.elements[1]
|