Skip to content

Commit 12dad0e

Browse files
author
Release Manager
committed
sagemathgh-38635: fixing most ruff PERF4 warnings in combinat
this is fixing most suggestions of `ruff check --select=PERF4` in the combinat folder. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. URL: sagemath#38635 Reported by: Frédéric Chapoton Reviewer(s): Matthias Köppe
2 parents cd38d5f + 94f80bb commit 12dad0e

25 files changed

+106
-171
lines changed

build/pkgs/configure/checksums.ini

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
tarball=configure-VERSION.tar.gz
2-
sha1=0f6355fc136bb6619585863b9e4bc954cc6e0e3d
3-
sha256=5b618581d51997afa78b5e6647584f7ef4e6d5844823dd44e607f2accd7abba5
2+
sha1=d41ea7340f0c4ffd00996dfb8bbe03f5dcff011b
3+
sha256=226027f38525957fdc8b95631dade33f01948387caef543e291493dd5a9f8d93
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
b9cb7bc2559cde857d84d77f0b37a3616ce1eb6c
1+
b16e0690ee0a679a3dc8c5e3917196079521a78c

src/sage/combinat/composition_tableau.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,7 @@ def descent_set(self):
211211
sage: CompositionTableau([[1],[3,2],[4,4]]).descent_set()
212212
[1, 3]
213213
"""
214-
cols = {}
215-
for row in self:
216-
for col, i in enumerate(row):
217-
cols[i] = col
214+
cols = {i: col for row in self for col, i in enumerate(row)}
218215
return sorted(i for i in cols if i + 1 in cols and cols[i + 1] >= cols[i])
219216

220217
def descent_composition(self):

src/sage/combinat/diagram.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,9 @@ def _latex_(self):
336336

337337
lr = r'\def\lr#1{\multicolumn{1}{|@{\hspace{.6ex}}c@{\hspace{.6ex}}|}{\raisebox{-.3ex}{$#1$}}}'
338338

339-
array = []
340-
for i in range(self._n_rows):
341-
row = []
342-
for j in range(self._n_cols):
343-
row.append("\\phantom{x}" if (i, j) in self else None)
344-
array.append(row)
339+
array = [[("\\phantom{x}" if (i, j) in self else None)
340+
for j in range(self._n_cols)]
341+
for i in range(self._n_rows)]
345342

346343
def end_line(r):
347344
# give the line ending to row ``r``

src/sage/combinat/diagram_algebras.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -3013,7 +3013,7 @@ def jucys_murphy_element(self, i):
30133013
sage: L = [P.L(i/2) for i in range(1,2*k+1)]
30143014
sage: all(x.dual() == x for x in L)
30153015
True
3016-
sage: all(x * y == y * x for x in L for y in L) # long time
3016+
sage: all(x * y == y * x for x, y in Subsets(L, 2)) # long time
30173017
True
30183018
sage: Lsum = sum(L)
30193019
sage: gens = [P.s(i) for i in range(1,k)]
@@ -3045,13 +3045,13 @@ def jucys_murphy_element(self, i):
30453045
30463046
The same tests for a half integer partition algebra::
30473047
3048-
sage: k = 9/2
3048+
sage: k = 7/2
30493049
sage: R.<n> = QQ[]
30503050
sage: P = PartitionAlgebra(k, n)
30513051
sage: L = [P.L(i/2) for i in range(1,2*k+1)]
30523052
sage: all(x.dual() == x for x in L)
30533053
True
3054-
sage: all(x * y == y * x for x in L for y in L) # long time
3054+
sage: all(x * y == y * x for x, y in Subsets(L, 2)) # long time
30553055
True
30563056
sage: Lsum = sum(L)
30573057
sage: gens = [P.s(i) for i in range(1,k-1/2)]
@@ -5847,11 +5847,9 @@ def to_Brauer_partition(l, k=None):
58475847
True
58485848
"""
58495849
L = to_set_partition(l, k=k)
5850-
L2 = []
58515850
paired = []
58525851
not_paired = []
5853-
for i in L:
5854-
L2.append(list(i))
5852+
L2 = (list(i) for i in L)
58555853
for i in L2:
58565854
if len(i) > 2:
58575855
raise ValueError("blocks must have size at most 2, but {} has {}".format(i, len(i)))

