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 098c645681 - Show all commits

185
main.py
View File

@ -115,17 +115,49 @@ 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)
els = {}
while fdeg >= sdeg:
# 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}"]:
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)]
divpoly[degree] = coefficient
divpoly = Poly(poly.int_mod, divpoly)
els[f"x{degree}"] = coefficient
mulpoly = divpoly * other
poly -= mulpoly
for i in poly.elements.keys():
if poly.elements[f"{i}"] != 0:
fdeg = int(i[1:])
break
return Poly(poly.int_mod, list(reversed(list(els.values()))))
def __mod__(self, other):
assert self.int_mod == other.int_mod
if other.is_empty():
raise ZeroDivisionError("Polynomial is empty")
poly = Poly(self.int_mod, list(reversed(list(self.elements.values()))))
fdeg = len(poly.elements) - 1
sdeg = len(other.elements) - 1
while fdeg >= sdeg:
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
@ -136,9 +168,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:])
@ -146,7 +178,9 @@ class Poly:
return poly
def __mod__(self, other):
@staticmethod
def gcd(self, other):
dividened = Poly(self.int_mod,
list(reversed(list(self.elements.values()))))
@ -154,43 +188,26 @@ class Poly:
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])
while True:
xs_zero = True
for e in div_result.elements:
if e != "x0":
if div_result.elements[e] != 0:
xs_zero = False
break
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()
# if xs_zero:
# if div_result.elements["x0"] == 1:
# break
if dividened.is_empty():
break
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
# 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):
@ -217,13 +234,75 @@ class PolyIntField:
self.elements.append(Poly(int_mod, p))
def get_nilpotents(self):
def invertibles(self):
invertibles = []
for e in self.elements:
if Poly.gcd(e, self.poly_modulo) == 1:
invertibles.append(e)
return invertibles
def nilpotents(self):
nilpotents = []
# for element in self.elements:
for e in self.elements:
if not e.is_empty():
for f in self.elements:
if not f.is_empty():
if (e ** f) % self.poly_modulo == e:
nilpotents.append(e)
return nilpotents
def idempotents(self):
idempotents = []
for e in self.elements:
if Poly.gcd((e ** 2), self.poly_modulo) == e:
idempotents.append(e)
return idempotents
def zero_divisors(self):
zero_divisors = []
for e in self.elements:
if not e.is_empty():
for f in self.elements:
if not f.is_empty():
if Poly.gcd((e * f), self.poly_modulo).is_empty():
zero_divisors.append(e)
return zero_divisors
if __name__ == "__main__":
# poly_field = PolyIntField(3, [1, 1, 2, 2])
# print(poly_field.invertibles())
# for p in poly_field.elements:
# print(p)
# print(p.elements)
# print(len(poly_field.elements))
# a = Poly(3, [0, 0, 0])
# b = Poly(3, [1, 2])
# print((a * b).is_empty())
# x = Poly(5, [1, 0, 4, 0, 2, 1])
# y = Poly(5, [4, 0, 0, 0, 1])
# print(x % y)
# o = Poly(5, [1, 0, 1, 2, 2, 1])
# print(o)
#
# p = Poly(5, [4, 0, 0, 0, 1])
# print(p)
# print(o / p)
# n = Poly(5, [1, 0, 1])
# m = Poly(5, [2, 4])
# print(n / m)
# a = Poly(5, [1, 0, 4, 0, 2, 1, 0, 0])
# # print(a)
# b = Poly(5, [4, 0, 0, 0, 1])
@ -236,16 +315,20 @@ if __name__ == "__main__":
# 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)
e = Poly(10, [-4, 0, -2, 1])
f = Poly(10, [-3, 1])
print(e / f)
print(e % f)
print(f / e)
print(f % e)
# print((a % b).elements)
# d = Poly(10, [2, 0, 6, 0, 1])