Zadanie 4 #34

Closed
s426284 wants to merge 17 commits from s426284/DALGLI0:Zadanie-4 into master
4 changed files with 171 additions and 40 deletions

View File

@ -1,21 +0,0 @@
## Zadanie
Napisać algorytm, który dla danego `n ∈ ` znajdzie wszystkie:
1. elementy odwracalne
2. dzielniki zera
3. elementy nilpotentne
4. elementy idempotentne
w pierścieniu `{/n, +, ⋅}`.
Termin: 31.05
### Przykłady:
> Input: `4`
> Output: `[[1,3], [0,2], [0,2], [0,1]]`
> Input: `6`
> Output: `[[1,5], [0,2,3,4], [0], [0,1,3,4]]`

View File

@ -1,19 +0,0 @@
## Zadanie
Napisać program, który dla danego pierścienia współczynników `R = /n, n ∈ ` oraz wielomianów `f,g ∈ R[x] ` zmiennej `x ` znajdzie:
1. iloczyn `f⋅g ∈ R[x]`
2. klasę reszty `f ∈ R[x]/(g)`
3. największy wspólny dzielnik `nwd(f,g)` korzystając z algorytmu Euklidesa.
**Uwaga**: wielomiany są podawane jako ciąg współczynników **od wyrazu wolnego, do współczynnika wiodącego**.
Termin: 07.06
### Przykłady:
> Input: `2, [1,1,1,0,1], [0,1,1]` (i.e. `f = 1 + x + x² + x⁴, g = x² + x`)
> Output: `[[0,1,0,0,1,1,1], [1,1], [1,1]]`
> Input: `6, [2,1,0,2,1,3], [1,0,0,5]`
> Output: `[[3,1,0,5,0,1,4,5,5], [5,2,1], DivisionError]`

83
Zadanie-4/poly.py Normal file
View File

@ -0,0 +1,83 @@
import sys
import ast
class Polynomial:
n = 0
def __init__(self, coeff_list):
self.degree = len(coeff_list) - 1
self.coefficients = [x % Polynomial.n for x in coeff_list]
def __pow__(self, n):
result = self
for _ in range(n):
result = Polynomial.multiply(result, result)
return result
@staticmethod
def add(p1, p2):
result = []
f = p1.coefficients
g = p2.coefficients
if len(f) >= len(g):
result = f
for i in range(len(g)):
result[i] = f[i] + g[i]
else:
result = g
for i in range(len(f)):
result[i] = f[i] + g[i]
result = [x % int(Polynomial.n) for x in result]
return Polynomial(result)
@staticmethod
def multiply(p1, p2):
result = [0] * (p1.degree + p2.degree + 1)
f = p1.coefficients
g = p2.coefficients
for i in range(len(f)):
for j in range(len(g)):
result[i+j] += f[i] * g[j]
result = [x % int(Polynomial.n) for x in result]
return Polynomial(result)
@staticmethod
def divide(p1, p2):
def inverse(x):
for i in range(1, int(Polynomial.n)):
r = (i * x) % int(Polynomial.n)
if r == 1:
break
else:
raise ZeroDivisionError
return i
if p1.degree < p2.degree:
return p1
f = p1.coefficients
g = p2.coefficients
g_lead_coef = g[-1]
g_deg = p2.degree
while len(f) >= len(g):
f_lead_coef = f[-1]
tmp_coef = f_lead_coef * inverse(g_lead_coef)
tmp_exp = len(f) - 1 - g_deg
tmp = []
for _ in range(tmp_exp):
tmp.append(0)
tmp.append(tmp_coef)
tmp_poly = Polynomial(tmp)
sub = Polynomial.multiply(p2, tmp_poly)
f = [x - y for x, y in zip(f, sub.coefficients)]
f = [x % int(Polynomial.n) for x in f]
while f and f[-1] == 0:
f.pop()
return Polynomial(f)
@staticmethod
def gcd(p1, p2):
if len(p2.coefficients) == 0:
return p1
return Polynomial.gcd(p2, Polynomial.divide(p1, p2))

View File

@ -0,0 +1,88 @@
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()