Skip to content

Commit 40fcffc

Browse files
author
Release Manager
committed
gh-40916: more usage of enumerate() as suggested by `ruff check --select=SIM113` ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: #40916 Reported by: Frédéric Chapoton Reviewer(s): Frédéric Chapoton, Martin Rubey
2 parents 3520ee2 + 6d4d7bf commit 40fcffc

File tree

12 files changed

+76
-117
lines changed

12 files changed

+76
-117
lines changed

src/sage/matrix/matrix_integer_dense_hnf.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,12 +733,10 @@ def ones(H, pivots):
733733
# that contain exactly one "1" entry and all other entries 0.
734734
onecol = []
735735
onerow = []
736-
i = 0
737-
for c in pivots:
736+
for i, c in enumerate(pivots):
738737
if H[i, c] == 1:
739738
onecol.append(c)
740739
onerow.append(i)
741-
i += 1
742740
onecol_set = set(onecol)
743741
non_onerow = [j for j in range(len(pivots)) if j not in onerow]
744742
non_onecol = [j for j in range(H.ncols()) if j not in onecol_set][:len(non_onerow)]

src/sage/matroids/constructor.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -928,11 +928,9 @@ def Matroid(groundset=None, data=None, **kwds):
928928
V = G.vertices(sort=True)
929929
n = G.num_verts()
930930
A = matrix(ZZ, n, m, 0)
931-
mm = 0
932-
for i, j, k in G.edge_iterator():
931+
for mm, (i, j, k) in enumerate(G.edge_iterator()):
933932
A[V.index(i), mm] = -1
934933
A[V.index(j), mm] += 1 # So loops get 0
935-
mm += 1
936934
M = RegularMatroid(matrix=A, groundset=groundset)
937935
want_regular = False # Save some time, since result is already regular
938936
else:

src/sage/matroids/utilities.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,16 +272,8 @@ def make_regular_matroid_from_matroid(matroid):
272272
# First create a reduced 0-1 matrix
273273
B = list(M.basis())
274274
NB = list(M.groundset().difference(B))
275-
dB = {}
276-
i = 0
277-
for e in B:
278-
dB[e] = i
279-
i += 1
280-
dNB = {}
281-
i = 0
282-
for e in NB:
283-
dNB[e] = i
284-
i += 1
275+
dB = {e: i for i, e in enumerate(B)}
276+
dNB = {e: i for i, e in enumerate(NB)}
285277
A = Matrix(ZZ, len(B), len(NB), 0)
286278
G = BipartiteGraph(A.transpose()) # Sage's BipartiteGraph uses the column set as first color class. This is an edgeless graph.
287279
for e in NB:

src/sage/misc/latex.py

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,45 +2164,36 @@ def repr_lincomb(symbols, coeffs):
21642164
sage: latex(x)
21652165
\text{\texttt{x}} + 2\text{\texttt{y}}
21662166
"""
2167-
s = ""
2168-
first = True
2169-
i = 0
2170-
21712167
from sage.rings.cc import CC
2168+
terms = []
2169+
for c, sym in zip(coeffs, symbols):
2170+
if c == 0:
2171+
continue
2172+
if c == 1:
2173+
coeff = ""
2174+
elif c == -1:
2175+
coeff = "-"
2176+
else:
2177+
coeff = coeff_repr(c)
21722178

2173-
for c in coeffs:
2174-
bv = symbols[i]
2175-
b = latex(bv)
2176-
if c != 0:
2177-
if c == 1:
2178-
if first:
2179-
s += b
2180-
else:
2181-
s += " + %s" % b
2179+
b = latex(sym)
2180+
# this is a hack: I want to say that if the symbol happens to
2181+
# be a number, then we should put a multiplication sign in
2182+
try:
2183+
if sym in CC and coeff not in ("", "-"):
2184+
term = f"{coeff}\\cdot {b}"
21822185
else:
2183-
coeff = coeff_repr(c)
2184-
if coeff == "-1":
2185-
coeff = "-"
2186-
if first:
2187-
coeff = str(coeff)
2188-
else:
2189-
coeff = " + %s" % coeff
2190-
# this is a hack: i want to say that if the symbol
2191-
# happens to be a number, then we should put a
2192-
# multiplication sign in
2193-
try:
2194-
if bv in CC:
2195-
s += r"%s\cdot %s" % (coeff, b)
2196-
else:
2197-
s += "%s%s" % (coeff, b)
2198-
except Exception:
2199-
s += "%s%s" % (coeff, b)
2200-
first = False
2201-
i += 1
2202-
if first:
2203-
s = "0"
2204-
s = s.replace("+ -", "- ")
2205-
return s
2186+
term = f"{coeff}{b}"
2187+
except Exception:
2188+
term = f"{coeff}{b}"
2189+
2190+
terms.append(term)
2191+
2192+
if not terms:
2193+
return "0"
2194+
2195+
s = " + ".join(terms)
2196+
return s.replace("+ -", "- ")
22062197

22072198

22082199
common_varnames = ['alpha',

src/sage/monoids/automatic_semigroup.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,13 +686,11 @@ def _iter_concurrent(self):
686686
sage: M._constructed
687687
True
688688
"""
689-
i = 0
690689
# self._elements is never empty; so we are sure
691-
for x in self._elements:
690+
for i, x in enumerate(self._elements, 1):
692691
yield x
693692
# some other iterator/ method of the semigroup may have
694693
# been called before we move on to the next line
695-
i += 1
696694
if i == len(self._elements) and not self._constructed:
697695
try:
698696
next(self._iter)

