Skip to content

Commit

Permalink
gh-37771: some C4 fixes in combinat folder
Browse files Browse the repository at this point in the history
    
Just fixing a few of the `ruff --select=C4` suggestions in the
`combinat` folder

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
    
URL: #37771
Reported by: Frédéric Chapoton
Reviewer(s): Frédéric Chapoton, Matthias Köppe
  • Loading branch information
Release Manager committed Apr 10, 2024
2 parents 7d17cca + b9ae35b commit 394f021
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 66 deletions.
9 changes: 6 additions & 3 deletions src/sage/algebras/quatalg/quaternion_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -1212,9 +1212,12 @@ def ramified_primes(self):
if not is_RationalField(self.base_ring()):
raise ValueError("base field must be the rational numbers")

return sorted([p for p in set([2]).union(prime_divisors(self._a.numerator()),
prime_divisors(self._a.denominator()), prime_divisors(self._b.numerator()),
prime_divisors(self._b.denominator())) if hilbert_symbol(self._a, self._b, p) == -1])
a, b = self._a, self._b
return sorted(p for p in {2}.union(prime_divisors(a.numerator()),
prime_divisors(a.denominator()),
prime_divisors(b.numerator()),
prime_divisors(b.denominator()))
if hilbert_symbol(self._a, self._b, p) == -1)

def is_isomorphic(self, A) -> bool:
"""
Expand Down
4 changes: 2 additions & 2 deletions src/sage/arith/functions.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def lcm(a, b=None):
- ``a,b`` -- two elements of a ring with lcm or
- ``a`` -- a list or tuple of elements of a ring with lcm
- ``a`` -- a list, tuple or iterable of elements of a ring with lcm
OUTPUT:
Expand Down Expand Up @@ -136,7 +136,7 @@ cpdef LCM_list(v):
INPUT:
- ``v`` -- an iterable
- ``v`` -- an iterable
OUTPUT: integer
Expand Down
2 changes: 1 addition & 1 deletion src/sage/combinat/sloane_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ def _eval(self, n):
sage: [sloane.A003418._eval(n) for n in range(1,11)]
[1, 2, 6, 12, 60, 60, 420, 840, 2520, 2520]
"""
return arith.lcm([i for i in range(1, n+1)])
return arith.lcm(range(1, n + 1))


class A007318(SloaneSequence):
Expand Down
2 changes: 1 addition & 1 deletion src/sage/combinat/species/partition_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def _structures(self, structure_class, labels):
yield structure_class(self, labels, [])
return

u = [i for i in reversed(range(1, n + 1))]
u = list(range(n, 0, -1))
s0 = u.pop()

# Reconstruct the set partitions from
Expand Down
3 changes: 2 additions & 1 deletion src/sage/combinat/subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,8 @@ def last(self):
if self._k > self._s.cardinality():
raise EmptySetError

return self.element_class([i for i in itertools.islice(reversed(self._s), int(self._k))])
return self.element_class(list(itertools.islice(reversed(self._s),
int(self._k))))

def _fast_iterator(self):
r"""
Expand Down
12 changes: 6 additions & 6 deletions src/sage/combinat/subword_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1855,8 +1855,8 @@ def cover_relations(self, label=False):
"""
N = len(self.group().long_element(as_word=True))
F = self.greedy_facet(side="positive")
Fs = set([F])
seen = set([F])
Fs = {F}
seen = {F}
covers = []
while Fs:
F = Fs.pop()
Expand Down Expand Up @@ -1896,7 +1896,7 @@ def increasing_flip_graph(self, label=True):
from sage.graphs.digraph import DiGraph
return DiGraph(self.cover_relations(label=label))

def interval(self, I, J):
def interval(self, I, J) -> set:
"""
Return the interval [I,J] in the increasing flip graph subword complex.
Expand Down Expand Up @@ -1925,7 +1925,7 @@ def interval(self, I, J):
"""
G = self.increasing_flip_graph()
paths = G.all_paths(I, J)
return set(K for path in paths for K in path)
return {K for path in paths for K in path}

