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 6454c537c2 - Show all commits

63
main.py
View File

@ -33,8 +33,7 @@ class Poly:
def __mul__(self, other): def __mul__(self, other):
if self.int_mod != other.int_mod: assert self.int_mod == other.int_mod
raise Exception("Different modulo")
elements = {} elements = {}
@ -63,8 +62,7 @@ class Poly:
def __add__(self, other): def __add__(self, other):
if self.int_mod != other.int_mod: assert self.int_mod == other.int_mod
raise Exception("Different modulo")
elements = self.elements.copy() elements = self.elements.copy()
@ -74,8 +72,29 @@ class Poly:
else: else:
elements[f] = other.elements[f] elements[f] = other.elements[f]
return Poly(self.int_mod, els = {}
list(reversed(list(sorted(elements.values()))))) for i in reversed(range(len(elements))):
els[f"x{i}"] = elements[f"x{i}"]
return Poly(self.int_mod, list(reversed(list(els.values()))))
def __sub__(self, other):
assert self.int_mod == other.int_mod
elements = self.elements.copy()
for e in other.elements:
if e in elements:
elements[e] -= other.elements[e]
else:
elements[e] = -other.elements[e]
els = {}
for i in reversed(range(len(elements))):
els[f"x{i}"] = elements[f"x{i}"]
return Poly(self.int_mod, list(reversed(list(els.values()))))
def __truediv__(self, other): def __truediv__(self, other):
@ -134,24 +153,36 @@ if __name__ == "__main__":
# print(c.elements) # print(c.elements)
# print(c) # print(c)
# print(p ** 2) # print(p ** 2)
d = Poly(5, [-3, 1])
# print(p) # print(p)
# print(d) # print(d)
# print(p) # print(p)
# print(p + d) # print(p + d)
d = Poly(5, [-3, 1])
g = Poly(5, [1, 2, 1]) g = Poly(5, [1, 2, 1])
print(d)
print(g)
print()
# print() # print()
# print(g ** 2) # print(g ** 2)
print(d) # print(d)
print(g) # print(g)
# c = d * g # # c = d * g
print(g * d) # print(g * d)
print(d * g) # print(d * g)
print(g + d) # print(g + d)
# print(d + g)
# print(d)
# print(g)
print(g - d)
print(d - g)
print()
print(d + g) print(d + g)
print(d) print(g + d)
print(g) # print(d)
# print(g)
# print(g - d) # print(g - d)
# print(p / d) # print(p / d)
# print(p.elements[0]) # print(p.elements[0])