diff --git a/src/sage/coding/abstract_code.py b/src/sage/coding/abstract_code.py index 85cdce652b9..ed84dc12517 100644 --- a/src/sage/coding/abstract_code.py +++ b/src/sage/coding/abstract_code.py @@ -512,7 +512,7 @@ def list(self): (1, 0, 1, 0, 1, 0, 1) True """ - return [x for x in self] + return list(self) def length(self): r""" diff --git a/src/sage/coding/ag_code.py b/src/sage/coding/ag_code.py index f705f75d476..0f189bb69a0 100644 --- a/src/sage/coding/ag_code.py +++ b/src/sage/coding/ag_code.py @@ -332,7 +332,7 @@ def designed_distance(self): """ Return the designed distance of the AG code. - If the code is of dimension zero, then a ``ValueError`` is raised. + If the code is of dimension zero, then a :class:`ValueError` is raised. EXAMPLES:: @@ -576,7 +576,7 @@ def designed_distance(self): """ Return the designed distance of the differential AG code. - If the code is of dimension zero, then a ``ValueError`` is raised. + If the code is of dimension zero, then a :class:`ValueError` is raised. EXAMPLES:: diff --git a/src/sage/coding/code_bounds.py b/src/sage/coding/code_bounds.py index c1e0eb77022..b192792f11f 100644 --- a/src/sage/coding/code_bounds.py +++ b/src/sage/coding/code_bounds.py @@ -192,10 +192,10 @@ def _check_n_q_d(n, q, d, field_based=True): Check that the length `n`, alphabet size `q` and minimum distance `d` type check and make sense for a code over a field. - More precisely, checks that the parameters are positive integers, that `q` - is a prime power for codes over a field, or, more generally, that - `q` is of size at least 2, and that `n >= d`. Raises a ``ValueError`` - otherwise. + More precisely, this checks that the parameters are positive + integers, that `q` is a prime power for codes over a field, or, + more generally, that `q` is of size at least 2, and that `n >= d`. + This raises a :class:`ValueError` otherwise. TESTS:: diff --git a/src/sage/coding/code_constructions.py b/src/sage/coding/code_constructions.py index d96ad3c7627..6870c28ec87 100644 --- a/src/sage/coding/code_constructions.py +++ b/src/sage/coding/code_constructions.py @@ -155,8 +155,8 @@ def _is_a_splitting(S1, S2, n, return_automorphism=False): This is a special case of Theorem 6.4.3 in [HP2003]_. """ R = IntegerModRing(n) - S1 = set(R(x) for x in S1) - S2 = set(R(x) for x in S2) + S1 = {R(x) for x in S1} + S2 = {R(x) for x in S2} # we first check whether (S1,S2) is a partition of R - {0} if (len(S1) + len(S2) != n-1 or len(S1) != len(S2) or diff --git a/src/sage/coding/databases.py b/src/sage/coding/databases.py index 3826734c09b..989d67e89d8 100644 --- a/src/sage/coding/databases.py +++ b/src/sage/coding/databases.py @@ -174,7 +174,7 @@ def best_linear_code_in_codetables_dot_de(n, k, F, verbose=False): i = s.find("
")
     j = s.find("
") if i == -1 or j == -1: - raise IOError("Error parsing data (missing pre tags).") + raise OSError("Error parsing data (missing pre tags).") return s[i+5:j].strip() diff --git a/src/sage/coding/delsarte_bounds.py b/src/sage/coding/delsarte_bounds.py index 47c84b2c59b..e25c0a4faf4 100644 --- a/src/sage/coding/delsarte_bounds.py +++ b/src/sage/coding/delsarte_bounds.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- r""" Delsarte (or linear programming) bounds @@ -24,9 +23,8 @@ # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. -# http://www.gnu.org/licenses/ +# https://www.gnu.org/licenses/ # **************************************************************************** -from __future__ import print_function, division def krawtchouk(n, q, l, x, check=True): @@ -142,7 +140,6 @@ def eberlein(n, w, k, u, check=True): default. Otherwise, pass it as it is. Use ``check=False`` at your own risk. - EXAMPLES:: sage: codes.bounds.eberlein(24,10,2,6) @@ -169,13 +166,12 @@ def eberlein(n, w, k, u, check=True): Traceback (most recent call last): ... TypeError: either m or x-m must be an integer - """ from sage.arith.misc import binomial from sage.arith.srange import srange - if 2*w > n: - return eberlein(n, n-w, k, u) + if 2 * w > n: + return eberlein(n, n - w, k, u) if check: from sage.rings.integer_ring import ZZ @@ -185,10 +181,10 @@ def eberlein(n, w, k, u, check=True): n = n0 return sum([(-1)**j*binomial(u, j)*binomial(w-u, k-j)*binomial(n-w-u, k-j) - for j in srange(0, k+1)]) + for j in srange(k + 1)]) -def _delsarte_LP_building(n, d, d_star, q, isinteger, solver, maxc=0): +def _delsarte_LP_building(n, d, d_star, q, isinteger, solver, maxc=0): r""" LP builder - common for the two functions; not exported. @@ -211,18 +207,18 @@ def _delsarte_LP_building(n, d, d_star, q, isinteger, solver, maxc=0): x_0 is a continuous variable (min=0, max=+oo) ... x_7 is a continuous variable (min=0, max=+oo) - """ from sage.numerical.mip import MixedIntegerLinearProgram p = MixedIntegerLinearProgram(maximization=True, solver=solver) A = p.new_variable(integer=isinteger, nonnegative=True) - p.set_objective(sum([A[r] for r in range(n+1)])) + p.set_objective(sum([A[r] for r in range(n + 1)])) p.add_constraint(A[0] == 1) for i in range(1, d): p.add_constraint(A[i] == 0) - for j in range(1, n+1): - rhs = sum([krawtchouk(n, q, j, r, check=False)*A[r] for r in range(n+1)]) + for j in range(1, n + 1): + rhs = sum([krawtchouk(n, q, j, r, check=False) * A[r] + for r in range(n + 1)]) p.add_constraint(0 <= rhs) if j >= d_star: p.add_constraint(0 <= rhs) @@ -230,7 +226,7 @@ def _delsarte_LP_building(n, d, d_star, q, isinteger, solver, maxc=0): p.add_constraint(0 == rhs) if maxc > 0: - p.add_constraint(sum([A[r] for r in range(n+1)]), max=maxc) + p.add_constraint(sum([A[r] for r in range(n + 1)]), max=maxc) return A, p @@ -272,10 +268,9 @@ def _delsarte_cwc_LP_building(n, d, w, solver, isinteger): Variables: x_0 is a continuous variable (min=0, max=+oo) x_1 is a continuous variable (min=0, max=+oo) - """ - from sage.numerical.mip import MixedIntegerLinearProgram from sage.arith.misc import binomial + from sage.numerical.mip import MixedIntegerLinearProgram p = MixedIntegerLinearProgram(maximization=True, solver=solver) A = p.new_variable(integer=isinteger, nonnegative=True) @@ -287,12 +282,14 @@ def _q(k, i): return mu_i*eberlein(n, w, i, k)/v_i for k in range(1, w+1): - p.add_constraint(sum([A[2*i]*_q(k, i) for i in range(d//2, w+1)]), min=-1) + p.add_constraint(sum([A[2*i]*_q(k, i) for i in range(d//2, w+1)]), + min=-1) return A, p -def delsarte_bound_constant_weight_code(n, d, w, return_data=False, solver="PPL", isinteger=False): +def delsarte_bound_constant_weight_code(n, d, w, return_data=False, + solver="PPL", isinteger=False): r""" Find the Delsarte bound on a constant weight code. @@ -337,15 +334,16 @@ def delsarte_bound_constant_weight_code(n, d, w, return_data=False, solver="PPL" sage: codes.bounds.delsarte_bound_constant_weight_code(17, 4, 3, isinteger=True) 43 - """ from sage.numerical.mip import MIPSolverException if d < 4: - raise ValueError("Violated constraint d>=4 for Binary Constant Weight Codes") + raise ValueError("Violated constraint d>=4 for " + "Binary Constant Weight Codes") if d >= 2*w or 2*w > n: - raise ValueError("Violated constraint d<2w<=n for Binary Constant Weight Codes") + raise ValueError("Violated constraint d<2w<=n for " + "Binary Constant Weight Codes") # minimum distance is even => if there is an odd lower bound on d we can # increase it by 1 @@ -356,23 +354,19 @@ def delsarte_bound_constant_weight_code(n, d, w, return_data=False, solver="PPL" try: bd = p.solve() except MIPSolverException as exc: - print("Solver exception: {}".format(exc)) - if return_data: - return A, p, False - return False + print(f"Solver exception: {exc}") + return (A, p, False) if return_data else False - if return_data: - return A, p, bd - else: - return int(bd) + return (A, p, bd) if return_data else int(bd) -def delsarte_bound_hamming_space(n, d, q, return_data=False, solver="PPL", isinteger=False): +def delsarte_bound_hamming_space(n, d, q, return_data=False, + solver="PPL", isinteger=False): r""" Find the Delsarte bound on codes in ``H_q^n`` of minimal distance ``d`` - Find the Delsarte bound [De1973]_ on the size of codes in the Hamming space ``H_q^n`` - of minimal distance ``d``. + Find the Delsarte bound [De1973]_ on the size of codes in + the Hamming space ``H_q^n`` of minimal distance ``d``. INPUT: @@ -444,94 +438,92 @@ def delsarte_bound_hamming_space(n, d, q, return_data=False, solver="PPL", isint try: bd = p.solve() except MIPSolverException as exc: - print("Solver exception: {}".format(exc)) - if return_data: - return A, p, False - return False + print(f"Solver exception: {exc}") + return (A, p, False) if return_data else False - if return_data: - return A, p, bd - else: - return bd + return (A, p, bd) if return_data else bd -def delsarte_bound_additive_hamming_space(n, d, q, d_star=1, q_base=0, return_data=False, solver="PPL", isinteger=False): +def delsarte_bound_additive_hamming_space(n, d, q, d_star=1, q_base=0, return_data=False, + solver="PPL", isinteger=False): r""" - Find a modified Delsarte bound on additive codes in Hamming space `H_q^n` of minimal distance `d` + Find a modified Delsarte bound on additive codes in Hamming space `H_q^n` of minimal distance `d`. - Find the Delsarte LP bound on ``F_{q_base}``-dimension of additive codes in - Hamming space `H_q^n` of minimal distance ``d`` with minimal distance of the dual - code at least ``d_star``. If ``q_base`` is set to - non-zero, then ``q`` is a power of ``q_base``, and the code is, formally, linear over - ``F_{q_base}``. Otherwise it is assumed that ``q_base==q``. + Find the Delsarte LP bound on ``F_{q_base}``-dimension of additive + codes in Hamming space `H_q^n` of minimal distance ``d`` with + minimal distance of the dual code at least ``d_star``. If + ``q_base`` is set to non-zero, then ``q`` is a power of + ``q_base``, and the code is, formally, linear over + ``F_{q_base}``. Otherwise it is assumed that ``q_base==q``. + INPUT: - INPUT: - - - ``n`` -- the code length + - ``n`` -- the code length - - ``d`` -- the (lower bound on) minimal distance of the code + - ``d`` -- the (lower bound on) minimal distance of the code - - ``q`` -- the size of the alphabet + - ``q`` -- the size of the alphabet - - ``d_star`` -- the (lower bound on) minimal distance of the dual code; - only makes sense for additive codes. + - ``d_star`` -- the (lower bound on) minimal distance of the dual code; + only makes sense for additive codes. - - ``q_base`` -- if ``0``, the code is assumed to be linear. Otherwise, - ``q=q_base^m`` and the code is linear over ``F_{q_base}``. + - ``q_base`` -- if ``0``, the code is assumed to be linear. Otherwise, + ``q=q_base^m`` and the code is linear over ``F_{q_base}``. - - ``return_data`` -- if ``True``, return a triple ``(W,LP,bound)``, where ``W`` is - a weights vector, and ``LP`` the Delsarte bound LP; both of them are Sage LP - data. ``W`` need not be a weight distribution of a code, or, - if ``isinteger==False``, even have integer entries. + - ``return_data`` -- if ``True``, return a triple ``(W,LP,bound)``, + where ``W`` is a weights vector, and ``LP`` the Delsarte bound + LP; both of them are Sage LP data. ``W`` need not be a weight + distribution of a code, or, if ``isinteger==False``, even have + integer entries. - - ``solver`` -- the LP/ILP solver to be used. Defaults to ``'PPL'``. It is arbitrary - precision, thus there will be no rounding errors. With other solvers - (see :class:`MixedIntegerLinearProgram` for the list), you are on your own! + - ``solver`` -- the LP/ILP solver to be used. Defaults to ``'PPL'``. It is + arbitrary precision, thus there will be no rounding errors. With + other solvers (see :class:`MixedIntegerLinearProgram` for the + list), you are on your own! - - ``isinteger`` -- if ``True``, uses an integer programming solver (ILP), rather - that an LP solver. Can be very slow if set to ``True``. + - ``isinteger`` -- if ``True``, uses an integer programming solver (ILP), + rather that an LP solver. Can be very slow if set to ``True``. - EXAMPLES: + EXAMPLES: - The bound on dimension of linear `\GF{2}`-codes of length 11 and minimal distance 6:: + The bound on dimension of linear `\GF{2}`-codes of length 11 and minimal distance 6:: - sage: codes.bounds.delsarte_bound_additive_hamming_space(11, 6, 2) - 3 - sage: a,p,val = codes.bounds.delsarte_bound_additive_hamming_space(\ - 11, 6, 2, return_data=True) - sage: [j for i,j in p.get_values(a).items()] - [1, 0, 0, 0, 0, 0, 5, 2, 0, 0, 0, 0] + sage: codes.bounds.delsarte_bound_additive_hamming_space(11, 6, 2) + 3 + sage: a,p,val = codes.bounds.delsarte_bound_additive_hamming_space(\ + 11, 6, 2, return_data=True) + sage: [j for i,j in p.get_values(a).items()] + [1, 0, 0, 0, 0, 0, 5, 2, 0, 0, 0, 0] - The bound on the dimension of linear `\GF{4}`-codes of length 11 and minimal distance 3:: + The bound on the dimension of linear `\GF{4}`-codes of length 11 and minimal distance 3:: - sage: codes.bounds.delsarte_bound_additive_hamming_space(11,3,4) - 8 + sage: codes.bounds.delsarte_bound_additive_hamming_space(11,3,4) + 8 - The bound on the `\GF{2}`-dimension of additive `\GF{4}`-codes of length 11 and minimal - distance 3:: + The bound on the `\GF{2}`-dimension of additive `\GF{4}`-codes of length 11 and minimal + distance 3:: - sage: codes.bounds.delsarte_bound_additive_hamming_space(11,3,4,q_base=2) - 16 + sage: codes.bounds.delsarte_bound_additive_hamming_space(11,3,4,q_base=2) + 16 - Such a ``d_star`` is not possible:: + Such a ``d_star`` is not possible:: - sage: codes.bounds.delsarte_bound_additive_hamming_space(11,3,4,d_star=9) - Solver exception: PPL : There is no feasible solution - False + sage: codes.bounds.delsarte_bound_additive_hamming_space(11,3,4,d_star=9) + Solver exception: PPL : There is no feasible solution + False - TESTS:: + TESTS:: - sage: a,p,x = codes.bounds.delsarte_bound_additive_hamming_space(\ - 19,15,7,return_data=True,isinteger=True) - sage: [j for i,j in p.get_values(a).items()] - [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 307, 0, 0, 1, 34] - sage: codes.bounds.delsarte_bound_additive_hamming_space(19,15,7,solver='glpk') - 3 - sage: codes.bounds.delsarte_bound_additive_hamming_space(\ - 19,15,7, isinteger=True, solver='glpk') - 3 - """ + sage: a,p,x = codes.bounds.delsarte_bound_additive_hamming_space(\ + 19,15,7,return_data=True,isinteger=True) + sage: [j for i,j in p.get_values(a).items()] + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 307, 0, 0, 1, 34] + sage: codes.bounds.delsarte_bound_additive_hamming_space(19,15,7,solver='glpk') + 3 + sage: codes.bounds.delsarte_bound_additive_hamming_space(\ + 19,15,7, isinteger=True, solver='glpk') + 3 + """ from sage.numerical.mip import MIPSolverException if q_base == 0: q_base = q @@ -550,18 +542,18 @@ def delsarte_bound_additive_hamming_space(n, d, q, d_star=1, q_base=0, return_da m = kk*n # this is to emulate repeat/until block bd = q**n+1 - while q_base**m < bd: # need to solve the LP repeatedly, as this is a new constraint! + while q_base**m < bd: + # need to solve the LP repeatedly, as this is a new constraint! # we might become infeasible. More precisely, after rounding down # to the closest value of q_base^m, the LP, with the constraint that # the objective function is at most q_base^m, - A, p = _delsarte_LP_building(n, d, d_star, q, isinteger, solver, q_base**m) + A, p = _delsarte_LP_building(n, d, d_star, q, isinteger, + solver, q_base**m) try: bd = p.solve() except MIPSolverException as exc: print("Solver exception:", exc) - if return_data: - return A, p, False - return False + return (A, p, False) if return_data else False # rounding the bound down to the nearest power of q_base, for q=q_base^m # bd_r = roundres(log(bd, base=q_base)) m = -1 @@ -570,10 +562,7 @@ def delsarte_bound_additive_hamming_space(n, d, q, d_star=1, q_base=0, return_da if q_base**(m+1) == bd: m += 1 - if return_data: - return A, p, m - else: - return m + return (A, p, m) if return_data else m def _delsarte_Q_LP_building(q, d, solver, isinteger): @@ -644,12 +633,13 @@ def _delsarte_Q_LP_building(q, d, solver, isinteger): p.add_constraint(A[i] == 0) for k in range(1, n): - p.add_constraint(sum([q[k][i]*A[i] for i in range(n)]), min=0) + p.add_constraint(sum([q[k][i] * A[i] for i in range(n)]), min=0) return A, p -def delsarte_bound_Q_matrix(q, d, return_data=False, solver="PPL", isinteger=False): +def delsarte_bound_Q_matrix(q, d, return_data=False, + solver="PPL", isinteger=False): r""" Delsarte bound on a code with Q matrix ``q`` and lower bound on min. dist. ``d``. @@ -701,25 +691,19 @@ def delsarte_bound_Q_matrix(q, d, return_data=False, solver="PPL", isinteger=Fal sage: a,p,val = codes.bounds.delsarte_bound_Q_matrix(q_matrix, 6, return_data=True) sage: [j for i,j in p.get_values(a).items()] [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1] - """ - - from sage.structure.element import is_Matrix from sage.numerical.mip import MIPSolverException + from sage.structure.element import is_Matrix if not is_Matrix(q): - raise ValueError("Input to delsarte_bound_Q_matrix should be a sage Matrix()") + raise ValueError("Input to delsarte_bound_Q_matrix " + "should be a sage Matrix()") A, p = _delsarte_Q_LP_building(q, d, solver, isinteger) try: bd = p.solve() except MIPSolverException as exc: - print("Solver exception: {}".format(exc)) - if return_data: - return A, p, False - return False + print(f"Solver exception: {exc}") + return (A, p, False) if return_data else False - if return_data: - return A, p, bd - else: - return bd + return (A, p, bd) if return_data else bd diff --git a/src/sage/coding/guruswami_sudan/interpolation.py b/src/sage/coding/guruswami_sudan/interpolation.py index eea537a115a..62ed9f3a8f1 100644 --- a/src/sage/coding/guruswami_sudan/interpolation.py +++ b/src/sage/coding/guruswami_sudan/interpolation.py @@ -130,7 +130,7 @@ def eqs_affine(x0, y0): eqs = [] for i in range(s): for j in range(s - i): - eq = dict() + eq = {} for monomial in monomials: ihat = monomial[0] jhat = monomial[1] diff --git a/src/sage/coding/linear_code.py b/src/sage/coding/linear_code.py index 0d84996a483..f8f051a00d9 100644 --- a/src/sage/coding/linear_code.py +++ b/src/sage/coding/linear_code.py @@ -1224,7 +1224,7 @@ def galois_closure(self, F0): n = len(G.columns()) k = len(G.rows()) G0 = [[x**q0 for x in g.list()] for g in G.rows()] - G1 = [[x for x in g.list()] for g in G.rows()] + G1 = [list(g.list()) for g in G.rows()] G2 = G0+G1 MS = MatrixSpace(F,2*k,n) G3 = MS(G2) @@ -1342,7 +1342,7 @@ def minimum_distance(self, algorithm=None): .. NOTE:: - When using GAP, this raises a ``NotImplementedError`` if + When using GAP, this raises a :class:`NotImplementedError` if the base field of the code has size greater than 256 due to limitations in GAP. @@ -1657,7 +1657,7 @@ def permutation_automorphism_group(self, algorithm="partition"): size = Gp.Size() print("\n Using the %s codewords of weight %s \n Supergroup size: \n %s\n " % (wts[wt], wt, size)) Cwt = filter(lambda c: c.WeightCodeword() == wt, eltsC) # bottleneck 2 (repeated - matCwt = list(map(lambda c: c.VectorCodeword(), Cwt)) # for each i until stop = 1) + matCwt = [c.VectorCodeword() for c in Cwt] # for each i until stop = 1) if len(matCwt) > 0: A = libgap(matCwt).MatrixAutomorphisms() Gp = A.Intersection2(Gp) # bottleneck 3 diff --git a/src/sage/coding/source_coding/huffman.py b/src/sage/coding/source_coding/huffman.py index 6c08a4d885a..1d41f84d77f 100644 --- a/src/sage/coding/source_coding/huffman.py +++ b/src/sage/coding/source_coding/huffman.py @@ -367,9 +367,9 @@ def pop(): # Build the binary tree of a Huffman code, where the root of the tree # is associated with the empty string. self._build_code_from_tree(self._tree, d, prefix="") - self._index = dict((i, s) for i, (s, w) in enumerate(symbols)) - self._character_to_code = dict( - (s, d[i]) for i, (s, w) in enumerate(symbols)) + self._index = {i: s for i, (s, w) in enumerate(symbols)} + self._character_to_code = { + s: d[i] for i, (s, w) in enumerate(symbols)} def encode(self, string): r"""