Fix polynomial division

This commit is contained in:
Varmen8 2018-06-28 01:46:46 +02:00
parent 64b400ba77
commit 0e5e2155be
1 changed files with 97 additions and 14 deletions

111
main.py
View File

@ -7,8 +7,16 @@ class Poly:
self.elements = {}
self.int_mod = int_mod
elements = list(reversed(elements))
z = 0
for i in elements:
if i != 0:
break
z += 1
elements = elements[z:]
i = len(elements) - 1
for e in reversed(elements):
for e in elements:
self.elements[f"x{i}"] = e % self.int_mod
i -= 1
@ -53,9 +61,6 @@ class Poly:
return Poly(self.int_mod, list(reversed(list(els.values()))))
def __mod__(self, other):
elements = {}
def __pow__(self, power, modulo=None):
poly = Poly(self.int_mod, list(reversed(list(self.elements.values()))))
@ -110,10 +115,20 @@ class Poly:
poly = Poly(self.int_mod, list(reversed(list(self.elements.values()))))
fdeg = len(poly.elements) - 1
sdeg = len(other.elements) - 1
# print(poly)
# print(poly.elements)
# print(fdeg)
# print(sdeg)
while fdeg >= sdeg:
coefficient = poly.elements[f"x{fdeg}"] / other.elements[f"x{sdeg}"]
coefficient %= poly.int_mod
# coefficient = poly.elements[f"x{fdeg}"] / other.elements[f"x{sdeg}"]
# coefficient %= poly.int_mod
# coefficient = 0
coefficient = other.elements[f"x{sdeg}"]
for i in range(1, poly.int_mod):
if (coefficient * i) % poly.int_mod == poly.elements[f"x{fdeg}"]:
coefficient = i
break
degree = fdeg - sdeg
divpoly = [0 for x in range(degree + 1)]
@ -121,9 +136,9 @@ class Poly:
divpoly = Poly(poly.int_mod, divpoly)
mulpoly = divpoly * other
# print(poly)
poly -= mulpoly
# print(poly)
for i in poly.elements.keys():
if poly.elements[f"{i}"] != 0:
fdeg = int(i[1:])
@ -131,6 +146,53 @@ class Poly:
return poly
def __mod__(self, other):
dividened = Poly(self.int_mod,
list(reversed(list(self.elements.values()))))
divisor = Poly(self.int_mod,
list(reversed(list(other.elements.values()))))
div_result = dividened / divisor
els = []
for e in div_result.elements.keys():
if div_result.elements[e] != 0:
els.append(div_result.elements[e])
div_result = Poly(self.int_mod, list(reversed(list(els))))
# #
# # print(dividened)
# # print(divisor)
# # print(div_result)
# #
# print(div_result.elements)
# dividened = Poly(dividened.int_mod,
# list(reversed(list(divisor.elements.values()))))
# divisor = Poly(divisor.int_mod,
# list(reversed(list(div_result.elements.values()))))
# print(dividened)
# print(divisor)
# div_result = dividened / divisor
#
# print()
# print(dividened)
# print(divisor)
# print(div_result)
# print()
# while True:
# dividened = Poly(dividened.int_mod,
# list(reversed(list(divisor.elements.values()))))
# divisor = Poly(divisor.int_mod,
# list(reversed(list(div_result.elements.values()))))
# div_result = dividened / divisor
#
# if div_result.is_empty():
# break
# print(div_result)
return div_result
def is_empty(self):
for e in self.elements:
@ -161,13 +223,34 @@ class PolyIntField:
# for element in self.elements:
if __name__ == "__main__":
# a = Poly(5, [1, 0, 4, 0, 2, 1, 0, 0])
# # print(a)
# b = Poly(5, [4, 0, 0, 0, 1])
# # print(b)
# # print(a + b)
# print(a % b)
a = Poly(10, [-4, 0, -2, 1])
b = Poly(10, [-3, 1])
c = a/b
print(c)
# d = Poly(5, [3, 1, 4])
# # print(d)
# e = Poly(5, [4, 0, 0, 0, 1])
# # print(e)
# print(e / d)
# c = Poly(5, [4, 0, 0, 0, 1])
# d = Poly(5, [3, 1, 4, 0, 0, 0])
# print(c)
# print(d)
# print(c % d)
# e = Poly(10, [-4, 0, -2, 1])
# f = Poly(10, [-3, 1])
# print(e / f)
# print((a % b).elements)
# d = Poly(10, [2, 0, 6, 0, 1])
# e = Poly(10, [5, 0, 1])
# print(d / e)
# print(a - Poly(10, [0, 0, -3, 1]))
# p = Poly(5, [-4, 0, -2, 1])
# print(p)