src/sage/combinat/dlx.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,7 @@ def AllExactCovers(M):
484484
ones = []
485485
r = 1 # damn 1-indexing
486486
for R in M.rows():
487-
row = []
488-
for i in range(len(R)):
489-
if R[i]:
490-
row.append(i + 1) # damn 1-indexing
487+
row = [i for i, Ri in enumerate(R, start=1) if Ri]
491488
ones.append([r, row])
492489
r += 1
493490
for s in DLXMatrix(ones):

src/sage/combinat/finite_state_machine.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -9437,9 +9437,9 @@ def graph(self, edge_labels='words_in_out'):
94379437
transitions = state.transitions
94389438
if not transitions:
94399439
isolated_vertices.append(state.label())
9440-
for t in transitions:
9441-
graph_data.append((t.from_state.label(), t.to_state.label(),
9442-
label_fct(t)))
9440+
graph_data.extend((t.from_state.label(), t.to_state.label(),
9441+
label_fct(t))
9442+
for t in transitions)
94439443

94449444
G = DiGraph(graph_data, multiedges=True, loops=True)
94459445
G.add_vertices(isolated_vertices)

src/sage/combinat/free_dendriform_algebra.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -939,14 +939,12 @@ def merge(self, other):
939939
return self
940940
ret = list(self.vars)
941941
cur_vars = set(ret)
942-
for v in other.vars:
943-
if v not in cur_vars:
944-
ret.append(v)
942+
ret.extend(v for v in other.vars if v not in cur_vars)
945943
return DendriformFunctor(Alphabet(ret))
946-
else:
947-
return None
948944

949-
def _repr_(self):
945+
return None
946+
947+
def _repr_(self) -> str:
950948
"""
951949
TESTS::
952950

src/sage/combinat/free_prelie_algebra.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1023,14 +1023,12 @@ def merge(self, other):
10231023
return self
10241024
ret = list(self.vars)
10251025
cur_vars = set(ret)
1026-
for v in other.vars:
1027-
if v not in cur_vars:
1028-
ret.append(v)
1026+
ret.extend(v for v in other.vars if v not in cur_vars)
10291027
return PreLieFunctor(Alphabet(ret))
1030-
else:
1031-
return None
10321028

1033-
def _repr_(self):
1029+
return None
1030+
1031+
def _repr_(self) -> str:
10341032
"""
10351033
TESTS::
10361034

src/sage/combinat/gelfand_tsetlin_patterns.py

+9-15
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,9 @@ def boxed_entries(self) -> tuple:
302302
sage: G.boxed_entries()
303303
((1, 0),)
304304
"""
305-
ret = []
306-
for i in range(1, len(self)):
307-
for j in range(len(self[i])):
308-
if self[i][j] == self[i - 1][j]:
309-
ret.append((i, j))
305+
ret = [(i, j) for i in range(1, len(self))
306+
for j, selfij in enumerate(self[i])
307+
if selfij == self[i - 1][j]]
310308
return tuple(ret)
311309

312310
@cached_method
@@ -324,11 +322,9 @@ def circled_entries(self) -> tuple:
324322
sage: G.circled_entries()
325323
((1, 1), (2, 0))
326324
"""
327-
ret = []
328-
for i in range(1, len(self)):
329-
for j in range(len(self[i])):
330-
if self[i][j] == self[i - 1][j + 1]:
331-
ret.append((i, j))
325+
ret = [(i, j) for i in range(1, len(self))
326+
for j, selfij in enumerate(self[i])
327+
if selfij == self[i - 1][j + 1]]
332328
return tuple(ret)
333329

334330
@cached_method
@@ -349,11 +345,9 @@ def special_entries(self) -> tuple:
349345
sage: G.special_entries()
350346
((2, 0),)
351347
"""
352-
ret = []
353-
for i in range(1, len(self)):
354-
for j in range(len(self[i])):
355-
if self[i-1][j] > self[i][j] and self[i][j] > self[i-1][j+1]:
356-
ret.append((i, j))
348+
ret = [(i, j) for i in range(1, len(self))
349+
for j, selfij in enumerate(self[i])
350+
if self[i - 1][j] > selfij > self[i - 1][j + 1]]
357351
return tuple(ret)
358352

359353
def number_of_boxes(self) -> int:

src/sage/combinat/graph_path.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,7 @@ def paths_from_source_to_target(self, source, target):
236236
[[2, 3, 4], [2, 4]]
237237
"""
238238
source_paths = self.outgoing_paths(source)
239-
paths = []
240-
for path in source_paths:
241-
if path[-1] == target:
242-
paths.append(path)
243-
return paths
239+
return [path for path in source_paths if path[-1] == target]
244240

