Skip to content

Commit

Permalink
sagemathgh-39342: simplify some .extend in combinat
Browse files Browse the repository at this point in the history
    
by avoiding lists inside if possible

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
    
URL: sagemath#39342
Reported by: Frédéric Chapoton
Reviewer(s): David Coudert, Frédéric Chapoton
  • Loading branch information
Release Manager committed Jan 25, 2025
2 parents 5ec8c38 + 6c58d92 commit 6e1c13f
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 56 deletions.
7 changes: 4 additions & 3 deletions src/sage/combinat/designs/bibd.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ def BIBD_from_TD(v, k, existence=False):

BIBD = TDkv._blocks
for i in range(k):
BIBD.extend([[x+i*v for x in B] for B in BIBDvk])
BIBD.extend([x+i*v for x in B] for B in BIBDvk)

# Second construction
elif ((v-1) % k == 0 and
Expand All @@ -630,7 +630,7 @@ def BIBD_from_TD(v, k, existence=False):
inf = v*k
BIBD = TDkv
for i in range(k):
BIBD.extend([[inf if x == v else x+i*v for x in B] for B in BIBDv1k])
BIBD.extend([inf if x == v else x+i*v for x in B] for B in BIBDv1k)

# Third construction
elif ((v-k) % k == 0 and
Expand All @@ -650,7 +650,8 @@ def BIBD_from_TD(v, k, existence=False):
BIBDvpkk = [B for B in BIBDvpkk if min(B) < v]

for i in range(k):
BIBD.extend([[(x-v)+inf if x >= v else x+i*v for x in B] for B in BIBDvpkk])
BIBD.extend([(x-v)+inf if x >= v else x+i*v for x in B]
for B in BIBDvpkk)

BIBD.append(list(range(k * v, v * k + k)))

Expand Down
21 changes: 11 additions & 10 deletions src/sage/combinat/designs/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,8 +1058,8 @@ def OA_11_185():
assert all(len(B) in (11,13) or set(B) == set(special_set) for B in PBD)

OA = OA_from_PBD(11,185,[B for B in PBD if len(B) < 17],check=False)[:-185]
OA.extend([[i]*11 for i in range(185) if i not in special_set])
OA.extend([[special_set[x] for x in B] for B in orthogonal_array(11,17)])
OA.extend([i]*11 for i in range(185) if i not in special_set)
OA.extend([special_set[x] for x in B] for B in orthogonal_array(11,17))
return OA


Expand Down Expand Up @@ -1126,21 +1126,21 @@ def OA_10_205():
GDD = [[relabel[xx] for xx in B if xx in relabel] for B in pplane if p not in B]

# We turn the GDD into a PBD by extending the groups with a new point 204.
GDD.extend([[relabel[xx] for xx in G]+[204] for G in groups])
GDD.extend([relabel[xx] for xx in G]+[204] for G in groups)

# We build the OA, knowing that the blocks of size 9 are disjoint
blocks_of_size_9 = [B for B in GDD if len(B) == 9]
blocks_of_size_9_union = sum(blocks_of_size_9,[])

OA = OA_from_PBD(10,205,[B for B in GDD if len(B) != 9],check=False)[:-205]

OA.extend([[B[xx] for xx in R]
for R in orthogonal_array(10,9)
for B in blocks_of_size_9])
OA.extend([B[xx] for xx in R]
for R in orthogonal_array(10,9)
for B in blocks_of_size_9)

# The missing [i,i,...] blocks
OA.extend([[i]*10
for i in set(range(205)).difference(blocks_of_size_9_union)])
OA.extend([i]*10
for i in set(range(205)).difference(blocks_of_size_9_union))

return OA

Expand Down Expand Up @@ -1627,7 +1627,8 @@ def OA_10_796():
OA = OA_relabel(OA,17,47,blocks=[OA[0]]) # making sure [46]*17 is a block
PBD = [[i*47+x for i,x in enumerate(B) if (x < 46 or i < 13)] for B in OA]
extra_point = 10000
PBD.extend([list(range(i*47,(i+1)*47-int(i >= 13)))+[extra_point] for i in range(17)]) # Adding the columns
PBD.extend(list(range(i*47,(i+1)*47-int(i >= 13)))+[extra_point]
for i in range(17)) # Adding the columns

rel = {v:i for i,v in enumerate(set(range(17*47)).difference([(i+1)*47-1 for i in range(13,17)]))}
rel[extra_point] = len(rel)
Expand All @@ -1645,7 +1646,7 @@ def OA_10_796():
for B in PBD:
if len(B) >= 47:
B.sort(key=lambda x:int(x == extra_point))
OA.extend([[B[i] for i in BB] for BB in iOA[len(B)]])
OA.extend([B[i] for i in BB] for BB in iOA[len(B)])
span.update(B[:-1])
else:
others.append(B)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def construction_3_5(k, n, m, r, s, t, explain_construction=False):
group_k_3 = [B[-1] for B in master_design if B[-3] not in group_k_1 and B[-2] not in group_k_2]
group_k_3 = list(set(group_k_3))
assert len(group_k_3) <= t
group_k_3.extend([x for x in range(q) if x not in group_k_3])
group_k_3.extend(x for x in range(q) if x not in group_k_3)
group_k_3 = group_k_3[:t]

# Relabelling the OA
Expand Down Expand Up @@ -545,7 +545,7 @@ def construction_q_x(k, q, x, check=True, explain_construction=False):
# delete points.
#
# TD.extend([range(i*q,(i+1)*q) for i in range(x)])
TD.extend([list(range(i*q,(i+1)*q))+[p2] for i in range(x,q)])
TD.extend(list(range(i*q,(i+1)*q))+[p2] for i in range(x,q))

points_to_delete = set([i*q+j for i in range(x) for j in range(1,q)]+[i*q for i in range(x,q)])
points_to_keep = set(range(q**2+2))-points_to_delete
Expand Down Expand Up @@ -1567,7 +1567,8 @@ def brouwer_separable_design(k, t, q, x, check=False, verbose=False, explain_con
blocks_of_size_q_plus_t = _reorder_matrix(blocks_of_size_q_plus_t)

for i,classs in enumerate(OA_tq1_classes):
OA.extend([R[xx] if xx < t+q else N-i-1 for xx in B] for R in blocks_of_size_q_plus_t for B in classs)
OA.extend([R[xx] if xx < t+q else N-i-1 for xx in B]
for R in blocks_of_size_q_plus_t for B in classs)

# The set of size x
OA.extend([N-1-xx for xx in R] for R in orthogonal_array(k,x))
Expand Down
14 changes: 7 additions & 7 deletions src/sage/combinat/interval_posets.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,8 @@ def _mul_(self, other: TIP) -> TIP:
n = self._size
m = other.size()
relations = self._poset.cover_relations()
relations.extend([(i + n, j + n)
for i, j in other._poset.cover_relations_iterator()])
relations.extend((i + n, j + n)
for i, j in other._poset.cover_relations_iterator())
P = FinitePoset(DiGraph([list(range(1, n + m + 1)), relations],
format='vertices_and_edges')) # type:ignore
return TamariIntervalPoset(P, check=False) # type:ignore
Expand Down Expand Up @@ -1980,9 +1980,9 @@ def subposet(self, start, end) -> TIP:
relations = [(i - start + 1, j - start + 1)
for (i, j) in self.increasing_cover_relations()
if i >= start and j < end]
relations.extend([(j - start + 1, i - start + 1)
for (j, i) in self.decreasing_cover_relations()
if i >= start and j < end])
relations.extend((j - start + 1, i - start + 1)
for (j, i) in self.decreasing_cover_relations()
if i >= start and j < end)
return TamariIntervalPoset(end - start, relations, check=False)

sub_poset = subposet
Expand Down Expand Up @@ -3110,7 +3110,7 @@ def get_relations(bt, start=1):
rroots, rrelations, rindex = get_relations(bt[1], start=index + 1)
roots.append(index)
relations.extend(rrelations)
relations.extend([(j, index) for j in rroots])
relations.extend((j, index) for j in rroots)
return roots, relations, rindex

_, relations, index = get_relations(binary_tree)
Expand Down Expand Up @@ -3224,7 +3224,7 @@ def get_relations(bt, start=1):
roots, relations, rindex = get_relations(bt[1], start=index + 1)
roots.append(index)
relations.extend(lrelations)
relations.extend([(j, index) for j in lroots])
relations.extend((j, index) for j in lroots)
return roots, relations, rindex

_, relations, index = get_relations(binary_tree)
Expand Down
4 changes: 2 additions & 2 deletions src/sage/combinat/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -2720,7 +2720,7 @@ def suter_diagonal_slide(self, n, exp=1):
exp += 1
continue
res = [n - leng - 1]
res.extend([i - 1 for i in ret._list if i > 1])
res.extend(i - 1 for i in ret._list if i > 1)
ret = Partition(res)
exp += 1
return ret
Expand Down Expand Up @@ -3066,7 +3066,7 @@ def young_subgroup(self):
gens = []
m = 0
for row in self:
gens.extend([(c, c+1) for c in range(m+1, m+row)])
gens.extend((c, c + 1) for c in range(m + 1, m + row))
m += row
gens.append(list(range(1, self.size() + 1))) # to ensure we get a subgroup of Sym_n
return PermutationGroup(gens)
Expand Down
4 changes: 2 additions & 2 deletions src/sage/combinat/partition_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -1485,7 +1485,7 @@ def young_subgroup(self):
m = 0
for comp in self:
for row in comp:
gens.extend([(c, c+1) for c in range(m+1, m+row)])
gens.extend((c, c + 1) for c in range(m + 1, m + row))
m += row
gens.append(list(range(1, self.size()+1))) # to ensure we get a subgroup of Sym_n
return PermutationGroup(gens)
Expand All @@ -1504,7 +1504,7 @@ def young_subgroup_generators(self):
m = 0
for comp in self:
for row in comp:
gens.extend(list(range(m + 1, m + row)))
gens.extend(range(m + 1, m + row))
m += row
return gens

Expand Down
2 changes: 1 addition & 1 deletion src/sage/combinat/posets/poset_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -1590,7 +1590,7 @@ def DoubleTailedDiamond(n):

edges = [(i, i + 1) for i in range(1, n)]
edges.extend([(n, n + 1), (n, n + 2), (n + 1, n + 3), (n + 2, n + 3)])
edges.extend([(i, i + 1) for i in range(n + 3, 2 * n + 2)])
edges.extend((i, i + 1) for i in range(n + 3, 2 * n + 2))
p = DiGraph([list(range(1, 2 * n + 3)), edges])
return DCompletePoset(p)

Expand Down
6 changes: 3 additions & 3 deletions src/sage/combinat/posets/posets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2584,10 +2584,10 @@ def intervals_poset(self):

covers = []
for a, b in ints:
covers.extend([[(a, b), (a, bb)] for bb in self.upper_covers(b)])
covers.extend([(a, b), (a, bb)] for bb in self.upper_covers(b))
if a != b:
covers.extend([[(a, b), (aa, b)] for aa in self.upper_covers(a)
if self.le(aa, b)])
covers.extend([(a, b), (aa, b)] for aa in self.upper_covers(a)
if self.le(aa, b))

dg = DiGraph([ints, covers], format='vertices_and_edges')
return constructor(dg, cover_relations=True)
Expand Down
8 changes: 4 additions & 4 deletions src/sage/combinat/rigged_configurations/kr_tableaux.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,17 +800,17 @@ def _fill(self, shape):
shape_list.append(0)

lst = []
for col in range(1, self._s+1):
for col in range(1, self._s + 1):
if (self._s - col) % 2 == 0:
lst.extend( [self.letters(self._r - x) for x in range(self._r)] )
lst.extend(self.letters(self._r - x) for x in range(self._r))
else:
m = self._r
for j, val in enumerate(shape_list):
if col >= val:
m = j
break
lst.extend([self.letters(-x) for x in range(m+1, self._r+1)])
lst.extend([self.letters(m - x) for x in range(m)])
lst.extend(self.letters(-x) for x in range(m + 1, self._r + 1))
lst.extend(self.letters(m - x) for x in range(m))

return self.element_class(self, lst)

Expand Down
3 changes: 2 additions & 1 deletion src/sage/combinat/root_system/branching_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -1774,7 +1774,8 @@ def rule(x):
if all(t[0] == 'A' for t in stypes):
def rule(x):
ret = [sum(x[i*ns[1]:(i+1)*ns[1]]) for i in range(ns[0])]
ret.extend([sum(x[ns[1]*j+i] for j in range(ns[0])) for i in range(ns[1])])
ret.extend(sum(x[ns[1]*j+i] for j in range(ns[0]))
for i in range(ns[1]))
return ret
return BranchingRule(Rtype, Stype, rule, "tensor")
else:
Expand Down
12 changes: 6 additions & 6 deletions src/sage/combinat/root_system/reflection_group_real.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,24 @@ def ReflectionGroup(*args, **kwds):
X = arg

# precheck for valid input data
if not (isinstance(X, (CartanType_abstract,tuple)) or (X in ZZ and 4 <= X <= 37)):
if not (isinstance(X, (CartanType_abstract, tuple)) or (X in ZZ and 4 <= X <= 37)):
raise ValueError(error_msg % X)

# transforming two reducible types and an irreducible type
if isinstance(X, CartanType_abstract):
if not X.is_finite():
raise ValueError(error_msg % X)
if hasattr(X,"cartan_type"):
if hasattr(X, "cartan_type"):
X = X.cartan_type()
if X.is_irreducible():
W_types.extend([(X.letter, X.n)])
else:
W_types.extend([(x.letter, x.n) for x in X.component_types()])
W_types.extend((x.letter, x.n) for x in X.component_types())

elif X == (2,2,2) or X == ('I',2):
W_types.extend([('A',1), ('A',1)])
elif X == (2, 2, 2) or X == ('I', 2):
W_types.extend([('A', 1), ('A', 1)])

elif X == (2,2,3):
elif X == (2, 2, 3):
W_types.extend([('A', 3)])

else:
Expand Down
12 changes: 6 additions & 6 deletions src/sage/combinat/root_system/type_C.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ def positive_roots(self):
(0, 0, 2)]
"""
res = []
for p in [0,1]:
for p in [0, 1]:
for j in range(self.n):
res.extend([self.root(i,j,0,p) for i in range(j)])
res.extend([self.root(i,i,0,0) for i in range(self.n)])
res.extend(self.root(i, j, 0, p) for i in range(j))
res.extend(self.root(i, i, 0, 0) for i in range(self.n))
return res

def negative_roots(self):
Expand All @@ -105,10 +105,10 @@ def negative_roots(self):
(0, 0, -2)]
"""
res = []
for p in [0,1]:
for p in [0, 1]:
for j in range(self.n):
res.extend( [self.root(i,j,1,p) for i in range(j) ] )
res.extend( [ self.root(i,i,1,1) for i in range(self.n) ] )
res.extend(self.root(i, j, 1, p) for i in range(j))
res.extend(self.root(i, i, 1, 1) for i in range(self.n))
return res

def fundamental_weight(self, i):
Expand Down
4 changes: 2 additions & 2 deletions src/sage/combinat/root_system/type_D.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def positive_roots(self):
res = []
for p in [0, 1]:
for j in range(self.n):
res.extend([self.root(i, j, 0, p) for i in range(j)])
res.extend(self.root(i, j, 0, p) for i in range(j))
return res

def negative_roots(self):
Expand All @@ -96,7 +96,7 @@ def negative_roots(self):
res = []
for p in [0, 1]:
for j in range(self.n):
res.extend([self.root(i, j, 1, p) for i in range(j)])
res.extend(self.root(i, j, 1, p) for i in range(j))
return res

def fundamental_weight(self, i):
Expand Down
9 changes: 3 additions & 6 deletions src/sage/combinat/skew_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,12 +1009,9 @@ def frobenius_rank(self):
"""
N = len(self[0])
mu_betas = [x - j for j, x in enumerate(self[1])]
mu_betas.extend([- j for j in range(len(self[1]), N)])
res = 0
for i, x in enumerate(self[0]):
if (x - i) not in mu_betas:
res += 1
return res
mu_betas.extend(- j for j in range(len(self[1]), N))
return sum(1 for i, x in enumerate(self[0])
if (x - i) not in mu_betas)

def cells(self):
"""
Expand Down

0 comments on commit 6e1c13f

Please sign in to comment.