From 472f4222967f81c5f6c2832ad2e718899d524116 Mon Sep 17 00:00:00 2001 From: Varmen8 Date: Tue, 26 Jun 2018 02:05:11 +0200 Subject: [PATCH 01/14] Add class definition --- main.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..87fe260 --- /dev/null +++ b/main.py @@ -0,0 +1,15 @@ +import itertools + + +class PolyIntField: + + def __init__(self, int_modulo, poly_modulo): + self.int_modulo = int_modulo + self.poly_modulo = poly_modulo + self.elements = list(itertools.product([x for x in range(0, int_modulo)] + , repeat=len(poly_modulo) - 1)) + +if __name__ == "__main__": + a = PolyIntField(3, [1, 1, 2, 2]) + print(a.elements) + print(len(a.elements)) \ No newline at end of file -- 2.20.1 From ded142a3e6c9600b70ba6bf3dad52e2fd2193301 Mon Sep 17 00:00:00 2001 From: Varmen8 Date: Wed, 27 Jun 2018 19:14:55 +0200 Subject: [PATCH 02/14] Add polynomial multiplication --- main.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 87fe260..3d418e8 100644 --- a/main.py +++ b/main.py @@ -1,15 +1,59 @@ import itertools +class Poly: + + def __init__(self, elements): + self.elements = {} + + i = len(elements) - 1 + for e in reversed(elements): + self.elements[f"x{i}"] = e + i -= 1 + + def __str__(self): + return str(self.elements) + + def __mul__(self, other): + elements = {} + + for e in self.elements: + for f in other.elements: + coefficient = self.elements[e] * other.elements[f] + degree = f"x{int(e[1:])+int(f[1:])}" + + if elements.get(f"{degree}") is None: + elements[degree] = coefficient + else: + elements[degree] += coefficient + + return Poly(list(reversed(list(elements.values())))) + + def __mod__(self, other): + elements = {} + class PolyIntField: def __init__(self, int_modulo, poly_modulo): self.int_modulo = int_modulo - self.poly_modulo = poly_modulo + self.poly_modulo = Poly(poly_modulo) self.elements = list(itertools.product([x for x in range(0, int_modulo)] , repeat=len(poly_modulo) - 1)) + def get_nilpotents(self): + nilpotents = [] + + # for element in self.elements: + + + if __name__ == "__main__": - a = PolyIntField(3, [1, 1, 2, 2]) - print(a.elements) - print(len(a.elements)) \ No newline at end of file + # a = PolyIntField(3, [1, 1, 2, 2]) + # print(a.elements) + # print(len(a.elements)) + b = Poly([4, 8, 1, 3]) + c = Poly([1, 4]) + + print(b.elements) + print(b * c * c) + # c = Poly() \ No newline at end of file -- 2.20.1 From 437428d6aaef164d5799800973c6a3c302241f1c Mon Sep 17 00:00:00 2001 From: Varmen8 Date: Wed, 27 Jun 2018 22:09:17 +0200 Subject: [PATCH 03/14] Add polynomial addition --- main.py | 145 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 126 insertions(+), 19 deletions(-) diff --git a/main.py b/main.py index 3d418e8..d0f033f 100644 --- a/main.py +++ b/main.py @@ -3,18 +3,39 @@ import itertools class Poly: - def __init__(self, elements): + def __init__(self, int_mod, elements): self.elements = {} + self.int_mod = int_mod i = len(elements) - 1 for e in reversed(elements): - self.elements[f"x{i}"] = e + self.elements[f"x{i}"] = e % self.int_mod i -= 1 def __str__(self): - return str(self.elements) + str_form = "" + deg = len(self.elements) - 1 + + for e in self.elements: + + if self.elements[e] >= 0: + if e != f"x{deg}": + str_form += "+ " + else: + str_form += "- " + + str_form += str(abs(self.elements[e])) + + if e != "x0": + str_form += e[0] + "^" + e[1:] + " " + + return str_form def __mul__(self, other): + + if self.int_mod != other.int_mod: + raise Exception("Different modulo") + elements = {} for e in self.elements: @@ -23,22 +44,81 @@ class Poly: degree = f"x{int(e[1:])+int(f[1:])}" if elements.get(f"{degree}") is None: - elements[degree] = coefficient + elements[degree] = coefficient % self.int_mod else: - elements[degree] += coefficient + elements[degree] += coefficient % self.int_mod - return Poly(list(reversed(list(elements.values())))) + return Poly(self.int_mod, list(reversed(list(elements.values())))) def __mod__(self, other): elements = {} + def __pow__(self, power, modulo=None): + poly = Poly(self.int_mod, list(reversed(list(self.elements.values())))) + + for i in range(power - 1): + poly *= poly + + return poly + + def __add__(self, other): + + if self.int_mod != other.int_mod: + raise Exception("Different modulo") + + elements = self.elements.copy() + + for f in other.elements: + if f in elements: + elements[f] += other.elements[f] + else: + elements[f] = other.elements[f] + + return Poly(self.int_mod, + list(reversed(list(sorted(elements.values()))))) + + def __truediv__(self, other): + + if self.int_mod != other.int_mod: + raise Exception("Different modulo") + + if other.is_empty(): + raise ZeroDivisionError("Polynomial is empty") + + fdeg = len(self.elements) - 1 + sdeg = len(other.elements) - 1 + + while not other.is_empty(): + coefficient = self.elements[f"x{fdeg}"] / other.elements[f"x{sdeg}"] + coefficient %= self.int_mod + degree = fdeg - sdeg + + # print(self.elements["x2"]) + # break + + def is_empty(self): + + for e in self.elements: + if self.elements[e] != 0: + return False + + return True + + class PolyIntField: - def __init__(self, int_modulo, poly_modulo): - self.int_modulo = int_modulo - self.poly_modulo = Poly(poly_modulo) - self.elements = list(itertools.product([x for x in range(0, int_modulo)] - , repeat=len(poly_modulo) - 1)) + def __init__(self, int_mod, poly_mod): + self.int_modulo = int_mod + self.poly_modulo = Poly(int_mod, poly_mod) + product = list(itertools.product([x for x in range(0, int_mod)], + repeat=len(poly_mod) - 1)) + + self.elements = [] + for p in product: + p = list(p) + p = [x % int_mod for x in p] + self.elements.append(Poly(int_mod, p)) + def get_nilpotents(self): nilpotents = [] @@ -48,12 +128,39 @@ class PolyIntField: if __name__ == "__main__": - # a = PolyIntField(3, [1, 1, 2, 2]) - # print(a.elements) - # print(len(a.elements)) - b = Poly([4, 8, 1, 3]) - c = Poly([1, 4]) + p = Poly(5, [-4, 0, -2, 1]) + # print(p) + # c = p * p + # print(c.elements) + # print(c) + # print(p ** 2) + d = Poly(5, [-3, 1]) + # print(p) + # print(d) + # print(p) + # print(p + d) - print(b.elements) - print(b * c * c) - # c = Poly() \ No newline at end of file + g = Poly(5, [1, 2, 1]) + # print() + # print(g ** 2) + print(d) + print(g) + # c = d * g + print(g * d) + print(d * g) + print(g + d) + print(d + g) + print(d) + print(g) + # print(g - d) + # print(p / d) + # print(p.elements[0]) + # a = PolyIntField(3, [1, 1, 2, 2]) + # for e in a.elements: + # print(e.elements) + + # print() + # print(a.elements[4]) + # print(a.elements[8]) + # print(a.elements[4] * a.elements[8]) + # a.elements[3] / a.elements[1] -- 2.20.1 From 6454c537c2c714b2fcb2d0260bf12e6cf6675d77 Mon Sep 17 00:00:00 2001 From: Varmen8 Date: Wed, 27 Jun 2018 23:04:58 +0200 Subject: [PATCH 04/14] Add polynomial subtraction --- main.py | 63 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index d0f033f..adff4ae 100644 --- a/main.py +++ b/main.py @@ -33,8 +33,7 @@ class Poly: def __mul__(self, other): - if self.int_mod != other.int_mod: - raise Exception("Different modulo") + assert self.int_mod == other.int_mod elements = {} @@ -63,8 +62,7 @@ class Poly: def __add__(self, other): - if self.int_mod != other.int_mod: - raise Exception("Different modulo") + assert self.int_mod == other.int_mod elements = self.elements.copy() @@ -74,8 +72,29 @@ class Poly: else: elements[f] = other.elements[f] - return Poly(self.int_mod, - list(reversed(list(sorted(elements.values()))))) + 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 __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): @@ -134,24 +153,36 @@ if __name__ == "__main__": # print(c.elements) # print(c) # print(p ** 2) - d = Poly(5, [-3, 1]) + # print(p) # print(d) # print(p) # print(p + d) - + d = Poly(5, [-3, 1]) g = Poly(5, [1, 2, 1]) + print(d) + print(g) + print() + # print() # print(g ** 2) - print(d) - print(g) - # c = d * g - print(g * d) - print(d * g) - print(g + d) + # print(d) + # print(g) + # # c = d * g + # print(g * d) + # print(d * g) + # print(g + d) + # print(d + g) + # print(d) + # print(g) + print(g - d) + print(d - g) + + print() print(d + g) - print(d) - print(g) + print(g + d) + # print(d) + # print(g) # print(g - d) # print(p / d) # print(p.elements[0]) -- 2.20.1 From 64b400ba779b4e6fd8d67309a5e1b57cf90ef652 Mon Sep 17 00:00:00 2001 From: Varmen8 Date: Thu, 28 Jun 2018 00:06:30 +0200 Subject: [PATCH 05/14] Add polynomial division --- main.py | 78 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/main.py b/main.py index adff4ae..3361fc7 100644 --- a/main.py +++ b/main.py @@ -47,7 +47,11 @@ class Poly: else: elements[degree] += coefficient % self.int_mod - return Poly(self.int_mod, list(reversed(list(elements.values())))) + 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 __mod__(self, other): elements = {} @@ -98,22 +102,34 @@ class Poly: def __truediv__(self, other): - if self.int_mod != other.int_mod: - raise Exception("Different modulo") + assert self.int_mod == other.int_mod if other.is_empty(): raise ZeroDivisionError("Polynomial is empty") - fdeg = len(self.elements) - 1 + poly = Poly(self.int_mod, list(reversed(list(self.elements.values())))) + fdeg = len(poly.elements) - 1 sdeg = len(other.elements) - 1 - while not other.is_empty(): - coefficient = self.elements[f"x{fdeg}"] / other.elements[f"x{sdeg}"] - coefficient %= self.int_mod + while fdeg >= sdeg: + coefficient = poly.elements[f"x{fdeg}"] / other.elements[f"x{sdeg}"] + coefficient %= poly.int_mod degree = fdeg - sdeg - # print(self.elements["x2"]) - # break + divpoly = [0 for x in range(degree + 1)] + divpoly[degree] = coefficient + divpoly = Poly(poly.int_mod, divpoly) + + mulpoly = divpoly * other + + poly -= mulpoly + + for i in poly.elements.keys(): + if poly.elements[f"{i}"] != 0: + fdeg = int(i[1:]) + break + + return poly def is_empty(self): @@ -147,7 +163,13 @@ class PolyIntField: if __name__ == "__main__": - p = Poly(5, [-4, 0, -2, 1]) + + a = Poly(10, [-4, 0, -2, 1]) + b = Poly(10, [-3, 1]) + c = a/b + print(c) + # print(a - Poly(10, [0, 0, -3, 1])) + # p = Poly(5, [-4, 0, -2, 1]) # print(p) # c = p * p # print(c.elements) @@ -158,29 +180,23 @@ if __name__ == "__main__": # print(d) # print(p) # print(p + d) - d = Poly(5, [-3, 1]) - g = Poly(5, [1, 2, 1]) - print(d) - print(g) - print() - + # d = Poly(5, [-3, 1]) + # g = Poly(5, [1, 2, 1]) + # print(d) + # print(g) + # print() + # + # # print(g) + # print(g - d) + # print(d - g) + # # print() - # print(g ** 2) - # print(d) - # print(g) - # # c = d * g - # print(g * d) - # print(d * g) - # print(g + d) # print(d + g) - # print(d) - # print(g) - print(g - d) - print(d - g) - - print() - print(d + g) - print(g + d) + # print(g + d) + # + # print() + # print(d * g) + # print(g * d) # print(d) # print(g) # print(g - d) -- 2.20.1 From 0e5e2155bec0c005aca51463040f23e1c4423af0 Mon Sep 17 00:00:00 2001 From: Varmen8 Date: Thu, 28 Jun 2018 01:46:46 +0200 Subject: [PATCH 06/14] Fix polynomial division --- main.py | 111 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 97 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 3361fc7..9fc5646 100644 --- a/main.py +++ b/main.py @@ -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) -- 2.20.1 From 098c64568163beebe5edfce9b40e0e4506430a7a Mon Sep 17 00:00:00 2001 From: Varmen8 Date: Thu, 28 Jun 2018 13:16:51 +0200 Subject: [PATCH 07/14] Change division and modulo return value --- main.py | 185 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 134 insertions(+), 51 deletions(-) diff --git a/main.py b/main.py index 9fc5646..52de692 100644 --- a/main.py +++ b/main.py @@ -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]) -- 2.20.1 From 0fa7c9445c640dcfd1d5e05c74dc7d2a7f07c24f Mon Sep 17 00:00:00 2001 From: Varmen8 Date: Thu, 28 Jun 2018 15:02:10 +0200 Subject: [PATCH 08/14] Fix idempotent elements --- main.py | 127 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 103 insertions(+), 24 deletions(-) diff --git a/main.py b/main.py index 52de692..57387a6 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ import itertools +import math class Poly: @@ -156,7 +157,11 @@ class Poly: while fdeg >= sdeg: coefficient = other.elements[f"x{sdeg}"] + if poly.is_empty(): + break for i in range(1, poly.int_mod): + # print("I") + # print(poly.elements) if coefficient * i % poly.int_mod == poly.elements[f"x{fdeg}"]: coefficient = i break @@ -170,7 +175,7 @@ class Poly: mulpoly = divpoly * other poly -= mulpoly - + # print(poly) for i in poly.elements.keys(): if poly.elements[f"{i}"] != 0: fdeg = int(i[1:]) @@ -178,7 +183,14 @@ class Poly: return poly - + # def __eq__(self, other): + # equal = True + # for e in self.elements: + # if e not in other.elements: + # equal = False + # break + # + # return equal @staticmethod def gcd(self, other): @@ -186,27 +198,37 @@ class Poly: list(reversed(list(self.elements.values())))) divisor = Poly(self.int_mod, list(reversed(list(other.elements.values())))) - div_result = dividened / divisor + + if len(self.elements) < len(other.elements): + dividened, divisor = divisor, dividened + + # div_result = dividened % divisor + + # if math.gcd(self.int_mod, + # dividened.elements[f"x{len(dividened.elements) - 1}"]) == 1: + # if dividened.elements[f"x{len(dividened.elements) - 1}"] == 1: + # raise Exception("Leading dividened coefficient not invertible") + div_result = dividened % divisor while True: - xs_zero = True - for e in div_result.elements: - if e != "x0": - if div_result.elements[e] != 0: - xs_zero = False - break - + # xs_zero = True + # for e in div_result.elements: + # if e != "x0": + # if div_result.elements[e] != 0: + # xs_zero = False + # break + # # if xs_zero: # if div_result.elements["x0"] == 1: # break - if dividened.is_empty(): + if div_result.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 + div_result = dividened % divisor return div_result @@ -238,8 +260,9 @@ class PolyIntField: invertibles = [] for e in self.elements: - if Poly.gcd(e, self.poly_modulo) == 1: - invertibles.append(e) + if not e.is_empty(): + if Poly.gcd(e, self.poly_modulo) == 1: + invertibles.append(e) return invertibles @@ -259,9 +282,10 @@ class PolyIntField: idempotents = [] for e in self.elements: - if Poly.gcd((e ** 2), self.poly_modulo) == e: - idempotents.append(e) + if ((e ** 2) % self.poly_modulo).elements == e.elements: + idempotents.append(list(reversed(list(e.elements.values())))) + idempotents[idempotents.index([])] = [0] return idempotents def zero_divisors(self): @@ -276,11 +300,35 @@ class PolyIntField: return zero_divisors + def __str__(self): + str_form = "[\n\t" + str_form += str([]) + "\n\t" + str_form += str([]) + "\n\t" + str_form += str([]) + "\n\t" + str_form += str(self.idempotents()) + "\n" + str_form += "]\n" + return str_form if __name__ == "__main__": - # poly_field = PolyIntField(3, [1, 1, 2, 2]) - # print(poly_field.invertibles()) + poly_field = PolyIntField(3, [1, 1, 2, 2]) + print(poly_field) + # x = poly_field.idempotents() + # for e in x: + # print(e) + # + # print() + pf = PolyIntField(2, [1, 1, 1]) + print(pf) + # y = pf.idempotents() + # for i in y: + # print(i) + # y = Poly(3, [0, 1, 2]) + # d = Poly(3, [1, 1, 2, 2]) + # # print(y ** 2 % d) + # h = y ** 2 + # print(h) + # print(h % d) # for p in poly_field.elements: # print(p) # print(p.elements) @@ -323,12 +371,43 @@ if __name__ == "__main__": # print(d) # print(c % d) - e = Poly(10, [-4, 0, -2, 1]) - f = Poly(10, [-3, 1]) - print(e / f) - print(e % f) - print(f / e) - print(f % e) + # e = Poly(10, [-4, 0, -2, 3]) + # f = Poly(10, [-3, 1]) + # print(e / f) + # print(e % f) + # print(f / e) + # print(f % e) + # print(e * f) + # print(f * e) + # print(e + f) + # print(f + e) + # print(e - f) + # print(f - e) + # print(e) + # print(f) + # print(Poly.gcd(e, f)) + + # a = Poly(5, [1, 0, 1, 0, 2, 1]) + # b = Poly(5, [4, 0, 0, 0, 1]) + # print(a) + # print(b) + # # print(Poly.gcd(a, b)) + # print(a % b) + # c = a % b + # print(b % c) + # print(Poly.gcd(a, b)) + # p = Poly(4, [6, 0, 8, 3]) + # o = Poly(4, [7, 1]) + # print(Poly.gcd(o, p)) + # print(Poly.gcd(b, a)) + + # t = Poly(5, [1, 0, 4, 0, 2, 1]) + # y = Poly(5, [4, 0, 0, 0, 1]) + # print(Poly.gcd(t, y)) + + + + # print((a % b).elements) # d = Poly(10, [2, 0, 6, 0, 1]) -- 2.20.1 From 272a361fb22a10669f628f946d96c0d9119a654e Mon Sep 17 00:00:00 2001 From: Varmen8 Date: Thu, 28 Jun 2018 15:24:51 +0200 Subject: [PATCH 09/14] Fix nilpotents --- main.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index 57387a6..69773e2 100644 --- a/main.py +++ b/main.py @@ -270,12 +270,12 @@ class PolyIntField: nilpotents = [] 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) + for n in range(1, self.int_modulo): + if ((e ** n) % self.poly_modulo).elements == {}: + nilpotents.append(list(reversed(list(e.elements.values())))) + break + nilpotents[nilpotents.index([])] = [0] return nilpotents def idempotents(self): @@ -304,22 +304,21 @@ class PolyIntField: str_form = "[\n\t" str_form += str([]) + "\n\t" str_form += str([]) + "\n\t" - str_form += str([]) + "\n\t" + str_form += str(self.nilpotents()) + "\n\t" str_form += str(self.idempotents()) + "\n" str_form += "]\n" return str_form + if __name__ == "__main__": + + pf = PolyIntField(2, [1, 1, 1]) + print(pf) + poly_field = PolyIntField(3, [1, 1, 2, 2]) print(poly_field) - # x = poly_field.idempotents() - # for e in x: - # print(e) - # - # print() - pf = PolyIntField(2, [1, 1, 1]) - print(pf) + # print(poly_field) # y = pf.idempotents() # for i in y: # print(i) -- 2.20.1 From 1a958042e15eb9076b1609cab6f05e0f7786c677 Mon Sep 17 00:00:00 2001 From: Varmen8 Date: Thu, 28 Jun 2018 15:36:22 +0200 Subject: [PATCH 10/14] Fix zero divisors --- main.py | 152 +++----------------------------------------------------- 1 file changed, 8 insertions(+), 144 deletions(-) diff --git a/main.py b/main.py index 69773e2..a7d74d5 100644 --- a/main.py +++ b/main.py @@ -289,22 +289,24 @@ class PolyIntField: return idempotents def zero_divisors(self): - zero_divisors = [] + zero_divisors = [[0]] 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) + 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([]) + "\n\t" - str_form += str([]) + "\n\t" - str_form += str(self.nilpotents()) + "\n\t" + str_form += str([]) + ",\n\t" + str_form += str(self.zero_divisors()) + ",\n\t" + str_form += str(self.nilpotents()) + ",\n\t" str_form += str(self.idempotents()) + "\n" str_form += "]\n" return str_form @@ -312,146 +314,8 @@ class PolyIntField: if __name__ == "__main__": - pf = PolyIntField(2, [1, 1, 1]) print(pf) poly_field = PolyIntField(3, [1, 1, 2, 2]) print(poly_field) - # print(poly_field) - # y = pf.idempotents() - # for i in y: - # print(i) - # y = Poly(3, [0, 1, 2]) - # d = Poly(3, [1, 1, 2, 2]) - # # print(y ** 2 % d) - # h = y ** 2 - # print(h) - # print(h % d) - # 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]) - # # print(b) - # # print(a + b) - # print(a % b) - - # 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, 3]) - # f = Poly(10, [-3, 1]) - # print(e / f) - # print(e % f) - # print(f / e) - # print(f % e) - # print(e * f) - # print(f * e) - # print(e + f) - # print(f + e) - # print(e - f) - # print(f - e) - # print(e) - # print(f) - # print(Poly.gcd(e, f)) - - # a = Poly(5, [1, 0, 1, 0, 2, 1]) - # b = Poly(5, [4, 0, 0, 0, 1]) - # print(a) - # print(b) - # # print(Poly.gcd(a, b)) - # print(a % b) - # c = a % b - # print(b % c) - # print(Poly.gcd(a, b)) - # p = Poly(4, [6, 0, 8, 3]) - # o = Poly(4, [7, 1]) - # print(Poly.gcd(o, p)) - # print(Poly.gcd(b, a)) - - # t = Poly(5, [1, 0, 4, 0, 2, 1]) - # y = Poly(5, [4, 0, 0, 0, 1]) - # print(Poly.gcd(t, y)) - - - - - - # 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) - # c = p * p - # print(c.elements) - # print(c) - # print(p ** 2) - - # print(p) - # print(d) - # print(p) - # print(p + d) - # d = Poly(5, [-3, 1]) - # g = Poly(5, [1, 2, 1]) - # print(d) - # print(g) - # print() - # - # # print(g) - # print(g - d) - # print(d - g) - # - # print() - # print(d + g) - # print(g + d) - # - # print() - # print(d * g) - # print(g * d) - # print(d) - # print(g) - # print(g - d) - # print(p / d) - # print(p.elements[0]) - # a = PolyIntField(3, [1, 1, 2, 2]) - # for e in a.elements: - # print(e.elements) - - # print() - # print(a.elements[4]) - # print(a.elements[8]) - # print(a.elements[4] * a.elements[8]) - # a.elements[3] / a.elements[1] -- 2.20.1 From ee39f1f88d690221c1337c28709ce1153a3be9d1 Mon Sep 17 00:00:00 2001 From: Varmen8 Date: Thu, 28 Jun 2018 18:05:43 +0200 Subject: [PATCH 11/14] "Fix" invertibles --- main.py | 64 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/main.py b/main.py index a7d74d5..a90b4f7 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,4 @@ import itertools -import math class Poly: @@ -144,6 +143,19 @@ 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 + def __mod__(self, other): assert self.int_mod == other.int_mod @@ -157,11 +169,11 @@ class Poly: while fdeg >= sdeg: coefficient = other.elements[f"x{sdeg}"] + if poly.is_empty(): break + for i in range(1, poly.int_mod): - # print("I") - # print(poly.elements) if coefficient * i % poly.int_mod == poly.elements[f"x{fdeg}"]: coefficient = i break @@ -183,14 +195,7 @@ class Poly: return poly - # def __eq__(self, other): - # equal = True - # for e in self.elements: - # if e not in other.elements: - # equal = False - # break - # - # return equal + # TODO @staticmethod def gcd(self, other): @@ -255,14 +260,18 @@ class PolyIntField: p = [x % int_mod for x in p] self.elements.append(Poly(int_mod, p)) - + # FIXME def invertibles(self): invertibles = [] for e in self.elements: if not e.is_empty(): - if Poly.gcd(e, self.poly_modulo) == 1: - invertibles.append(e) + 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 return invertibles @@ -304,7 +313,7 @@ class PolyIntField: def __str__(self): str_form = "[\n\t" - str_form += str([]) + ",\n\t" + str_form += str(self.invertibles()) + ",\n\t" str_form += str(self.zero_divisors()) + ",\n\t" str_form += str(self.nilpotents()) + ",\n\t" str_form += str(self.idempotents()) + "\n" @@ -313,9 +322,30 @@ class PolyIntField: if __name__ == "__main__": + # print(phi(36)) - pf = PolyIntField(2, [1, 1, 1]) - print(pf) + # a = Poly(5, [1]) + # b = Poly(5, [3, 5]) + # # print(a == ) + # p = Poly(2, [1, 1, 1]) + # c = Poly(2, [1, 1]) + # d = Poly(2, [1]) + # print(c * Poly(2, [0, 1]) % p) + # print(d) + # print((c * Poly(2, [0, 1]) % p) == d) + # pf = PolyIntField(2, [1, 1, 1]) + # print(pf.invertibles()) + # a = pf.invertibles() + # for i in a: + # print(i) + # a = Poly(5, [1, 1]) + # print(a.elements == {"x1": 1, "x0": 1}) + # [[1], [2], [0, 1], [0, 2], [0, 0, 1], [1, 0, 1], [2, 1, 1], [2, 2, 1], [0, 0, 2], [2, 0, 2], [1, 1, 2], [1, 2, 2]], # odwracalne + # [ ], + + # [[0, 0, 1], [0, 0, 2], [0, 1], [0, 1, 1], [0, 1, 2], [0, 2], [0, 2, 1], [0, 2, 2], [1], [1, 0, 1], [1, 0, 2], [1, 1], [1, 1, 1], [1, 1, 2], [1, 2], [1, 2, 1], [1, 2, 2], [2], [2, 0, 1], [2, 0, 2], [2, 1], [2, 1, 1], [2, 1, 2], [2, 2], [2, 2, 1], [2, 2, 2] + d = [[1], [2], [0, 1], [0, 2], [0, 0, 1], [1, 0, 1], [2, 1, 1], [2, 2, 1], [0, 0, 2], [2, 0, 2], [1, 1, 2], [1, 2, 2]] + print(len(d)) poly_field = PolyIntField(3, [1, 1, 2, 2]) print(poly_field) -- 2.20.1 From ce6babfeb99ac0a6b01eaeb62fca32745a3804f3 Mon Sep 17 00:00:00 2001 From: Varmen8 Date: Thu, 28 Jun 2018 18:17:47 +0200 Subject: [PATCH 12/14] Add user input --- main.py | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/main.py b/main.py index a90b4f7..da825a5 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,6 @@ import itertools +import ast +import sys class Poly: @@ -313,39 +315,23 @@ class PolyIntField: def __str__(self): str_form = "[\n\t" - str_form += str(self.invertibles()) + ",\n\t" - str_form += str(self.zero_divisors()) + ",\n\t" - str_form += str(self.nilpotents()) + ",\n\t" - str_form += str(self.idempotents()) + "\n" + str_form += str(self.invertibles()) + ", # odwracalne\n\t" + str_form += str(self.zero_divisors()) + ", # dzielniki zera\n\t" + str_form += str(self.nilpotents()) + ", # nilpotenty\n\t" + str_form += str(self.idempotents()) + " # idempotenty\n" str_form += "]\n" return str_form if __name__ == "__main__": - # print(phi(36)) - # a = Poly(5, [1]) - # b = Poly(5, [3, 5]) - # # print(a == ) - # p = Poly(2, [1, 1, 1]) - # c = Poly(2, [1, 1]) - # d = Poly(2, [1]) - # print(c * Poly(2, [0, 1]) % p) - # print(d) - # print((c * Poly(2, [0, 1]) % p) == d) - # pf = PolyIntField(2, [1, 1, 1]) - # print(pf.invertibles()) - # a = pf.invertibles() - # for i in a: - # print(i) + if len(sys.argv) < 3: + print("Niepoprawny input") + exit(1) - # a = Poly(5, [1, 1]) - # print(a.elements == {"x1": 1, "x0": 1}) - # [[1], [2], [0, 1], [0, 2], [0, 0, 1], [1, 0, 1], [2, 1, 1], [2, 2, 1], [0, 0, 2], [2, 0, 2], [1, 1, 2], [1, 2, 2]], # odwracalne - # [ ], + if sys.argv[1].find(",") != -1: + print(f"Proszę użyć spacji, nie przecinków: '{sys.argv[1]}'") + exit(1) - # [[0, 0, 1], [0, 0, 2], [0, 1], [0, 1, 1], [0, 1, 2], [0, 2], [0, 2, 1], [0, 2, 2], [1], [1, 0, 1], [1, 0, 2], [1, 1], [1, 1, 1], [1, 1, 2], [1, 2], [1, 2, 1], [1, 2, 2], [2], [2, 0, 1], [2, 0, 2], [2, 1], [2, 1, 1], [2, 1, 2], [2, 2], [2, 2, 1], [2, 2, 2] - d = [[1], [2], [0, 1], [0, 2], [0, 0, 1], [1, 0, 1], [2, 1, 1], [2, 2, 1], [0, 0, 2], [2, 0, 2], [1, 1, 2], [1, 2, 2]] - print(len(d)) - poly_field = PolyIntField(3, [1, 1, 2, 2]) - print(poly_field) + field = PolyIntField(int(sys.argv[1]), ast.literal_eval(sys.argv[2])) + print(field) -- 2.20.1 From cfb71dcc8a8fc6da13587852aeef2df0ce7b2a31 Mon Sep 17 00:00:00 2001 From: Varmen8 Date: Sun, 1 Jul 2018 16:37:20 +0200 Subject: [PATCH 13/14] Fix polynomial equality --- main.py | 119 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 47 deletions(-) diff --git a/main.py b/main.py index da825a5..cb3dbb6 100644 --- a/main.py +++ b/main.py @@ -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]], -- 2.20.1 From b5ac572aab40f82c668cd5c7c325ed1f7cb50060 Mon Sep 17 00:00:00 2001 From: Varmen8 Date: Sun, 1 Jul 2018 17:51:15 +0200 Subject: [PATCH 14/14] Fix modulo of coefficients --- main.py | 69 ++++++++++++++------------------------------------------- 1 file changed, 17 insertions(+), 52 deletions(-) diff --git a/main.py b/main.py index cb3dbb6..f3fcb9d 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,8 @@ class Poly: self.elements = {} self.int_mod = int_mod + elements = [x % self.int_mod for x in elements] + elements = list(reversed(elements)) z = 0 for i in elements: @@ -19,7 +21,7 @@ class Poly: i = len(elements) - 1 for e in elements: - self.elements[f"x{i}"] = e % self.int_mod + self.elements[f"x{i}"] = e i -= 1 def __str__(self): @@ -69,7 +71,7 @@ class Poly: for i in range(power - 1): poly *= poly - return poly + return Poly(poly.int_mod, list(reversed(list(poly.elements.values())))) def __add__(self, other): @@ -179,13 +181,13 @@ class Poly: mulpoly = divpoly * other poly -= mulpoly - # print(poly) + for i in poly.elements.keys(): if poly.elements[f"{i}"] != 0: fdeg = int(i[1:]) break - return poly + return Poly(poly.int_mod, list(reversed(list(poly.elements.values())))) # TODO @staticmethod @@ -238,8 +240,9 @@ class PolyIntField: def __init__(self, int_mod, poly_mod): self.int_modulo = int_mod self.poly_modulo = Poly(int_mod, poly_mod) - product = list(itertools.product([x for x in range(0, int_mod)], - repeat=len(poly_mod) - 1)) + product = list(itertools.product( + [x for x in range(0, self.int_modulo)], + repeat=len(self.poly_modulo.elements) - 1)) self.elements = [] for p in product: @@ -312,51 +315,13 @@ class PolyIntField: if __name__ == "__main__": - # 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 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) - # 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]], + field = PolyIntField(int(sys.argv[1]), ast.literal_eval(sys.argv[2])) + print(field) \ No newline at end of file -- 2.20.1