diff --git a/src/sage/matrix/matrix_integer_dense_hnf.py b/src/sage/matrix/matrix_integer_dense_hnf.py index e77ffd60845..64fec462d42 100644 --- a/src/sage/matrix/matrix_integer_dense_hnf.py +++ b/src/sage/matrix/matrix_integer_dense_hnf.py @@ -733,12 +733,10 @@ def ones(H, pivots): # that contain exactly one "1" entry and all other entries 0. onecol = [] onerow = [] - i = 0 - for c in pivots: + for i, c in enumerate(pivots): if H[i, c] == 1: onecol.append(c) onerow.append(i) - i += 1 onecol_set = set(onecol) non_onerow = [j for j in range(len(pivots)) if j not in onerow] non_onecol = [j for j in range(H.ncols()) if j not in onecol_set][:len(non_onerow)] diff --git a/src/sage/matroids/constructor.py b/src/sage/matroids/constructor.py index f1c0af7bea7..65a1efb1294 100644 --- a/src/sage/matroids/constructor.py +++ b/src/sage/matroids/constructor.py @@ -928,11 +928,9 @@ def Matroid(groundset=None, data=None, **kwds): V = G.vertices(sort=True) n = G.num_verts() A = matrix(ZZ, n, m, 0) - mm = 0 - for i, j, k in G.edge_iterator(): + for mm, (i, j, k) in enumerate(G.edge_iterator()): A[V.index(i), mm] = -1 A[V.index(j), mm] += 1 # So loops get 0 - mm += 1 M = RegularMatroid(matrix=A, groundset=groundset) want_regular = False # Save some time, since result is already regular else: diff --git a/src/sage/matroids/utilities.py b/src/sage/matroids/utilities.py index 50c2af49d0b..f0762496191 100644 --- a/src/sage/matroids/utilities.py +++ b/src/sage/matroids/utilities.py @@ -272,16 +272,8 @@ def make_regular_matroid_from_matroid(matroid): # First create a reduced 0-1 matrix B = list(M.basis()) NB = list(M.groundset().difference(B)) - dB = {} - i = 0 - for e in B: - dB[e] = i - i += 1 - dNB = {} - i = 0 - for e in NB: - dNB[e] = i - i += 1 + dB = {e: i for i, e in enumerate(B)} + dNB = {e: i for i, e in enumerate(NB)} A = Matrix(ZZ, len(B), len(NB), 0) G = BipartiteGraph(A.transpose()) # Sage's BipartiteGraph uses the column set as first color class. This is an edgeless graph. for e in NB: diff --git a/src/sage/misc/latex.py b/src/sage/misc/latex.py index 6451f82ad22..6ce2f88f679 100644 --- a/src/sage/misc/latex.py +++ b/src/sage/misc/latex.py @@ -2164,45 +2164,36 @@ def repr_lincomb(symbols, coeffs): sage: latex(x) \text{\texttt{x}} + 2\text{\texttt{y}} """ - s = "" - first = True - i = 0 - from sage.rings.cc import CC + terms = [] + for c, sym in zip(coeffs, symbols): + if c == 0: + continue + if c == 1: + coeff = "" + elif c == -1: + coeff = "-" + else: + coeff = coeff_repr(c) - for c in coeffs: - bv = symbols[i] - b = latex(bv) - if c != 0: - if c == 1: - if first: - s += b - else: - s += " + %s" % b + b = latex(sym) + # this is a hack: I want to say that if the symbol happens to + # be a number, then we should put a multiplication sign in + try: + if sym in CC and coeff not in ("", "-"): + term = f"{coeff}\\cdot {b}" else: - coeff = coeff_repr(c) - if coeff == "-1": - coeff = "-" - if first: - coeff = str(coeff) - else: - coeff = " + %s" % coeff - # this is a hack: i want to say that if the symbol - # happens to be a number, then we should put a - # multiplication sign in - try: - if bv in CC: - s += r"%s\cdot %s" % (coeff, b) - else: - s += "%s%s" % (coeff, b) - except Exception: - s += "%s%s" % (coeff, b) - first = False - i += 1 - if first: - s = "0" - s = s.replace("+ -", "- ") - return s + term = f"{coeff}{b}" + except Exception: + term = f"{coeff}{b}" + + terms.append(term) + + if not terms: + return "0" + + s = " + ".join(terms) + return s.replace("+ -", "- ") common_varnames = ['alpha', diff --git a/src/sage/monoids/automatic_semigroup.py b/src/sage/monoids/automatic_semigroup.py index fac295b2341..38987f7145b 100644 --- a/src/sage/monoids/automatic_semigroup.py +++ b/src/sage/monoids/automatic_semigroup.py @@ -686,13 +686,11 @@ def _iter_concurrent(self): sage: M._constructed True """ - i = 0 # self._elements is never empty; so we are sure - for x in self._elements: + for i, x in enumerate(self._elements, 1): yield x # some other iterator/ method of the semigroup may have # been called before we move on to the next line - i += 1 if i == len(self._elements) and not self._constructed: try: next(self._iter) diff --git a/src/sage/rings/function_field/drinfeld_modules/charzero_drinfeld_module.py b/src/sage/rings/function_field/drinfeld_modules/charzero_drinfeld_module.py index ac3c1367cac..1c562e82419 100644 --- a/src/sage/rings/function_field/drinfeld_modules/charzero_drinfeld_module.py +++ b/src/sage/rings/function_field/drinfeld_modules/charzero_drinfeld_module.py @@ -626,10 +626,10 @@ def class_polynomial(self): # the Fq-vector space generated by the phi_T^i(T^(-s+1)) # for i varying in NN. v = vector(Fq, s) - v[s-1] = 1 + v[s - 1] = 1 vs = [v] - for i in range(s-1): - v = v*M + for i in range(s - 1): + v = v * M vs.append(v) V = matrix(vs) V.echelonize() @@ -638,12 +638,11 @@ def class_polynomial(self): # as an Fq-linear map (encoded in the matrix N) dim = V.rank() pivots = V.pivots() - j = ip = 0 - for i in range(dim, s): + j = 0 + for ip, i in enumerate(range(dim, s)): while ip < dim and j == pivots[ip]: j += 1 - ip += 1 - V[i,j] = 1 + V[i, j] = 1 N = (V * M * ~V).submatrix(dim, dim) # The class module is now H where the action of T diff --git a/src/sage/rings/number_field/number_field.py b/src/sage/rings/number_field/number_field.py index 844bfc3d557..13da0830d0e 100644 --- a/src/sage/rings/number_field/number_field.py +++ b/src/sage/rings/number_field/number_field.py @@ -11924,18 +11924,17 @@ def _multiplicative_order_table(self): try: return self.__multiplicative_order_table except AttributeError: - t = {} - x = self(1) - n = self.zeta_order() - m = 0 - zeta = self.zeta(n) - # todo: this desperately needs to be optimized!!! - for i in range(n): - t[x.polynomial()] = n // gcd(m, n) # multiplicative_order of (zeta_n)**m - x *= zeta - m += 1 - self.__multiplicative_order_table = t - return t + pass + t = {} + x = self.one() + n = self.zeta_order() + zeta = self.zeta(n) + # todo: this desperately needs to be optimized!!! + for m in range(n): + t[x.polynomial()] = n // gcd(m, n) # multiplicative_order of (zeta_n)**m + x *= zeta + self.__multiplicative_order_table = t + return t def zeta(self, n=None, all=False): """ diff --git a/src/sage/rings/polynomial/polynomial_quotient_ring.py b/src/sage/rings/polynomial/polynomial_quotient_ring.py index 37c6aed8d37..8761e789ba9 100644 --- a/src/sage/rings/polynomial/polynomial_quotient_ring.py +++ b/src/sage/rings/polynomial/polynomial_quotient_ring.py @@ -1351,13 +1351,10 @@ def _S_decomposition(self, S): fields = [] isos = [] iso_classes = [] - i = 0 - for f, _ in F: - D = K.extension(f, 'x'+str(i)) + for i, (f, _) in enumerate(F): + D = K.extension(f, f'x{i}') fields.append(D) - D_abs = D.absolute_field('y'+str(i)) - i += 1 - + D_abs = D.absolute_field(f'y{i}') seen_before = False j = 0 for D_iso, _ in iso_classes: diff --git a/src/sage/schemes/elliptic_curves/ell_egros.py b/src/sage/schemes/elliptic_curves/ell_egros.py index 365070a3809..df09cf2bf50 100644 --- a/src/sage/schemes/elliptic_curves/ell_egros.py +++ b/src/sage/schemes/elliptic_curves/ell_egros.py @@ -396,7 +396,6 @@ def egros_get_j(S=[], proof=None, verbose=False): SS = [-1] + S jlist = [] - wcount = 0 nw = 6**len(S) * 2 if verbose: @@ -404,9 +403,8 @@ def egros_get_j(S=[], proof=None, verbose=False): print("Using ", nw, " twists of base curve") sys.stdout.flush() - for ei in xmrange([6] * len(S) + [2]): + for wcount, ei in enumerate(xmrange([6] * len(S) + [2]), 1): w = QQ.prod(p**e for p, e in zip(reversed(SS), ei)) - wcount += 1 if verbose: print("Curve #", wcount, "/", nw, ":") print("w = ", w, "=", w.factor()) diff --git a/src/sage/schemes/elliptic_curves/ell_rational_field.py b/src/sage/schemes/elliptic_curves/ell_rational_field.py index 3e2387105ba..6cae5d79f37 100644 --- a/src/sage/schemes/elliptic_curves/ell_rational_field.py +++ b/src/sage/schemes/elliptic_curves/ell_rational_field.py @@ -6925,13 +6925,12 @@ def S_integral_x_coords_with_abs_bounded_by(abs_bound): mw_base_p_log = [] beta = [] mp = [] - tmp = 0 - for p in S: + for tmp, p in enumerate(S): Np = E.Np(p) cp = E.tamagawa_exponent(p) mp_temp = Z(len_tors).lcm(cp*Np) mp.append(mp_temp) # only necessary because of verbose below - p_prec = 30+E.discriminant().valuation(p) + p_prec = 30 + E.discriminant().valuation(p) p_prec_ok = False while not p_prec_ok: if verbose: @@ -6957,7 +6956,6 @@ def S_integral_x_coords_with_abs_bounded_by(abs_bound): except ValueError: # e.g. mw_base_p_log[tmp]==[0]: can occur e.g. [?]'172c6, S=[2] beta.append([0] for j in range(r)) - tmp += 1 if verbose: print('mw_base', mw_base) diff --git a/src/sage/schemes/elliptic_curves/gal_reps_number_field.py b/src/sage/schemes/elliptic_curves/gal_reps_number_field.py index 609aab5f4bf..62f82cc900f 100644 --- a/src/sage/schemes/elliptic_curves/gal_reps_number_field.py +++ b/src/sage/schemes/elliptic_curves/gal_reps_number_field.py @@ -571,21 +571,15 @@ def primes_iter(): for P in K.primes_above(p): if E.has_good_reduction(P): yield P - numP = 0 - for P in primes_iter(): + + for numP, P in enumerate(primes_iter()): if not L or numP == patience: # stop if no primes are left, or patience is exhausted break - numP += 1 - # Discard any l for which the Frobenius polynomial at P is # irreducible modulo l - disc = E.reduction(P).frobenius_polynomial().discriminant() - - L = [l for l in L if legendre_symbol(disc,l) != -1] - - #print("After using {} primes P, {} primes l remain".format(numP,len(L))) + L = [l for l in L if legendre_symbol(disc, l) != -1] if include_2: L = [2] + L diff --git a/src/sage/tensor/modules/comp.py b/src/sage/tensor/modules/comp.py index 98f9ff82d38..2850069cb96 100644 --- a/src/sage/tensor/modules/comp.py +++ b/src/sage/tensor/modules/comp.py @@ -762,7 +762,7 @@ def __getitem__(self, args): return self._output_formatter(self._ring.zero()) else: return self._output_formatter(self._ring.zero(), - format_type) + format_type) def _get_list(self, ind_slice, no_format=True, format_type=None): r""" @@ -2345,23 +2345,21 @@ def compprod(a, b): # definition of the parallel function @parallel(p_iter='multiprocessing', ncpus=nproc) def make_Contraction(this, other, local_list, rev_s, rev_o, - shift_o,contractions, comp_for_contr): + shift_o, contractions, comp_for_contr): local_res = [] for ind in local_list: ind_s = [None for i in range(this._nid)] # initialization - ind_o = [None for i in range(other._nid)] # initialization + ind_o = [None for i in range(other._nid)] # initialization for i, pos in enumerate(rev_s): ind_s[pos] = ind[i] for i, pos in enumerate(rev_o): - ind_o[pos] = ind[shift_o+i] + ind_o[pos] = ind[shift_o + i] sm = 0 for ind_c in comp_for_contr.index_generator(): - ic = 0 - for pos_s, pos_o in contractions: + for ic, (pos_s, pos_o) in enumerate(contractions): k = ind_c[ic] ind_s[pos_s] = k ind_o[pos_o] = k - ic += 1 sm += this[[ind_s]] * other[[ind_o]] local_res.append([ind, sm]) return local_res @@ -2373,19 +2371,17 @@ def make_Contraction(this, other, local_list, rev_s, rev_o, # sequential computation for ind in res.non_redundant_index_generator(): ind_s = [None for i in range(self._nid)] # initialization - ind_o = [None for i in range(other._nid)] # initialization + ind_o = [None for i in range(other._nid)] # initialization for i, pos in enumerate(rev_s): ind_s[pos] = ind[i] for i, pos in enumerate(rev_o): - ind_o[pos] = ind[shift_o+i] + ind_o[pos] = ind[shift_o + i] sm = 0 for ind_c in comp_for_contr.index_generator(): - ic = 0 - for pos_s, pos_o in contractions: + for ic, (pos_s, pos_o) in enumerate(contractions): k = ind_c[ic] ind_s[pos_s] = k ind_o[pos_o] = k - ic += 1 sm += self[[ind_s]] * other[[ind_o]] res[[ind]] = sm @@ -2775,7 +2771,7 @@ def _matrix_(self): return matrix(tab) -#****************************************************************************** +# ***************************************************************************** class CompWithSym(Components): r""" @@ -4018,15 +4014,16 @@ def non_redundant_index_generator(self): # to reset it ind[isym[k]] = si if not step_finished and i == 0 and len(antisym) == 0: - return # we went through all indices and didn't - # find one which we can increase, thus we - # have generated all indices - for i in range(len(antisym)-1,-1,-1): + # we went through all indices and didn't find one + # which we can increase, thus we have generated + # all indices + return + for i in range(len(antisym)-1, -1, -1): # the antisymmetrized indices work similar to the # symmetrized ones isym = antisym[i] if not step_finished: - for k in range(len(isym)-1,-1,-1): + for k in range(len(isym)-1, -1, -1): if ind[isym[k]] + len(isym)-1-k != imax: ind[isym[k]] += 1 for l in range(k+1, len(isym)): @@ -4609,7 +4606,7 @@ def antisymmetrize(self, *pos): return result -#****************************************************************************** +# ***************************************************************************** class CompFullySym(CompWithSym): r""" @@ -5054,7 +5051,7 @@ def paral_sum(a, b, local_list_ind): return CompWithSym.__add__(self, other) -#****************************************************************************** +# ***************************************************************************** class CompFullyAntiSym(CompWithSym): r""" @@ -5491,7 +5488,7 @@ def interior_product(self, other): res[[ind]] = factorial_s*sm return res -#****************************************************************************** +# ***************************************************************************** class KroneckerDelta(CompFullySym):