245241
def paths(self):
246242
"""

src/sage/combinat/integer_vector.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1174,19 +1174,17 @@ def _list_rec(self, n, k):
11741174
EXAMPLES::
11751175
11761176
sage: IV = IntegerVectors(2,3)
1177-
sage: IV._list_rec(2,3)
1177+
sage: list(IV._list_rec(2,3))
11781178
[(2, 0, 0), (1, 1, 0), (1, 0, 1), (0, 2, 0), (0, 1, 1), (0, 0, 2)]
11791179
"""
1180-
res = []
1181-
11821180
if k == 1:
1183-
return [(n, )]
1181+
yield (n,)
1182+
return
11841183

11851184
for nbar in range(n + 1):
11861185
n_diff = n - nbar
11871186
for rest in self._list_rec(nbar, k - 1):
1188-
res.append((n_diff,) + rest)
1189-
return res
1187+
yield (n_diff,) + rest
11901188

11911189
def __iter__(self):
11921190
"""

src/sage/combinat/knutson_tao_puzzles.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -2064,12 +2064,9 @@ def _fill_piece(self, nw_label, ne_label, pieces) -> list[PuzzlePiece]:
20642064
sage: ps._fill_piece('0', '0', ps._bottom_deltas)
20652065
[0/0\0]
20662066
"""
2067-
output = []
2068-
for piece in pieces:
2069-
if (piece['north_west'] == nw_label and
2070-
piece['north_east'] == ne_label):
2071-
output.append(piece)
2072-
return output
2067+
return [piece for piece in pieces
2068+
if (piece['north_west'] == nw_label and
2069+
piece['north_east'] == ne_label)]
20732070

20742071
@cached_method
20752072
def _fill_strip(self, nw_labels, ne_label, pieces, final_pieces=None):

src/sage/combinat/misc.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,7 @@ def _monomial_exponent_to_lower_factorial(me, x):
202202
sage: _monomial_exponent_to_lower_factorial(([2,2,2]),a)
203203
x^2*y^2*z^2 - x^2*y^2*z - x^2*y*z^2 - x*y^2*z^2 + x^2*y*z + x*y^2*z + x*y*z^2 - x*y*z
204204
"""
205-
terms = []
206-
for i in range(len(me)):
207-
for j in range(me[i]):
208-
terms.append( x[i]-j )
209-
return prod(terms)
205+
return prod(x[i] - j for i, mei in enumerate(me) for j in range(mei))
210206

211207

212208
def umbral_operation(poly):
@@ -235,7 +231,7 @@ def umbral_operation(poly):
235231
exponents = poly.exponents()
236232
coefficients = poly.coefficients()
237233
length = len(exponents)
238-
return sum( [coefficients[i]*_monomial_exponent_to_lower_factorial(exponents[i],x) for i in range(length)] )
234+
return sum(coefficients[i]*_monomial_exponent_to_lower_factorial(exponents[i], x) for i in range(length))
239235

240236

241237
class IterableFunctionCall:

src/sage/combinat/parallelogram_polyomino.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -1974,12 +1974,9 @@ def widths(self) -> list:
19741974
sage: pp.widths()
19751975
[]
19761976
"""
1977-
widths = []
19781977
uw = self.upper_widths()
19791978
lw = self.lower_widths()
1980-
for i in range(len(lw)):
1981-
widths.append(uw[i] - lw[i])
1982-
return widths
1979+
return [up - lo for up, lo in zip(uw, lw)]
19831980

19841981
def degree_convexity(self) -> int:
19851982
r"""
@@ -3387,11 +3384,10 @@ def get_BS_nodes(self):
33873384
sage: pp.set_options(drawing_components=dict(tree=True))
33883385
sage: view(pp) # not tested
33893386
"""
3390-
result = []
3391-
for h in range(1, self.height()):
3392-
result.append(self._get_node_position_at_row(h))
3393-
for w in range(1, self.width()):
3394-
result.append(self._get_node_position_at_column(w))
3387+
result = [self._get_node_position_at_row(h)
3388+
for h in range(1, self.height())]
3389+
result.extend(self._get_node_position_at_column(w)
3390+
for w in range(1, self.width()))
33953391
return result
33963392

