def checkPolyRemainder(a, n, r): val1 = sympy.rem( (x + a)**n, x**r - 1, modulus=n) val2 = sympy.rem( (x**n + a), x**r - 1, modulus=n) if (val1 != val2): return False return True
def __init__(self, equations, field): def genus(self): """ The arithmetic genus of H """ f = equations[0].subs(y**2, 0) if degree(f) == 3: return 1 else: return 2 self.genus = self.genus() def operation((u1, v1), (u2, v2)): """ Addition of two divisors using cantor's algorithm """ f = equations[0].subs(y**2, 0) g = self.genus e1, e2, d1 = sp.gcdex(u1, u2) c1, c2, d = sp.gcdex(d1, v1 + v2) s1, s2, s3 = c1 * e1, c1 * e2, c2 u = u1 * u2 / d**2 v = sp.rem((s1 * u1 * v2 + s2 * u2 * v1 + s3 * (v1 * v2 + f)) / d, u) while sp.degree(u) > g: u = (f - v**2) / u v = sp.rem(-v, u) if u.coeffs()[len(u.coeffs()) - 1] != 1: u = u / u.coeffs()[len(u.coeffs()) - 1] return (u, v)
def extend(self, y: sp.Symbol, n: int, ext_irr: sp.Poly): self.y = y self.ext_irr = ext_irr self.ext_elements = [sp.Poly(0, self.ext_irr.gens[::-1], modulus=self.p)] + \ [sp.Poly(self.y ** i, self.ext_irr.gens[::-1], modulus=self.p) for i in range(n)] for i in range(n, self.dim**n): el = sp.rem(sp.Poly(self.y**i, self.y), self.ext_irr, modulus=self.p, symmetric=False) el = sp.rem(sp.Poly(el, el.gens[::-1]), self.irr, modulus=self.p, symmetric=False) if el not in self.ext_elements: self.ext_elements.append(el) else: if el == 1 and len(self.elements) == self.dim: print( 'Last element is 1. Irreducible polynomial is really primitive.' ) else: raise ValueError( 'Repeated field element detected; please make sure your irreducible polynomial is primitive.' )
def _mult_ext(self, p1, p2): res = p1 * p2 res = sp.rem(sp.Poly(res, res.gens[::-1]), sp.Poly(self.ext_irr), modulus=self.p, symmetric=False) res = sp.rem(sp.Poly(res, res.gens[::-1]), sp.Poly(self.irr), modulus=self.p, symmetric=False) return res
def get_row_coefficients(self): """ Returns ======= row_coefficients: list The row coefficients of Macaulay's matrix """ row_coefficients = [] divisible = [] for i in range(self.n): if i == 0: degree = self.degree_m - self.degrees[i] monomial = self.get_monomials_of_certain_degree(degree) row_coefficients.append(monomial) else: divisible.append(self.variables[i - 1] ** self.degrees[i - 1]) degree = self.degree_m - self.degrees[i] poss_rows = self.get_monomials_of_certain_degree(degree) for div in divisible: for p in poss_rows: if rem(p, div) == 0: poss_rows = [item for item in poss_rows if item != p] row_coefficients.append(poss_rows) return row_coefficients
def __init__(self, x: sp.Symbol, p: int, n: int, irr: sp.Poly): # Extension parameters self.ext_elements = None self.ext_irr = None self.y = None self.p = p self.n = n self.irr = irr self.x = x self.dim = p**n # Initial elements self.elements = [sp.Poly(0, self.x, modulus=self.p)] + \ [sp.Poly(self.x ** i, self.x, modulus=self.p) for i in range(n)] for i in range(n, self.dim): el = sp.rem(sp.Poly(self.x**i, x), self.irr, modulus=p, symmetric=False) if el not in self.elements: self.elements.append(el) else: if el == 1 and len(self.elements) == self.dim: print( 'Last element is 1. Irreducible polynomial is really primitive.' ) else: raise ValueError( 'Repeated field element detected; please make sure your irreducible polynomial is primitive.' )
def get_row_coefficients(self): """ Returns ======= row_coefficients: list The row coefficients of Macaulay's matrix """ row_coefficients = [] divisible = [] for i in range(self.n): if i == 0: degree = self.degree_m - self.degrees[i] monomial = self.get_monomials_of_certain_degree(degree) row_coefficients.append(monomial) else: divisible.append(self.variables[i - 1]**self.degrees[i - 1]) degree = self.degree_m - self.degrees[i] poss_rows = self.get_monomials_of_certain_degree(degree) for div in divisible: for p in poss_rows: if rem(p, div) == 0: poss_rows = [ item for item in poss_rows if item != p ] row_coefficients.append(poss_rows) return row_coefficients
def minpoly (deg, p, fl): r_first = sp.rem (a**deg, fl, a, modulus=p) r_last = r_first squ = sp.poly (x - r_first, x, a, modulus=p) while True: deg *= p r = sp.rem (r_last**p, fl, a, modulus=p) r_last = r if r == r_first: break squ = sp.poly (squ * sp.poly (x - r, x, a, modulus=p), modulus=p) cf = sp.poly (squ, x).all_coeffs () cf2 = [sp.rem (e, fl, a, modulus=p) for e in cf] n = len (cf2) fl = sum ([int (cf2[i] % p)*a**(n-i-1) for i in range (n)]) return sp.poly (fl, a, modulus=p)
def construct_matrice(polynomial, p): q = [] n = len(polynomial) - 1 for deg in range(0, n): x = Symbol('x') r = rem(x**(p * deg), Poly.from_list(polynomial, gens=x)) r_rev = get_coeffs(r, p, n) r_rev.reverse() r_rev[deg] -= 1 r_rev = field(r_rev, p) q.append(r_rev) return q
def minimal(name1, name2, name3): file1 = open(name1, "r") read_ls = file1.readlines() file1.close() p = int(read_ls[0].split()[0]) n = int(read_ls[1].split()[0]) pol_ls = read_ls[2].split() fl = sum([int(pol_ls[i]) * a**i for i in range(n + 1)]) file2 = open(name2, "r") read_ls = file2.readlines() file2.close() deg = int(read_ls[0].split()[0]) r_first = sp.rem(a**deg, fl, a, modulus=p) r_last = r_first squ = sp.poly(x - r_first, x, a, modulus=p) while True: deg *= p r = sp.rem(r_last**p, fl, a, modulus=p) r_last = r if r == r_first: break squ = sp.poly(squ * sp.poly(x - r, x, a, modulus=p), modulus=p) res = sp.poly(squ, x).all_coeffs() file3 = open(name3, "w") file3.write(str(p) + '\n') file3.write(str(len(res) - 1) + '\n') for i in reversed(range(len(res))): file3.write(str(int(sp.rem(res[i], fl, a, modulus=p)) % p)) if i == 0: file3.write('\n') else: file3.write(' ') file3.close()
def is_primitive(poly): if not poly.is_irreducible: return False degree = int(sympy.degree(poly)) if sympy.isprime(2**degree - 1): return True # compare with generating the full sequence for k in (d for d in sympy.divisors(2**degree - 1) if degree < d < (2**degree - 1)): q = sympy.Poly(x**k + 1, x, modulus=2) if sympy.rem(q, poly, modulus=2).is_zero: return False return True
def main(): irr = x**3 + x + 1 p = 2 m = 3 elements = [0] for i in range(1, p**m): res = sp.rem(x**i, irr, modulus=p, symmetric=False) print(res) if res not in elements: elements.append(res) else: print('ALARM') print(len(elements))
def our_euclid_rem(f, g, x): """ Μέγιστος κοινός διαιρέτης πολυωνύμων f and g, deg(f) > deg(g) > 0 στους ρητούς (QQ). Τα υπόλοιπα κάθε διαίρεσης υπολογίζονται με την συνάρτηση rem(). Τα πρόσημα των υπολοίπων είναι σωστά. """ print('gcd(f, g) using rem()', '\n') print(f, '\n') print(g, '\n') our_es = [f, g] while degree(g, x) > 0: h = rem(f, g) our_es.append(h) f, g = g, h print(g, '\n') return our_es
def GPD_meth(poly, var, div, myorder): x, y = symbols('x y') zero = symbols('0') dlen = len(div) # num of dividents q = [] NotDivide = False divoccur = True for k in range(0, len(div)): # init list of quos q.append(zero) ############################################### for i in range(0, len(div)): #print("iteration",i) if poly == 0: break if NotDivide == True: NotDivide = False while NotDivide == False: LTP = LT(poly, var, order=myorder) # check if LTD divides LTP LTD = LT(div[i], var, order=myorder) #print("check3") if quo(LTP, LTD) != 0 and rem(LTP, LTD) == 0: #print("leading tems divisor/divident0:",LTP,LTD) divLT = simplify(LTP / LTD) #works thatsgood #print(divLT) ##########check changes in quo rem################ if q[i] == zero: # if quo is initialized q[i] = simplify(divLT) else: q[i] = simplify(q[i] + divLT) poly = simplify(poly - simplify(divLT * div[i])) #print("check0",poly) #print("new poly,new quo",poly,q[i]) ################################################## else: NotDivide = True break return poly, q # final results
def is_primitive(poly): """ Checks whether a binary polynomial is primitive over GF(2). Parameters ---------- poly : SymPy polynomial The polynomial must be using `x` as its generator and has `modulus` set to 2. Returns ------- b : bool True if `poly` is primitive over GF(2), False otherwise. Examples -------- >>> is_primitive(sympy.Poly(x**3 + x + 1, x, modulus=2)) True >>> is_primitive(sympy.Poly(x**4 + 1, x, modulus=2)) # reducible False >>> is_primitive(sympy.Poly(x**4 + x**3 + x**2 + x + 1, x, modulus=2)) # irreducible non-primitive False """ if not poly.is_irreducible: return False degree = int(sympy.degree(poly)) if sympy.isprime(2**degree - 1): return True for k in (d for d in sympy.divisors(2**degree - 1) if degree < d < (2**degree - 1)): q = sympy.Poly(x**k + 1, x, modulus=2) if sympy.rem(q, poly, modulus=2).is_zero: return False return True
def MultiplyPol(f, g, phi): r = sp.rem(f.mul(g), phi) return r
def decrypt(f, e, fp): a = f * e a = sym.rem(a, xN, modulus=q) m = fp * a m = sym.rem(m, xN, modulus=p) return m
def encrypt(m, h, r): e = r * h e = e + toPoly(m, len(m)) e = sym.rem(e, xN, symmetric=False, modulus=q) return e
def _mult(self, p1, p2): return sp.rem(p1 * p2, self.irr, modulus=self.p, symmetric=False)
def my_subresultants (p, q, x): """ So far we obtain the Sturm sequence, whether complete or incomplete. Next, we will obtain the subresultant prs """ subresL = [p, q] my_poly = p p = my_poly if(sp.LC(my_poly) < 0): p = -p q = -q degree_rem_poly = 0 poly1 = p # polynomial q is the first derivative of p # q = sp.simplify(sp.diff(p,x)) poly2 = q ui = 0; vi = 0; rho_neg = sp.LC(poly1) # is the LC of poly1 -> we'll use it in the rest of the cases... r0_i = rho_neg # this will always be multiplied in the denominator # of the formula mass_of_u = pi = p0 = 1 fd = sp.degree(poly1) while(1): rhoi = sp.LC(poly2) # is the rho0, rho1 of the formula rem_poly = -sp.rem(poly1,poly2,x) p_plusplus = sp.degree(poly2) - sp.degree(rem_poly) pold = p_plusplus degree_rem_poly = sp.degree(rem_poly) # so as to know where to stop rem_LC = sp.LC(rem_poly) # the LC of the new remnant poly1=poly2 # refresh poly1 and poly2 poly2=rem_poly # if we are in the first loop this is p1 # if we are in the second loop this is p2 fd = sp.LC(poly1) ui = sp.summation(i,(i,1,pi)); vi = vi + pi; mass_of_u = (-1)**ui * mass_of_u; sign = mass_of_u * (-1)**vi; pi = p_plusplus; if(p_plusplus>1): r0_i = r0_i * rhoi**(1+p0) # the result of the Pell_Gordon formula else: r0_i = r0_i * rhoi**(p_plusplus+p0) # multiply the denominator od the formula with # the r0_i so as to find the det of the current submatrix LC = (rem_poly * r0_i)/sp.LC(my_poly)/sign # print(LC) subresL.append(LC) # when the remnant is finally a number # LC and degree function throw an exception # so let's take a different case if(degree_rem_poly==1): rhoi = sp.LC(poly2) rem_poly = -sp.rem(poly1,poly2,x) p_plusplus = sp.degree(poly1)-1 rem_LC = rem_poly ui = sp.summation(i,(i,1,pi)) vi = vi + pi mass_of_u = (-1)**ui * mass_of_u sign = mass_of_u * (-1)**vi if(p_plusplus>1): if(rhoi<0): r0_i = -r0_i*fd**(pold-p0) *(rhoi**(pold+p0))/sign # the result of the Pell_Gordon formula else: r0_i = r0_i*fd**(pold-p0) *(rhoi**(pold+p0)) else: r0_i = r0_i * rhoi**(1+p0) LC = (rem_LC * r0_i)/sign #print(LC) subresL.append(LC) break #print("End of Current Computation") return subresL
def MultiplyPol(f, g, reduction): r = sp.rem(f.mul(g), reduction) return r