def ExtendedBinaryGolayCode(): """ ExtendedBinaryGolayCode() returns the extended binary Golay code. This is a perfect [24,12,8] code. This code is self-dual. EXAMPLES:: sage: C = codes.ExtendedBinaryGolayCode() sage: C Linear code of length 24, dimension 12 over Finite Field of size 2 sage: C.minimum_distance() 8 sage: C.minimum_distance(algorithm='gap') # long time, check d=8 8 AUTHORS: - David Joyner (2007-05) """ B = [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1],\ [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0],\ [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1],\ [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0],\ [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1],\ [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1],\ [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1],\ [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0],\ [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0],\ [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0],\ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1],\ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1]] V = span(B, GF(2)) return LinearCodeFromVectorSpace(V, d=8)
def ExtendedTernaryGolayCode(): """ ExtendedTernaryGolayCode returns a ternary Golay code. This is a self-dual perfect [12,6,6] code. EXAMPLES:: sage: C = codes.ExtendedTernaryGolayCode() sage: C Linear code of length 12, dimension 6 over Finite Field of size 3 sage: C.minimum_distance() 6 sage: C.minimum_distance(algorithm='gap') # long time, check d=6 6 AUTHORS: - David Joyner (11-2005) """ B = [[1, 0, 0, 0, 0, 0, 2, 0, 1, 2, 1, 2],\ [0, 1, 0, 0, 0, 0, 1, 2, 2, 2, 1, 0],\ [0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1],\ [0, 0, 0, 1, 0, 0, 1, 1, 0, 2, 2, 2],\ [0, 0, 0, 0, 1, 0, 2, 1, 2, 2, 0, 1],\ [0, 0, 0, 0, 0, 1, 0, 2, 1, 2, 2, 1]] V = span(B, GF(3)) return LinearCodeFromVectorSpace(V, d=6)
def TernaryGolayCode(): r""" TernaryGolayCode returns a ternary Golay code. This is a perfect [11,6,5] code. It is also equivalent to a cyclic code, with generator polynomial `g(x)=2+x^2+2x^3+x^4+x^5`. EXAMPLES:: sage: C = codes.TernaryGolayCode() sage: C Linear code of length 11, dimension 6 over Finite Field of size 3 sage: C.minimum_distance() 5 sage: C.minimum_distance(algorithm='gap') # long time, check d=5 5 AUTHORS: - David Joyner (2007-5) """ F = GF(3) B = [[2, 0, 1, 2, 1, 1, 0, 0, 0, 0, 0],\ [0, 2, 0, 1, 2, 1, 1, 0, 0, 0, 0],\ [0, 0, 2, 0, 1, 2, 1, 1, 0, 0, 0],\ [0, 0, 0, 2, 0, 1, 2, 1, 1, 0, 0],\ [0, 0, 0, 0, 2, 0, 1, 2, 1, 1, 0],\ [0, 0, 0, 0, 0, 2, 0, 1, 2, 1, 1]] V = span(B, F) return LinearCodeFromVectorSpace(V, d=5)
def ExtendedBinaryGolayCode(): """ ExtendedBinaryGolayCode() returns the extended binary Golay code. This is a perfect [24,12,8] code. This code is self-dual. EXAMPLES:: sage: C = codes.ExtendedBinaryGolayCode() sage: C [24, 12] linear code over GF(2) sage: C.minimum_distance() 8 sage: C.minimum_distance(algorithm='gap') # long time, check d=8 8 AUTHORS: - David Joyner (2007-05) """ B = [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1],\ [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0],\ [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1],\ [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0],\ [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1],\ [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1],\ [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1],\ [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0],\ [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0],\ [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0],\ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1],\ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1]] V = span(B, GF(2)) return LinearCode(V, d=8)
def transmit_unsafe(self, message): r""" Returns ``message`` with as many errors as ``self._rank_error`` in it. If ``self._rank_error`` was passed as a tuple for the number of errors, it will pick a random integer between the bounds of the tuple and use it as the number of errors. This method does not check if ``message`` belongs to the input space of``self``. INPUT: - ``message`` -- a vector OUTPUT: - a vector of the output space EXAMPLES:: sage: F = GF(16)^6 sage: n_err = 2 sage: Chan = channels.StaticRankErrorChannel(F, n_err, GF(4)) sage: set_random_seed(10) sage: msg = F.random_element() sage: msg (z4 + 1, z4, z4^3 + z4 + 1, z4^3 + z4^2 + z4 + 1, z4^2, z4^2) sage: set_random_seed(10) sage: c = Chan.transmit_unsafe(msg) (z4^3 + z4, 1, z4^2 + 1, z4^3 + z4^2 + z4 + 1, 1, z4^3 + z4^2 + 1) TESTS:: sage: from sage.coding.linear_rank_metric import rank_distance sage: rank_distance(msg, c, GF(4)) 2 """ w = copy(message) rank_error = randint(*self.rank_error()) Fqm = self._base_field Fq = self._relative_field V = Fqm.vector_space(Fq, map=False) n = self.input_space().dimension() good = False w = None while not good: basis = [V.random_element() for i in range(rank_error)] R = span(basis) err = [R.random_element() for i in range(n)] M = matrix(Fq, err).transpose() if M.rank() == rank_error: good = True e = from_matrix_representation(M, Fqm) w = message + e return w
def rational_part(self): """ Return the rational part of this `L`-function at the central critical value 1. OUTPUT: a rational number EXAMPLES:: sage: from sage_modabvar import J0 sage: A, B = J0(43).decomposition() sage: A.lseries().rational_part() 0 sage: B.lseries().rational_part() 2/7 """ abelian_variety = self.abelian_variety() modular_symbols = abelian_variety.modular_symbols() Phi = modular_symbols.rational_period_mapping() ambient_module = modular_symbols.ambient_module() if self.vanishes_at_1(): return QQ(0) else: s = ambient_module.sturm_bound() I = ambient_module.hecke_images(0, range(1, s+1)) PhiTe = span([Phi(ambient_module(I[n])) for n in range(I.nrows())], ZZ) ambient_plus = ambient_module.sign_submodule(1) ambient_plus_cusp = ambient_plus.cuspidal_submodule() PhiH1plus = span([Phi(x) for x in ambient_plus_cusp.integral_basis()], ZZ) return PhiTe.index_in(PhiH1plus)
def rational_part(self): """ Return the rational part of this `L`-function at the central critical value 1. OUTPUT: a rational number EXAMPLES:: sage: A, B = J0(43).decomposition() sage: A.lseries().rational_part() 0 sage: B.lseries().rational_part() 2/7 """ abelian_variety = self.abelian_variety() modular_symbols = abelian_variety.modular_symbols() Phi = modular_symbols.rational_period_mapping() ambient_module = modular_symbols.ambient_module() if self.vanishes_at_1(): return QQ(0) else: s = ambient_module.sturm_bound() I = ambient_module.hecke_images(0, range(1, s + 1)) PhiTe = span([Phi(ambient_module(I[n])) for n in range(I.nrows())], ZZ) ambient_plus = ambient_module.sign_submodule(1) ambient_plus_cusp = ambient_plus.cuspidal_submodule() PhiH1plus = span([Phi(x) for x in ambient_plus_cusp.integral_basis()], ZZ) return PhiTe.index_in(PhiH1plus)
def BinaryGolayCode(): r""" BinaryGolayCode() returns a binary Golay code. This is a perfect [23,12,7] code. It is also (equivalent to) a cyclic code, with generator polynomial `g(x)=1+x^2+x^4+x^5+x^6+x^{10}+x^{11}`. Extending it yields the extended Golay code (see ExtendedBinaryGolayCode). EXAMPLE:: sage: C = codes.BinaryGolayCode() sage: C Linear code of length 23, dimension 12 over Finite Field of size 2 sage: C.minimum_distance() 7 sage: C.minimum_distance(algorithm='gap') # long time, check d=7 7 AUTHORS: - David Joyner (2007-05) """ F = GF(2) B = [ [1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1], ] # MS = MatrixSpace(F,12,23) # V = VectorSpace(F,23) V = span(B, F) return LinearCode(V, d=7)
def BinaryGolayCode(): r""" BinaryGolayCode() returns a binary Golay code. This is a perfect [23,12,7] code. It is also (equivalent to) a cyclic code, with generator polynomial `g(x)=1+x^2+x^4+x^5+x^6+x^{10}+x^{11}`. Extending it yields the extended Golay code (see ExtendedBinaryGolayCode). EXAMPLE:: sage: C = codes.BinaryGolayCode() sage: C Linear code of length 23, dimension 12 over Finite Field of size 2 sage: C.minimum_distance() 7 sage: C.minimum_distance(algorithm='gap') # long time, check d=7 7 AUTHORS: - David Joyner (2007-05) """ F = GF(2) B = [[1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\ [0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\ [0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],\ [0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\ [0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],\ [0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],\ [0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0],\ [0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0],\ [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0],\ [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0],\ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0],\ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1]] # MS = MatrixSpace(F,12,23) # V = VectorSpace(F,23) V = span(B, F) return LinearCodeFromVectorSpace(V, d=7)
def RandomLinearCode(n,k,F): r""" The method used is to first construct a `k \times n` matrix using Sage's random_element method for the MatrixSpace class. The construction is probabilistic but should only fail extremely rarely. INPUT: Integers n,k, with `n>k`, and a finite field F OUTPUT: Returns a "random" linear code with length n, dimension k over field F. EXAMPLES:: sage: C = codes.RandomLinearCode(30,15,GF(2)) sage: C Linear code of length 30, dimension 15 over Finite Field of size 2 sage: C = codes.RandomLinearCode(10,5,GF(4,'a')) sage: C Linear code of length 10, dimension 5 over Finite Field in a of size 2^2 AUTHORS: - David Joyner (2007-05) """ MS = MatrixSpace(F,k,n) for i in range(50): G = MS.random_element() if G.rank() == k: V = span(G.rows(), F) return LinearCodeFromVectorSpace(V) # may not be in standard form MS1 = MatrixSpace(F,k,k) MS2 = MatrixSpace(F,k,n-k) Ik = MS1.identity_matrix() A = MS2.random_element() G = Ik.augment(A) return LinearCode(G) # in standard form
def is_convex(self): """ sage: from cutgeneratingfunctionology.spam.polyhedral_complex import PolyhedralComplex sage: from sage.geometry.polyhedron.constructor import Polyhedron sage: pc = PolyhedralComplex([Polyhedron(base_ring=QQ, vertices=[[1,0],[0,1]], ....: rays=[[1,0],[0,1]])]) sage: pc.is_convex() True sage: pc = PolyhedralComplex([Polyhedron(base_ring=QQ, vertices=[[1,0,0],[0,1,0]], ....: rays=[[1,0,0],[0,1,0]])]) sage: pc.is_convex() True sage: pc = PolyhedralComplex([Polyhedron(base_ring=QQ, vertices=[[-1,0],[1,0]], ....: lines=[[0,1]])]) sage: pc.is_convex() True sage: pc = PolyhedralComplex([Polyhedron(base_ring=QQ, vertices=[[1,0],[0,1]], ....: rays=[[1,0],[0,1]]),Polyhedron(base_ring=QQ, vertices=[[1,0],[0,-1]], ....: rays=[[1,0],[0,-1]])]) sage: pc.is_convex() False sage: pc = PolyhedralComplex([Polyhedron(base_ring=QQ, vertices=[[0,0]], ....: rays=[[1,0],[-1,1]]),Polyhedron(base_ring=QQ, vertices=[[0,0]], ....: rays=[[1,0],[-1,-1]])]) sage: pc.is_convex() False sage: pc = PolyhedralComplex([Polyhedron(base_ring=QQ, vertices=[[0,0,0]], ....: rays=[[1,0,0],[-1,1,0]]),Polyhedron(base_ring=QQ, vertices=[[0,0,0]], ....: rays=[[1,0,0],[-1,-1,0]])]) sage: pc.is_convex() False sage: pc = PolyhedralComplex([Polyhedron(base_ring=QQ, vertices=[[0,0,0]],rays=[[1,0,0],[0,1,0],[0,0,-1]]), ....: Polyhedron(base_ring=QQ, vertices=[[0,0,0]],rays=[[1,0,0],[0,-1,0],[0,0,-1]]), ....: Polyhedron(base_ring=QQ, vertices=[[0,0,0]],rays=[[1,0,0],[0,-1,0],[0,0,1]]), ....: Polyhedron(base_ring=QQ, vertices=[[0,0,0]],rays=[[-1,0,0],[0,-1,0],[0,0,-1]]), ....: Polyhedron(base_ring=QQ, vertices=[[0,0,0]],rays=[[-1,0,0],[0,-1,0],[0,0,1]]), ....: Polyhedron(base_ring=QQ, vertices=[[0,0,0]],rays=[[-1,0,0],[0,1,0],[0,0,-1]]), ....: Polyhedron(base_ring=QQ, vertices=[[0,0,0]],rays=[[-1,0,0],[0,1,0],[0,0,1]])]) sage: pc.is_convex() False """ if hasattr(self, '_is_convex' ): # FIXME: bad! _is_convex can not be changed later. return self._is_convex if not self.is_pure(): self._is_convex = False return False if not self.is_full_dimensional(): from sage.modules.free_module import span face = self._maximal_cells[self._dim][0] affine_space = span(face.equations_list(), face.base_ring()) for face in self._maximal_cells[self._dim][1::]: if span(face.equations_list(), face.base_ring()) != affine_space: self._is_convex = False return False # # If they lie in different subspaces, can't be convex. When they all lie in the same subspace, then you orient the boundary halfspaces toward a strict convex combination of the vertices. Then you check whether all vertices are contained. After you made sure that the affine hulls of the cells are the same, it does not matter that is not full dimensional. boundaries = self.boundary_cells() vertices = set([]) rays = set([]) lines = set([]) # lines are useless, because they are in the affine space of each boundary cell. for cell in boundaries: # is that enough, or need vertices of all cells? I think that is enough. for v in cell.vertices_list(): vv = vector(v) vv.set_immutable() vertices.add(vv) for cell in self._maximal_cells[self._dim]: for r in cell.rays_list(): rr = vector(r) rr.set_immutable() rays.add(rr) for l in cell.lines_list(): ll = vector(l) ll.set_immutable() lines.add(ll) center = sum(vertices) / len(vertices) for cell in boundaries: for equation in cell.equations_list( ): # if not full-dim, cell has more than one equaiton. coeff = vector(equation[1::]) const = equation[0] if const + coeff * center == 0: sign = 0 elif const + coeff * center > 0: sign = 1 for v in vertices: if const + coeff * v < 0: self._is_convex = False return False elif const + coeff * center < 0: sign = -1 for v in vertices: if const + coeff * v > 0: self._is_convex = False return False for r in rays: if sign == 0: sign = coeff * r else: if sign * (coeff * r) < 0: self._is_convex = False return False self._is_convex = True self._polyhedron = Polyhedron(vertices=vertices, rays=rays, lines=lines) return True