DALGLI0/Zadanie-4/quotient_ring.py

88 lines
2.8 KiB
Python

from poly import Polynomial as P
import sys
import ast
class QuotientRing:
def __init__(self, coeffs):
self.fx = P(coeffs)
self.remainder_set = self.create_remainder_set()
self.invertible_elements = self.get_invertible_elements()
self.zero_divisors = self.get_zero_divisors()
self.nilpotent_elements = self.get_nilpotent_elements()
self.idempotent_elements = self.get_idempotent_elements()
def create_remainder_set(self):
remainders = []
rem = [0]
i = 0
while len(rem) < len(self.fx.coefficients):
remainders.append(P(rem))
i = (i + 1) % P.n
rem[0] = i
if i == 0:
if len(rem) == 1:
rem.append(1)
else:
rem[1] += 1
for j in range(1, len(rem)):
if rem[j] == 0 or rem[j] % P.n != 0:
break
tmp = rem[j] % P.n
rem[j] = 0
if tmp == 0:
if (j + 1) < len(rem):
rem[j+1] += 1
else:
rem.append(1)
return remainders
def get_invertible_elements(self):
invertible_elements = []
for i in self.remainder_set:
if i.coefficients != [0] and len(P.gcd(self.fx, i).coefficients) == 1:
invertible_elements.append(i)
return invertible_elements
def get_zero_divisors(self):
zero_diviors = []
for i in self.remainder_set:
if i not in self.invertible_elements:
zero_diviors.append(i)
return zero_diviors
def get_nilpotent_elements(self):
nilpotent_elements = []
for i in self.zero_divisors:
for j in range(1, len(self.invertible_elements) + 1):
if i.coefficients == [0] or len(P.divide(i**j, self.fx).coefficients) == 0:
nilpotent_elements.append(i)
break
return nilpotent_elements
def get_idempotent_elements(self):
idempotent_elements = []
for i in self.remainder_set:
if P.divide(i**2, self.fx).coefficients == P.divide(i, self.fx).coefficients:
idempotent_elements.append(i)
return idempotent_elements
def main():
P.n = int(sys.argv[1])
coeffs = ast.literal_eval(sys.argv[2])
Q = QuotientRing(coeffs)
ans = [
[x.coefficients for x in Q.invertible_elements],
[x.coefficients for x in Q.zero_divisors],
[x.coefficients for x in Q.nilpotent_elements],
[x.coefficients for x in Q.idempotent_elements]
]
for i in range(len(ans)):
print(ans[i])
if __name__ == '__main__':
main()