def increasing_flip_poset(self):
"""
Expand Down Expand Up @@ -2126,8 +2126,8 @@ def _greedy_flip_algorithm(Q, w):
flip_to_ancestors.append(j)
next_index = i + 1
has_new_child = True
facet_list.append([x for x in F])
extended_root_conf_indices_list.append([x for x in R])
facet_list.append(list(F))
extended_root_conf_indices_list.append(list(R))
if not has_new_child:
i = flip_to_ancestors.pop()
if i != -1:
Expand Down
2 changes: 1 addition & 1 deletion src/sage/combinat/superpartition.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ def add_horizontal_border_strip_star(self, h) -> list:
# TODO: Check that this is not supposed to be
# a tuple of size 1
+ [(i) for i in circ_list if row_changed[i[0]] == 0]]
if len(set([k for (j, k) in new_sp[1]])) == len(new_sp[1]):
if len({k for j, k in new_sp[1]}) == len(new_sp[1]):
out += [SuperPartition.from_circled_diagram(*new_sp)]
return out

Expand Down
4 changes: 2 additions & 2 deletions src/sage/combinat/tableau_residues.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def __getitem__(self, k):
except (IndexError, KeyError):
raise IndexError('k must be in the range 1, 2, ..., {}'.format(len(self)))

def residues(self):
def residues(self) -> list:
r"""
Return a list of the residue sequence.
Expand All @@ -351,7 +351,7 @@ def residues(self):
sage: ResidueSequence(3,(0,0,1),[0,0,1,1,2,2,3,3]).residues()
[0, 0, 1, 1, 2, 2, 0, 0]
"""
return [r for r in self]
return list(self)

def restrict(self, m):
r"""
Expand Down
9 changes: 4 additions & 5 deletions src/sage/combinat/tableau_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ def components(self):
6 7
8 9
"""
return [t for t in self]
return list(self)

def to_list(self):
"""
Expand Down Expand Up @@ -2372,9 +2372,8 @@ def list(self):
([[3, 5], [4]], [[1, 2]])]
"""
if self.is_finite():
return [y for y in self]
else:
raise NotImplementedError('this is an infinite set of tableaux')
return list(self)
raise NotImplementedError('this is an infinite set of tableaux')


class TableauTuples_all(TableauTuples):
Expand Down Expand Up @@ -2667,7 +2666,7 @@ def an_element(self):
if self.size() == 0:
return self.element_class(self, [[] for _ in range(self.level())])

tab = [[[m for m in range(1, self.size() + 1)]]]
tab = [[list(range(1, self.size() + 1))]]
for _ in range(self.level() - 1):
tab.append([])
return self.element_class(self, tab)
Expand Down
8 changes: 4 additions & 4 deletions src/sage/combinat/tamari_lattices.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ def paths_in_triangle(i, j, a, b) -> list[tuple[int, ...]]:
return [tuple([1] * j)]

if (j - 1) * a >= (i) * b:
result = [u + tuple([1]) for u in paths_in_triangle(i, j - 1, a, b)]
result += [u + tuple([0]) for u in paths_in_triangle(i - 1, j, a, b)]
result = [u + (1,) for u in paths_in_triangle(i, j - 1, a, b)]
result += [u + (0,) for u in paths_in_triangle(i - 1, j, a, b)]
return result

return [u + tuple([0]) for u in paths_in_triangle(i - 1, j, a, b)]
return [u + (0,) for u in paths_in_triangle(i - 1, j, a, b)]


def swap(p, i, m=1) -> tuple[int, ...]:
Expand Down Expand Up @@ -157,7 +157,7 @@ def swap(p, i, m=1) -> tuple[int, ...]:
height -= 1
if height <= 0:
found = True
q = [k for k in p]
q = list(p)
for k in range(i, j):
q[k] = p[k + 1]
q[j] = 0
Expand Down
32 changes: 14 additions & 18 deletions src/sage/combinat/tiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,12 +992,13 @@ def canonical_isometric_copies(self, orientation_preserving=True,
"""
if mod_box_isometries:
L = ncube_isometry_group_cosets(self._dimension, orientation_preserving)
P_cosets = set(frozenset((m * self).canonical() for m in coset) for coset in L)
P_cosets = {frozenset((m * self).canonical() for m in coset)
for coset in L}
P_cosets_representents = [min(s, key=lambda a: a.sorted_list()) for s in P_cosets]
return sorted(P_cosets_representents, key=lambda a:a.sorted_list())
return sorted(P_cosets_representents, key=lambda a: a.sorted_list())
else:
L = ncube_isometry_group(self._dimension, orientation_preserving)
P_images = set((m * self).canonical() for m in L)
P_images = {(m * self).canonical() for m in L}
return sorted(P_images, key=lambda a: a.sorted_list())

