Add polynomial multiplication

This commit is contained in:
Varmen8 2018-06-27 19:14:55 +02:00
parent 472f422296
commit ded142a3e6

52
main.py
View File

@ -1,15 +1,59 @@
import itertools import itertools
class Poly:
def __init__(self, elements):
self.elements = {}
i = len(elements) - 1
for e in reversed(elements):
self.elements[f"x{i}"] = e
i -= 1
def __str__(self):
return str(self.elements)
def __mul__(self, other):
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:
elements[degree] = coefficient
else:
elements[degree] += coefficient
return Poly(list(reversed(list(elements.values()))))
def __mod__(self, other):
elements = {}
class PolyIntField: class PolyIntField:
def __init__(self, int_modulo, poly_modulo): def __init__(self, int_modulo, poly_modulo):
self.int_modulo = int_modulo self.int_modulo = int_modulo
self.poly_modulo = poly_modulo self.poly_modulo = Poly(poly_modulo)
self.elements = list(itertools.product([x for x in range(0, int_modulo)] self.elements = list(itertools.product([x for x in range(0, int_modulo)]
, repeat=len(poly_modulo) - 1)) , repeat=len(poly_modulo) - 1))
def get_nilpotents(self):
nilpotents = []
# for element in self.elements:
if __name__ == "__main__": if __name__ == "__main__":
a = PolyIntField(3, [1, 1, 2, 2]) # a = PolyIntField(3, [1, 1, 2, 2])
print(a.elements) # print(a.elements)
print(len(a.elements)) # print(len(a.elements))
b = Poly([4, 8, 1, 3])
c = Poly([1, 4])
print(b.elements)
print(b * c * c)
# c = Poly()