Rozwiązanie zadania "Ilorazy pierścienia wielomianów" #35

Closed
s426211 wants to merge 15 commits from (deleted):zad4 into master
Showing only changes of commit 0e5e2155be - Show all commits

111
main.py
View File

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