def translated_copies(self, box):
Expand Down Expand Up @@ -1223,8 +1224,8 @@ def isometric_copies(self, box, orientation_preserving=True,
raise ValueError("Dimension of input box must match the "
"dimension of the polyomino")
box_min_coords, box_max_coords = box.bounding_box()
if mod_box_isometries and len(set(b-a for (a,b) in zip(box_min_coords,
box_max_coords))) < box._dimension:
if mod_box_isometries and len({b - a for a, b in zip(box_min_coords,
box_max_coords)}) < box._dimension:
raise NotImplementedError("The code below assumes that the"
" sizes of the box (={}) are all distinct when"
" argument `mod_box_isometries` is True.".format(box))
Expand Down Expand Up @@ -1268,8 +1269,8 @@ def isometric_copies_intersection(self, box, orientation_preserving=True):
"""
all_distinct_cano = self.canonical_isometric_copies(orientation_preserving,
mod_box_isometries=False)
return set([t for cano in all_distinct_cano
for t in cano.translated_copies_intersection(box=box)])
return {t for cano in all_distinct_cano
for t in cano.translated_copies_intersection(box=box)}

def neighbor_edges(self):
r"""
Expand Down Expand Up @@ -1767,11 +1768,10 @@ def coord_to_int_dict(self):
((2, 0), 4), ((2, 1), 5)]
"""
if self._reusable:
return dict((c, i) for i, c in enumerate(self.space()))
else:
number_of_pieces = len(self._pieces)
return dict((c, i+number_of_pieces)
for i, c in enumerate(self.space()))
return {c: i for i, c in enumerate(self.space())}

number_of_pieces = len(self._pieces)
return {c: i + number_of_pieces for i, c in enumerate(self.space())}

@cached_method
def int_to_coord_dict(self):
Expand Down Expand Up @@ -1812,14 +1812,10 @@ def int_to_coord_dict(self):
True
sage: all(B[A[i]] == i for i in A)
True
"""
if self._reusable:
return dict((i, c) for i, c in enumerate(self.space()))
else:
number_of_pieces = len(self._pieces)
return dict((i+number_of_pieces, c)
for i, c in enumerate(self.space()))
return dict(enumerate(self.space()))
return dict(enumerate(self.space(), start=len(self._pieces)))

@cached_method
def rows_for_piece(self, i, mod_box_isometries=False):
Expand Down
4 changes: 2 additions & 2 deletions src/sage/combinat/tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self, S, k):
"""
self.S = S
self.k = k
self._index_list = list(set(S.index(s) for s in S))
self._index_list = list({S.index(s) for s in S})
category = FiniteEnumeratedSets()
Parent.__init__(self, category=category)

Expand Down Expand Up @@ -169,7 +169,7 @@ def __init__(self, S, k):
"""
self.S = S
self.k = k
self._index_list = list(set(S.index(s) for s in S))
self._index_list = list({S.index(s) for s in S})
category = FiniteEnumeratedSets()
Parent.__init__(self, category=category)

Expand Down
6 changes: 3 additions & 3 deletions src/sage/combinat/words/finite_word.py
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ def factor_set(self, n=None, algorithm='suffix tree'):
return Set(self.factor_iterator(n))
elif algorithm == 'naive':
if n is None:
S = set([self[0:0]])
S = {self[0:0]}
for n in range(1, self.length()+1):
for i in range(self.length()-n+1):
S.add(self[i:i+n])
Expand Down Expand Up @@ -3007,7 +3007,7 @@ def palindromes(self, f=None):
[word: , word: ab, word: abbabaab, word: ba, word: baba, word: bbabaa]
"""
LPS = self.lps_lengths(f)
return set(self[i - LPS[i]: i] for i in range(len(self) + 1))
return {self[i - LPS[i]: i] for i in range(len(self) + 1)}

def palindromic_complexity(self, n):
r"""
Expand Down Expand Up @@ -6416,7 +6416,7 @@ def delta(self):
return Words()([])
ss = self[0]
c = 0
v = list()
v = []
max_c = 0
for s in self:
if s == ss:
Expand Down
Loading

0 comments on commit 394f021

Please sign in to comment.