Skip to content

Commit 5eb9eb7

Browse files
author
Release Manager
committed
gh-40439: simplify some nested for loops either using walrus or simpler syntax ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: #40439 Reported by: Frédéric Chapoton Reviewer(s): Martin Rubey
2 parents 68068ed + 63e441b commit 5eb9eb7

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

src/sage/algebras/quatalg/quaternion_algebra.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -955,9 +955,10 @@ def maximal_order(self, take_shortcuts=True, order_basis=None):
955955
Order of Quaternion Algebra (-22, 210) with base ring Rational Field
956956
with basis (1, i, 1/2*i + 1/2*j, 1/2 + 17/22*i + 1/44*k)
957957
958-
sage: for d in ( m for m in range(1, 750) if is_squarefree(m) ): # long time (3s)
959-
....: A = QuaternionAlgebra(d)
960-
....: assert A.maximal_order(take_shortcuts=False).is_maximal()
958+
sage: Q = QuaternionAlgebra
959+
sage: all(Q(d).maximal_order(take_shortcuts=False).is_maximal()
960+
....: for d in range(1, 350) if is_squarefree(d))
961+
True
961962
962963
Specifying an order basis gives an extension of orders::
963964

src/sage/categories/finite_semigroups.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def j_classes(self):
9595
return self.cayley_graph(side='twosided', simple=True).strongly_connected_components()
9696

9797
@cached_method
98-
def j_classes_of_idempotents(self):
98+
def j_classes_of_idempotents(self) -> list[list]:
9999
r"""
100100
Return all the idempotents of self, grouped by J-class.
101101
@@ -108,7 +108,9 @@ def j_classes_of_idempotents(self):
108108
[['a'], ['ab', 'ba'], ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'],
109109
['ac', 'ca'], ['b'], ['bc', 'cb'], ['c']]
110110
"""
111-
return [l for l in ([x for x in cl if attrcall('is_idempotent')(x)] for cl in self.j_classes()) if len(l) > 0]
111+
it = ([x for x in cl if attrcall('is_idempotent')(x)]
112+
for cl in self.j_classes())
113+
return [ell for ell in it if ell]
112114

113115
@cached_method
114116
def j_transversal_of_idempotents(self):

src/sage/combinat/partition.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,7 +2043,7 @@ def cell_poset(self, orientation='SE'):
20432043
covers[(i, j)] = [(i, j - 1)]
20442044
return Poset(covers)
20452045

2046-
def frobenius_coordinates(self):
2046+
def frobenius_coordinates(self) -> tuple[list, list]:
20472047
"""
20482048
Return a pair of sequences of Frobenius coordinates aka beta numbers
20492049
of the partition.
@@ -2065,11 +2065,11 @@ def frobenius_coordinates(self):
20652065
mu = self
20662066
muconj = mu.conjugate() # Naive implementation
20672067
if len(mu) <= len(muconj):
2068-
a = [x for x in (val-i-1 for i, val in enumerate(mu)) if x >= 0]
2069-
b = [x for x in (muconj[i]-i-1 for i in range(len(a))) if x >= 0]
2068+
a = [x for i, val in enumerate(mu) if (x := val - i - 1) >= 0]
2069+
b = [x for i in range(len(a)) if (x := muconj[i] - i - 1) >= 0]
20702070
else:
2071-
b = [x for x in (val-i-1 for i, val in enumerate(muconj)) if x >= 0]
2072-
a = [x for x in (mu[i]-i-1 for i in range(len(b))) if x >= 0]
2071+
b = [x for i, val in enumerate(muconj) if (x := val - i - 1) >= 0]
2072+
a = [x for i in range(len(b)) if (x := mu[i] - i - 1) >= 0]
20732073
return (a, b)
20742074

20752075
def frobenius_rank(self):

src/sage/graphs/generators/smallgraphs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5473,8 +5473,9 @@ def JankoKharaghaniTonchevGraph():
54735473
301, 304, 308, 309, 310, 312, 313, 314, 316, 317, 318)
54745474
Gamma = Graph(multiedges=False, name='Janko-Kharaghani-Tonchev')
54755475
for i, b in ((1, B1), (163, B163)):
5476-
for j in (x[0] for x in st.OrbitsDomain(b)):
5477-
Gamma.add_edges(map(tuple, G.Orbit(libgap.Set([i, j]), libgap.OnSets)))
5476+
for x in st.OrbitsDomain(b):
5477+
Gamma.add_edges(map(tuple, G.Orbit(libgap.Set([i, x[0]]),
5478+
libgap.OnSets)))
54785479
Gamma.relabel(range(Gamma.order()))
54795480
return Gamma
54805481

src/sage/graphs/orientations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,7 @@ def bounded_outdegree_orientation(G, bound, solver=None, verbose=False,
13611361
d.add_edges(('s', vertices_id[v], b[v]) for v in vertices)
13621362

13631363
d.add_edges(((vertices_id[u], vertices_id[v]), 't', 1)
1364-
for u, v in G.edges(sort=False, labels=None))
1364+
for u, v in G.edges(sort=False, labels=None))
13651365

13661366
# each v is linked to its incident edges
13671367

@@ -1381,7 +1381,7 @@ def bounded_outdegree_orientation(G, bound, solver=None, verbose=False,
13811381
# The flow graph may not contain all the vertices, if they are
13821382
# not part of the flow...
13831383
edges = ((vertices[u], vertices[vv if vv != u else uu])
1384-
for u in (x for x in range(n) if x in flow)
1384+
for u in range(n) if u in flow
13851385
for uu, vv in flow.neighbors_out(u))
13861386

13871387
return _initialize_digraph(G, edges)

0 commit comments

Comments
 (0)