33973393
def get_right_BS_nodes(self):

src/sage/combinat/partition_algebra.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,7 @@ def __iter__(self):
364364
True
365365
"""
366366
for p in Permutations(self.k):
367-
res = []
368-
for i in range(self.k):
369-
res.append(Set([i + 1, -p[i]]))
367+
res = [Set([i, -pi]) for i, pi in enumerate(p, start=1)]
370368
yield self.element_class(self, res)
371369

372370

@@ -433,10 +431,7 @@ def __iter__(self):
433431
{{1, -3}, {2, -2}, {4, -4}, {3, -1}}]
434432
"""
435433
for p in Permutations(self.k):
436-
res = []
437-
for i in range(self.k):
438-
res.append(Set([i + 1, -p[i]]))
439-
434+
res = [Set([i, -pi]) for i, pi in enumerate(p, start=1)]
440435
res.append(Set([self.k + 1, -self.k - 1]))
441436
yield self.element_class(self, res)
442437

@@ -1941,8 +1936,8 @@ def to_set_partition(l, k=None):
19411936
to_be_added -= spart
19421937
sp.append(spart)
19431938

1944-
for singleton in to_be_added:
1945-
sp.append(Set([singleton]))
1939+
sp.extend(Set([singleton])
1940+
for singleton in to_be_added)
19461941

19471942
return Set(sp)
19481943

src/sage/combinat/ribbon_shaped_tableau.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,17 @@ def from_permutation(self, p):
350350
[[1, 2], [3]],
351351
[[1], [2], [3]]]
352352
"""
353-
if p == []:
353+
if not p:
354354
return self.element_class(self, [])
355355

356356
comp = p.descents()
357357

358-
if comp == []:
358+
if not comp:
359359
return self.element_class(self, [p[:]])
360360

361-
r = []
362-
r.append([p[j] for j in range(comp[0])])
363-
for i in range(len(comp) - 1):
364-
r.append([p[j] for j in range(comp[i], comp[i + 1])])
361+
r = [[p[j] for j in range(comp[0])]]
362+
r.extend([p[j] for j in range(comp[i], comp[i + 1])]
363+
for i in range(len(comp) - 1))
365364
r.append([p[j] for j in range(comp[-1], len(p))])
366365
r.reverse()
367366
return self.element_class(self, r)

src/sage/combinat/rsk.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2957,12 +2957,10 @@ def _backward_format_output(self, obj1, obj2, output):
29572957
if j == 0:
29582958
df.append([])
29592959
if j > 0 and obj1[j] < obj1[j-1]:
2960-
for _ in range(obj1[j-1]-obj1[j]):
2961-
df.append([])
2960+
df.extend([] for _ in range(obj1[j-1]-obj1[j]))
29622961
df[-1].append(obj2[j])
29632962
if obj1:
2964-
for a in range(obj1[-1]-1):
2965-
df.append([])
2963+
df.extend([] for a in range(obj1[-1]-1))
29662964
# If biword is empty, return a decreasing factorization with 1 factor
29672965
else:
29682966
df.append([])

src/sage/combinat/shifted_primed_tableau.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -2716,9 +2716,11 @@ def _add_strip(sub_tab, full_tab, length):
27162716
if sub_tab and len(sub_tab) < len(full_tab):
27172717
plat_list.append(min(sub_tab[-1] + primed_strip[-2] - 1,
27182718
full_tab[len(sub_tab)]))
2719-
for row in reversed(range(1, len(sub_tab))):
2720-
plat_list.append(min(sub_tab[row-1]+primed_strip[row-1]-1, full_tab[row])
2721-
- sub_tab[row] - primed_strip[row])
2719+
plat_list.extend(
2720+
min(sub_tab[row-1] + primed_strip[row-1] - 1, full_tab[row])
2721+
- sub_tab[row] - primed_strip[row]
2722+
for row in reversed(range(1, len(sub_tab))))
2723+
27222724
if sub_tab:
27232725
plat_list.append(full_tab[0] - sub_tab[0] - primed_strip[0])
27242726
else:

0 commit comments

Comments
 (0)