src/sage/rings/function_field/drinfeld_modules/charzero_drinfeld_module.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,10 @@ def class_polynomial(self):
626626
# the Fq-vector space generated by the phi_T^i(T^(-s+1))
627627
# for i varying in NN.
628628
v = vector(Fq, s)
629-
v[s-1] = 1
629+
v[s - 1] = 1
630630
vs = [v]
631-
for i in range(s-1):
632-
v = v*M
631+
for i in range(s - 1):
632+
v = v * M
633633
vs.append(v)
634634
V = matrix(vs)
635635
V.echelonize()
@@ -638,12 +638,11 @@ def class_polynomial(self):
638638
# as an Fq-linear map (encoded in the matrix N)
639639
dim = V.rank()
640640
pivots = V.pivots()
641-
j = ip = 0
642-
for i in range(dim, s):
641+
j = 0
642+
for ip, i in enumerate(range(dim, s)):
643643
while ip < dim and j == pivots[ip]:
644644
j += 1
645-
ip += 1
646-
V[i,j] = 1
645+
V[i, j] = 1
647646
N = (V * M * ~V).submatrix(dim, dim)
648647

649648
# The class module is now H where the action of T

src/sage/rings/number_field/number_field.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11924,18 +11924,17 @@ def _multiplicative_order_table(self):
1192411924
try:
1192511925
return self.__multiplicative_order_table
1192611926
except AttributeError:
11927-
t = {}
11928-
x = self(1)
11929-
n = self.zeta_order()
11930-
m = 0
11931-
zeta = self.zeta(n)
11932-
# todo: this desperately needs to be optimized!!!
11933-
for i in range(n):
11934-
t[x.polynomial()] = n // gcd(m, n) # multiplicative_order of (zeta_n)**m
11935-
x *= zeta
11936-
m += 1
11937-
self.__multiplicative_order_table = t
11938-
return t
11927+
pass
11928+
t = {}
11929+
x = self.one()
11930+
n = self.zeta_order()
11931+
zeta = self.zeta(n)
11932+
# todo: this desperately needs to be optimized!!!
11933+
for m in range(n):
11934+
t[x.polynomial()] = n // gcd(m, n) # multiplicative_order of (zeta_n)**m
11935+
x *= zeta
11936+
self.__multiplicative_order_table = t
11937+
return t
1193911938

1194011939
def zeta(self, n=None, all=False):
1194111940
"""

src/sage/rings/polynomial/polynomial_quotient_ring.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,13 +1351,10 @@ def _S_decomposition(self, S):
13511351
fields = []
13521352
isos = []
13531353
iso_classes = []
1354-
i = 0
1355-
for f, _ in F:
1356-
D = K.extension(f, 'x'+str(i))
1354+
for i, (f, _) in enumerate(F):
1355+
D = K.extension(f, f'x{i}')
13571356
fields.append(D)
1358-
D_abs = D.absolute_field('y'+str(i))
1359-
i += 1
1360-
1357+
D_abs = D.absolute_field(f'y{i}')
13611358
seen_before = False
13621359
j = 0
13631360
for D_iso, _ in iso_classes:

src/sage/schemes/elliptic_curves/ell_egros.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,17 +396,15 @@ def egros_get_j(S=[], proof=None, verbose=False):
396396
SS = [-1] + S
397397

398398
jlist = []
399-
wcount = 0
400399
nw = 6**len(S) * 2
401400

402401
if verbose:
403402
print("Finding possible j invariants for S = ", S)
404403
print("Using ", nw, " twists of base curve")
405404
sys.stdout.flush()
406405

407-
for ei in xmrange([6] * len(S) + [2]):
406+
for wcount, ei in enumerate(xmrange([6] * len(S) + [2]), 1):
408407
w = QQ.prod(p**e for p, e in zip(reversed(SS), ei))
409-
wcount += 1
410408
if verbose:
411409
print("Curve #", wcount, "/", nw, ":")
412410
print("w = ", w, "=", w.factor())

src/sage/schemes/elliptic_curves/ell_rational_field.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6925,13 +6925,12 @@ def S_integral_x_coords_with_abs_bounded_by(abs_bound):
69256925
mw_base_p_log = []
69266926
beta = []
69276927
mp = []
6928-
tmp = 0
6929-
for p in S:
6928+
for tmp, p in enumerate(S):
69306929
Np = E.Np(p)
69316930
cp = E.tamagawa_exponent(p)
69326931
mp_temp = Z(len_tors).lcm(cp*Np)
69336932
mp.append(mp_temp) # only necessary because of verbose below
6934-
p_prec = 30+E.discriminant().valuation(p)
6933+
p_prec = 30 + E.discriminant().valuation(p)
69356934
p_prec_ok = False
69366935
while not p_prec_ok:
69376936
if verbose:
@@ -6957,7 +6956,6 @@ def S_integral_x_coords_with_abs_bounded_by(abs_bound):
69576956
except ValueError:
69586957
# e.g. mw_base_p_log[tmp]==[0]: can occur e.g. [?]'172c6, S=[2]
69596958
beta.append([0] for j in range(r))
6960-
tmp += 1
69616959

69626960
if verbose:
69636961
print('mw_base', mw_base)

0 commit comments

Comments
 (0)