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 cfb71dcc8a - Show all commits

119
main.py
View File

@ -146,17 +146,7 @@ class Poly:
return Poly(poly.int_mod, list(reversed(list(els.values()))))
def __eq__(self, other):
elements_equal = True
for e in self.elements:
if e not in other.elements:
elements_equal = False
break
else:
if self.elements[e] != other.elements[e]:
elements_equal = False
break
return elements_equal and self.int_mod == other.int_mod
return self.int_mod == other.int_mod and self.elements == other.elements
def __mod__(self, other):
@ -240,12 +230,7 @@ class Poly:
return div_result
def is_empty(self):
for e in self.elements:
if self.elements[e] != 0:
return False
return True
return self == Poly(self.int_mod, [0])
class PolyIntField:
@ -266,23 +251,39 @@ class PolyIntField:
def invertibles(self):
invertibles = []
one = Poly(self.int_modulo, [1])
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).elements == {"x0": 1}:
invertibles.append(
list(reversed(list(e.elements.values()))))
break
for f in self.elements:
if e * f % self.poly_modulo == one:
invertibles.append(
list(reversed(list(e.elements.values()))))
break
return invertibles
def zero_divisors(self):
zero_divisors = [[0]]
zero = Poly(self.int_modulo, [0])
for e in self.elements:
for f in self.elements:
if e * f % self.poly_modulo == zero and e != zero and f != zero:
zero_divisors.append(
list(reversed(list(e.elements.values()))))
break
return zero_divisors
def nilpotents(self):
nilpotents = []
zero = Poly(self.int_modulo, [0])
for e in self.elements:
for n in range(1, self.int_modulo):
if ((e ** n) % self.poly_modulo).elements == {}:
if e ** n % self.poly_modulo == zero:
nilpotents.append(list(reversed(list(e.elements.values()))))
break
@ -293,26 +294,12 @@ class PolyIntField:
idempotents = []
for e in self.elements:
if ((e ** 2) % self.poly_modulo).elements == e.elements:
if e ** 2 % self.poly_modulo == e:
idempotents.append(list(reversed(list(e.elements.values()))))
idempotents[idempotents.index([])] = [0]
return idempotents
def zero_divisors(self):
zero_divisors = [[0]]
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).elements == {}:
zero_divisors.append(
list(reversed(list(e.elements.values()))))
break
return zero_divisors
def __str__(self):
str_form = "[\n\t"
str_form += str(self.invertibles()) + ", # odwracalne\n\t"
@ -325,13 +312,51 @@ class PolyIntField:
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Niepoprawny input")
exit(1)
# if len(sys.argv) < 3:
# print("Niepoprawny input")
# exit(1)
#
# if sys.argv[1].find(",") != -1:
# print(f"Proszę użyć spacji, nie przecinków: '{sys.argv[1]}'")
# exit(1)
if sys.argv[1].find(",") != -1:
print(f"Proszę użyć spacji, nie przecinków: '{sys.argv[1]}'")
exit(1)
field = PolyIntField(int(sys.argv[1]), ast.literal_eval(sys.argv[2]))
# field = PolyIntField(int(sys.argv[1]), ast.literal_eval(sys.argv[2]))
# c = Poly(2, [0, 0, 0, 1])
# # print(c)
# a = Poly(2, [1, 0, 0, 1])
# print(a == c)
# print(c == a)
# print(a + Poly(2, [1]) == c)
f = PolyIntField(2, [1, 1, 1])
print(f)
field = PolyIntField(3, [1, 1, 2, 2])
print(field)
# g = PolyIntField(3, [1, 2, 3])
# print(g)
# a = Poly(5, [1, 0, 0])
# b = Poly(5, [1])
# print(a == b)
# print(b == a)
# a = Poly(5, [1, 3])
# b = Poly(5, [1, 2])
# print(a + b)
#
# c = Poly(4, [2, 0, 3, 2])
# d = Poly(4, [2, 0, 1, 2])
# print(c)
# print(d)
# # 0x^5 + 3x^4 + 0x^3 + 0x^2 + 0x^1 + 0
# print(c * d)
# print(Poly(5, [1, 1]) == Poly(5, [1]))
# print(Poly(5, [1]) == Poly(5, [1]))
# [[0], [1, 1], [2, 1], [1, 2], [2, 2], [2, 0, 1], [0, 1, 1], [1, 1, 1], [0, 2, 1], [1, 2, 1], [1, 0, 2], [0, 1, 2], [2, 1, 2], [0, 2, 2], [2, 2, 2]],
# [[0], [0, 1, 1], [0, 1, 2], [0, 2, 1], [0, 2, 2], [1, 0, 2], [1, 1], [1, 1, 1], [1, 2], [1, 2, 1], [2, 0, 1], [2, 1], [2, 1, 2], [2, 2], [2, 2, 2]],