zad 4 #32
166
hw4.py
166
hw4.py
@ -1,84 +1,84 @@
|
|||||||
from poly import Polynomial
|
from poly import Polynomial
|
||||||
from sys import argv
|
from sys import argv
|
||||||
from ast import literal_eval
|
from ast import literal_eval
|
||||||
from fractions import gcd
|
from fractions import gcd
|
||||||
|
|
||||||
class QuotientRing():
|
class QuotientRing():
|
||||||
def __init__(self, f, m):
|
def __init__(self, f, m):
|
||||||
self.f = Polynomial(f, m)
|
self.f = Polynomial(f, m)
|
||||||
self.m = m
|
self.m = m
|
||||||
self.remainders = self.remainders()
|
self.remainders = self.remainders()
|
||||||
self.reversibles = self.reversibles()
|
self.reversibles = self.reversibles()
|
||||||
self.zero_divisors = self.zero_divisors()
|
self.zero_divisors = self.zero_divisors()
|
||||||
self.idempotent = self.idempotent()
|
self.idempotent = self.idempotent()
|
||||||
self.nilpotent = self.nilpotent()
|
self.nilpotent = self.nilpotent()
|
||||||
|
|
||||||
def remainders(self): #n - exponent
|
def remainders(self): #n - exponent
|
||||||
rems = [] #lista reszt
|
rems = [] #lista reszt
|
||||||
m = self.m
|
m = self.m
|
||||||
t = [0]
|
t = [0]
|
||||||
i = 0
|
i = 0
|
||||||
while len(t) < len(self.f.poly):
|
while len(t) < len(self.f.poly):
|
||||||
rems.append(Polynomial(t, m))
|
rems.append(Polynomial(t, m))
|
||||||
i = (i + 1) % m
|
i = (i + 1) % m
|
||||||
t[0] = i
|
t[0] = i
|
||||||
if i == 0:
|
if i == 0:
|
||||||
if len(t) == 1:
|
if len(t) == 1:
|
||||||
t.append(1)
|
t.append(1)
|
||||||
else:
|
else:
|
||||||
t[1] += 1
|
t[1] += 1
|
||||||
for j in range(1, len(t)):
|
for j in range(1, len(t)):
|
||||||
if t[j] == 0 or t[j] % m != 0:
|
if t[j] == 0 or t[j] % m != 0:
|
||||||
break
|
break
|
||||||
temp = t[j] % m
|
temp = t[j] % m
|
||||||
t[j] = 0
|
t[j] = 0
|
||||||
if temp == 0:
|
if temp == 0:
|
||||||
if (j + 1) < len(t):
|
if (j + 1) < len(t):
|
||||||
t[j+1] += 1
|
t[j+1] += 1
|
||||||
else:
|
else:
|
||||||
t.append(1)
|
t.append(1)
|
||||||
return rems
|
return rems
|
||||||
|
|
||||||
def reversibles(self):
|
def reversibles(self):
|
||||||
return [ rem for rem in self.remainders if len(rem.poly_gcd(self.f).poly) == 1 ]
|
return [ rem for rem in self.remainders if len(rem.poly_gcd(self.f).poly) == 1 ]
|
||||||
|
|
||||||
#dopelnienie elementow odwracalnych
|
#dopelnienie elementow odwracalnych
|
||||||
def zero_divisors(self):
|
def zero_divisors(self):
|
||||||
return [ rem for rem in self.remainders if rem not in self.reversibles ]
|
return [ rem for rem in self.remainders if rem not in self.reversibles ]
|
||||||
|
|
||||||
def idempotent(self):
|
def idempotent(self):
|
||||||
idems = []
|
idems = []
|
||||||
for rem in self.remainders:
|
for rem in self.remainders:
|
||||||
if (rem * rem / self.f) == (rem / self.f):
|
if (rem * rem % self.f) == (rem % self.f):
|
||||||
idems.append(rem)
|
idems.append(rem)
|
||||||
try:
|
try:
|
||||||
if idems[0].poly == []: #implementacja wielomianow ucina zera
|
if idems[0].poly == []: #implementacja wielomianow ucina zera
|
||||||
idems[0].poly = [0]
|
idems[0].poly = [0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return idems
|
return idems
|
||||||
return idems
|
return idems
|
||||||
|
|
||||||
def nilpotent(self):
|
def nilpotent(self):
|
||||||
nils = []
|
nils = []
|
||||||
phi = len([ i for i in range(1, self.m) if gcd(i, self.m) == 1 ])
|
phi = len([ i for i in range(1, self.m) if gcd(i, self.m) == 1 ])
|
||||||
for zero_div in self.zero_divisors:
|
for zero_div in self.zero_divisors:
|
||||||
for i in range(self.m):
|
for i in range(self.m):
|
||||||
if len((zero_div ** i / self.f).poly) == 0:
|
if len((zero_div ** i % self.f).poly) == 0:
|
||||||
nils.append(zero_div)
|
nils.append(zero_div)
|
||||||
break
|
break
|
||||||
return nils
|
return nils
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
m = int(argv[1])
|
m = int(argv[1])
|
||||||
f = literal_eval(argv[2])
|
f = literal_eval(argv[2])
|
||||||
qr = QuotientRing(f, m)
|
qr = QuotientRing(f, m)
|
||||||
out = [
|
out = [
|
||||||
[ rev.poly for rev in qr.reversibles ],
|
[ rev.poly for rev in qr.reversibles ],
|
||||||
[ zero_div.poly for zero_div in qr.zero_divisors ],
|
[ zero_div.poly for zero_div in qr.zero_divisors ],
|
||||||
[ nil.poly for nil in qr.nilpotent ],
|
[ nil.poly for nil in qr.nilpotent ],
|
||||||
[ idem.poly for idem in qr.idempotent ]
|
[ idem.poly for idem in qr.idempotent ]
|
||||||
]
|
]
|
||||||
print(*out, sep='\n')
|
print(*out, sep='\n')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
Loading…
Reference in New Issue
Block a user