def QuadraticResidueCodeOddPair(n, F): """ Quadratic residue codes of a given odd prime length and base ring either don't exist at all or occur as 4-tuples - a pair of "odd-like" codes and a pair of "even-like" codes. If n 2 is prime then (Theorem 6.6.2 in [HP2003]_) a QR code exists over GF(q) iff q is a quadratic residue mod n. They are constructed as "odd-like" duadic codes associated the splitting (Q,N) mod n, where Q is the set of non-zero quadratic residues and N is the non-residues. EXAMPLES:: sage: codes.QuadraticResidueCodeOddPair(17, GF(13)) # known bug (#25896) ([17, 9] Cyclic Code over GF(13), [17, 9] Cyclic Code over GF(13)) sage: codes.QuadraticResidueCodeOddPair(17, GF(2)) ([17, 9] Cyclic Code over GF(2), [17, 9] Cyclic Code over GF(2)) sage: codes.QuadraticResidueCodeOddPair(13, GF(9,"z")) # known bug (#25896) ([13, 7] Cyclic Code over GF(9), [13, 7] Cyclic Code over GF(9)) sage: C1 = codes.QuadraticResidueCodeOddPair(17, GF(2))[1] sage: C1x = C1.extended_code() sage: C2 = codes.QuadraticResidueCodeOddPair(17, GF(2))[0] sage: C2x = C2.extended_code() sage: C2x.spectrum(); C1x.spectrum() [1, 0, 0, 0, 0, 0, 102, 0, 153, 0, 153, 0, 102, 0, 0, 0, 0, 0, 1] [1, 0, 0, 0, 0, 0, 102, 0, 153, 0, 153, 0, 102, 0, 0, 0, 0, 0, 1] sage: C3 = codes.QuadraticResidueCodeOddPair(7, GF(2))[0] sage: C3x = C3.extended_code() sage: C3x.spectrum() [1, 0, 0, 0, 14, 0, 0, 0, 1] This is consistent with Theorem 6.6.14 in [HP2003]_. TESTS:: sage: codes.QuadraticResidueCodeOddPair(9,GF(2)) Traceback (most recent call last): ... ValueError: the argument n must be an odd prime """ from sage.arith.srange import srange from sage.categories.finite_fields import FiniteFields if F not in FiniteFields(): raise ValueError("the argument F must be a finite field") q = F.order() n = Integer(n) if n <= 2 or not n.is_prime(): raise ValueError("the argument n must be an odd prime") Q = quadratic_residues(n) Q.remove(0) # non-zero quad residues N = [x for x in srange(1, n) if x not in Q] # non-zero quad non-residues if q not in Q: raise ValueError( "the order of the finite field must be a quadratic residue modulo n" ) return DuadicCodeOddPair(F, Q, N)
def _enumerator(self): """ Return the most suitable enumerator for points. OUTPUT: An iterable that yields the coordinates of all points as tuples. EXAMPLES:: sage: P123 = toric_varieties.P2_123(base_ring=GF(3)) sage: point_set = P123.point_set() sage: point_set._enumerator() <sage.schemes.toric.points.FiniteFieldPointEnumerator object at 0x...> """ ambient = super(SchemeHomset_points_subscheme_toric_field, self)._enumerator() ring = self.domain().base_ring() if ring in FiniteFields(): from sage.schemes.toric.points import FiniteFieldSubschemePointEnumerator Enumerator = FiniteFieldSubschemePointEnumerator else: from sage.schemes.toric.points import NaiveSubschemePointEnumerator Enumerator = NaiveSubschemePointEnumerator return Enumerator(self.codomain().defining_polynomials(), ambient)
def _finite_field_enumerator(self, finite_field=None): """ Fast enumerator for points of the toric variety. INPUT: - ``finite_field`` -- a finite field (optional; defaults to the base ring of the toric variety). The ring over which the points are considered. OUTPUT: A :class:`sage.schemes.toric.points.FiniteFieldPointEnumerator` instance that can be used to iterate over the points. EXAMPLES:: sage: P123 = toric_varieties.P2_123(base_ring=GF(3)) sage: point_set = P123.point_set() sage: next(iter(point_set._finite_field_enumerator())) (0, 0, 1) sage: next(iter(point_set)) [0 : 0 : 1] """ from sage.schemes.toric.points import FiniteFieldPointEnumerator variety = self.codomain() if finite_field is None: finite_field = variety.base_ring() if not finite_field in FiniteFields(): raise ValueError('not a finite field') return FiniteFieldPointEnumerator(variety.fan(), finite_field)
def _repr_field_name(self): """ Return an abbreviated field name as string RAISES: ``NotImplementedError``: The field does not have an abbreviated name defined. EXAMPLES:: sage: FilteredVectorSpace(2, base_ring=QQ)._repr_field_name() 'QQ' sage: F.<a> = GF(9) sage: FilteredVectorSpace(2, base_ring=F)._repr_field_name() 'GF(9)' sage: FilteredVectorSpace(2, base_ring=AA)._repr_field_name() Traceback (most recent call last): ... NotImplementedError """ if self.base_ring() == QQ: return 'QQ' elif self.base_ring() == RDF: return 'RDF' elif self.base_ring() == RR: return 'RR' from sage.categories.finite_fields import FiniteFields if self.base_ring() in FiniteFields(): return 'GF({0})'.format(len(self.base_ring())) else: raise NotImplementedError()
def intersection_points(self, C, F=None): r""" Return the points in the intersection of this curve and the curve ``C``. If the intersection of these two curves has dimension greater than zero, and if the base ring of this curve is not a finite field, then an error is returned. INPUT: - ``C`` -- a curve in the same ambient space as this curve. - ``F`` -- (default: None). Field over which to compute the intersection points. If not specified, the base ring of this curve is used. OUTPUT: - a list of points in the ambient space of this curve. EXAMPLES:: sage: R.<a> = QQ[] sage: K.<b> = NumberField(a^2 + a + 1) sage: P.<x,y,z,w> = ProjectiveSpace(QQ, 3) sage: C = Curve([y^2 - w*z, w^3 - y^3], P) sage: D = Curve([x*y - w*z, z^3 - y^3], P) sage: C.intersection_points(D, F=K) [(-b - 1 : -b - 1 : b : 1), (b : b : -b - 1 : 1), (1 : 0 : 0 : 0), (1 : 1 : 1 : 1)] :: sage: A.<x,y> = AffineSpace(GF(7), 2) sage: C = Curve([y^3 - x^3], A) sage: D = Curve([-x*y^3 + y^4 - 2*x^3 + 2*x^2*y], A) sage: C.intersection_points(D) [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 3), (5, 5), (5, 6), (6, 6)] :: sage: A.<x,y> = AffineSpace(QQ, 2) sage: C = Curve([y^3 - x^3], A) sage: D = Curve([-x*y^3 + y^4 - 2*x^3 + 2*x^2*y], A) sage: C.intersection_points(D) Traceback (most recent call last): ... NotImplementedError: the intersection must have dimension zero or (=Rational Field) must be a finite field """ if C.ambient_space() != self.ambient_space(): raise TypeError("(=%s) must be a curve in the same ambient space as (=%s)"%(C,self)) if not isinstance(C, Curve_generic): raise TypeError("(=%s) must be a curve"%C) X = self.intersection(C) if F is None: F = self.base_ring() if X.dimension() == 0 or F in FiniteFields(): return X.rational_points(F=F) else: raise NotImplementedError("the intersection must have dimension zero or (=%s) must be a finite field"%F)
def ReedMullerCode(base_field, order, num_of_var): r""" Returns a Reed-Muller code. A Reed-Muller Code of order `r` and number of variables `m` over a finite field `F` is the set: .. MATH:: \{ (f(\alpha_i)\mid \alpha_i \in F^m) \mid f \in F[x_1,x_2,\ldots,x_m], \deg f \leq r \} INPUT: - ``base_field`` -- The finite field `F` over which the code is built. - ``order`` -- The order of the Reed-Muller Code, which is the maximum degree of the polynomial to be used in the code. - ``num_of_var`` -- The number of variables used in polynomial. .. WARNING:: For now, this implementation only supports Reed-Muller codes whose order is less than q. Binary Reed-Muller codes must have their order less than or equal to their number of variables. EXAMPLES: We build a Reed-Muller code:: sage: F = GF(3) sage: C = codes.ReedMullerCode(F, 2, 2) sage: C Reed-Muller Code of order 2 and 2 variables over Finite Field of size 3 We ask for its parameters:: sage: C.length() 9 sage: C.dimension() 6 sage: C.minimum_distance() 3 If one provides a finite field of size 2, a Binary Reed-Muller code is built:: sage: F = GF(2) sage: C = codes.ReedMullerCode(F, 2, 2) sage: C Binary Reed-Muller Code of order 2 and number of variables 2 """ if base_field not in FiniteFields(): raise ValueError("The parameter `base_field` must be a finite field") q = base_field.cardinality() if q == 2: return BinaryReedMullerCode(order, num_of_var) else: return QAryReedMullerCode(base_field, order, num_of_var)
def _late_import(): """ Used to reset the class of PARI finite field elements in their initialization. EXAMPLES:: sage: from sage.rings.finite_rings.element_ext_pari import FiniteField_ext_pariElement sage: k.<a> = GF(3^17) sage: a.__class__ is FiniteField_ext_pariElement # indirect doctest False """ global dynamic_FiniteField_ext_pariElement dynamic_FiniteField_ext_pariElement = dynamic_class( "%s_with_category" % FiniteField_ext_pariElement.__name__, (FiniteField_ext_pariElement, FiniteFields().element_class), doccls=FiniteField_ext_pariElement)
def __init__(self, base_field, order, num_of_var): r""" TESTS: Note that the order given cannot be greater than (q-1). An error is raised if that happens:: sage: from sage.coding.reed_muller_code import QAryReedMullerCode sage: C = QAryReedMullerCode(GF(3), 4, 4) Traceback (most recent call last): ... ValueError: The order must be less than 3 The order and the number of variable must be integers:: sage: C = QAryReedMullerCode(GF(3),1.1,4) Traceback (most recent call last): ... ValueError: The order of the code must be an integer The base_field parameter must be a finite field:: sage: C = QAryReedMullerCode(QQ,1,4) Traceback (most recent call last): ... ValueError: the input `base_field` must be a FiniteField """ # input sanitization if base_field not in FiniteFields(): raise ValueError("the input `base_field` must be a FiniteField") if not (isinstance(order, (Integer, int))): raise ValueError("The order of the code must be an integer") if not (isinstance(num_of_var, (Integer, int))): raise ValueError("The number of variables must be an integer") q = base_field.cardinality() if (order >= q): raise ValueError("The order must be less than %s" % q) super(QAryReedMullerCode, self).__init__(base_field, q**num_of_var, "EvaluationVector", "Syndrome") self._order = order self._num_of_var = num_of_var self._dimension = binomial(num_of_var + order, order)
def _enumerator(self): """ Return the most suitable enumerator for points. OUTPUT: An iterable that yields the coordinates of all points as tuples. EXAMPLES:: sage: P123 = toric_varieties.P2_123(base_ring=GF(3)) sage: point_set = P123.point_set() sage: point_set._enumerator() <sage.schemes.toric.points.FiniteFieldPointEnumerator object at 0x...> """ ring = self.domain().base_ring() if ring in FiniteFields(): return self._finite_field_enumerator() elif ring.is_finite(): return self._naive_enumerator() else: from sage.schemes.toric.points import InfinitePointEnumerator return InfinitePointEnumerator(self.codomain().fan(), ring)
def QuadraticResidueCodeEvenPair(n, F): """ Quadratic residue codes of a given odd prime length and base ring either don't exist at all or occur as 4-tuples - a pair of "odd-like" codes and a pair of "even-like" codes. If `n > 2` is prime then (Theorem 6.6.2 in [HP2003]_) a QR code exists over `GF(q)` iff q is a quadratic residue mod `n`. They are constructed as "even-like" duadic codes associated the splitting (Q,N) mod n, where Q is the set of non-zero quadratic residues and N is the non-residues. EXAMPLES:: sage: codes.QuadraticResidueCodeEvenPair(17, GF(13)) # known bug (#25896) ([17, 8] Cyclic Code over GF(13), [17, 8] Cyclic Code over GF(13)) sage: codes.QuadraticResidueCodeEvenPair(17, GF(2)) ([17, 8] Cyclic Code over GF(2), [17, 8] Cyclic Code over GF(2)) sage: codes.QuadraticResidueCodeEvenPair(13,GF(9,"z")) # known bug (#25896) ([13, 6] Cyclic Code over GF(9), [13, 6] Cyclic Code over GF(9)) sage: C1,C2 = codes.QuadraticResidueCodeEvenPair(7,GF(2)) sage: C1.is_self_orthogonal() True sage: C2.is_self_orthogonal() True sage: C3 = codes.QuadraticResidueCodeOddPair(17,GF(2))[0] sage: C4 = codes.QuadraticResidueCodeEvenPair(17,GF(2))[1] sage: C3.systematic_generator_matrix() == C4.dual_code().systematic_generator_matrix() True This is consistent with Theorem 6.6.9 and Exercise 365 in [HP2003]_. TESTS:: sage: codes.QuadraticResidueCodeEvenPair(14,Zmod(4)) Traceback (most recent call last): ... ValueError: the argument F must be a finite field sage: codes.QuadraticResidueCodeEvenPair(14,GF(2)) Traceback (most recent call last): ... ValueError: the argument n must be an odd prime sage: codes.QuadraticResidueCodeEvenPair(5,GF(2)) Traceback (most recent call last): ... ValueError: the order of the finite field must be a quadratic residue modulo n """ from sage.arith.srange import srange from sage.categories.finite_fields import FiniteFields if F not in FiniteFields(): raise ValueError("the argument F must be a finite field") q = F.order() n = Integer(n) if n <= 2 or not n.is_prime(): raise ValueError("the argument n must be an odd prime") Q = quadratic_residues(n) Q.remove(0) # non-zero quad residues N = [x for x in srange(1, n) if x not in Q] # non-zero quad non-residues if q not in Q: raise ValueError( "the order of the finite field must be a quadratic residue modulo n" ) return DuadicCodeEvenPair(F, Q, N)
# Distributed under the terms of the GNU General Public License (GPL) # # This code is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # The full text of the GPL is available at: # # http://www.gnu.org/licenses/ #***************************************************************************** from sage.rings.finite_rings.finite_field_base import FiniteField as FiniteField_generic from sage.categories.finite_fields import FiniteFields _FiniteFields = FiniteFields() import sage.rings.finite_rings.integer_mod_ring as integer_mod_ring from sage.rings.integer import Integer import sage.rings.finite_rings.integer_mod as integer_mod from sage.rings.integer_ring import ZZ from sage.rings.finite_rings.integer_mod_ring import IntegerModRing_generic import sage.structure.factorization as factorization from sage.structure.parent import Parent class FiniteField_prime_modn(FiniteField_generic, integer_mod_ring.IntegerModRing_generic): r""" Finite field of order `p` where `p` is prime. EXAMPLES::