Skip to content

Commit 03bb8f3

Browse files
author
Release Manager
committed
gh-37017: cosmetic little change in topology/ (some ruff UP and PERF) a set of small fixes there ### 📝 Checklist - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. URL: #37017 Reported by: Frédéric Chapoton Reviewer(s): Frédéric Chapoton, Tobias Diez
2 parents 947a7cc + edcd5b8 commit 03bb8f3

10 files changed

+75
-93
lines changed

src/sage/topology/cell_complex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ def _repr_(self):
11951195
for dim in self.cells():
11961196
cells += len(self.cells()[dim])
11971197
if cells != 1:
1198-
cells_string = " and %s %s" % (cells, cells_name)
1198+
cells_string = " and {} {}".format(cells, cells_name)
11991199
else:
12001200
cells_string = " and 1 %s" % cell_name
12011201
return Name + " complex " + vertex_string + cells_string

src/sage/topology/cubical_complex.py

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -545,13 +545,12 @@ def _triangulation_(self):
545545
if self.dimension() < 0: # the empty cube
546546
return [Simplex(())] # the empty simplex
547547
v = tuple([max(j) for j in self.tuple()])
548+
Sv = Simplex((v,))
548549
if self.dimension() == 0: # just v
549-
return [Simplex((v,))]
550-
simplices = []
551-
for i in range(self.dimension()):
552-
for S in self.face(i, upper=False)._triangulation_():
553-
simplices.append(S.join(Simplex((v,)), rename_vertices=False))
554-
return simplices
550+
return [Sv]
551+
return [S.join(Sv, rename_vertices=False)
552+
for i in range(self.dimension())
553+
for S in self.face(i, upper=False)._triangulation_()]
555554

556555
def alexander_whitney(self, dim):
557556
r"""
@@ -719,7 +718,7 @@ def _repr_(self):
719718
sage: C1._repr_()
720719
'[1,1] x [2,3] x [4,5]'
721720
"""
722-
s = ("[%s,%s]" % (str(x), str(y)) for x, y in self.__tuple)
721+
s = ("[{},{}]".format(str(x), str(y)) for x, y in self.__tuple)
723722
return " x ".join(s)
724723

725724
def _latex_(self):
@@ -1077,8 +1076,8 @@ def cells(self, subcomplex=None):
10771076
dimension = max([cube.dimension() for cube in self._facets])
10781077
# initialize the lists: add each maximal cube to Cells and sub_facets
10791078
for i in range(-1, dimension+1):
1080-
Cells[i] = set([])
1081-
sub_facets[i] = set([])
1079+
Cells[i] = set()
1080+
sub_facets[i] = set()
10821081
for f in self._facets:
10831082
Cells[f.dimension()].add(f)
10841083
if subcomplex is not None:
@@ -1229,7 +1228,7 @@ def chain_complex(self, subcomplex=None, augmented=False,
12291228
differentials[dim] = self._complex[(dim, subcomplex)].change_ring(base_ring)
12301229
mat = differentials[dim]
12311230
if verbose:
1232-
print(" boundary matrix (cached): it's %s by %s." % (mat.nrows(), mat.ncols()))
1231+
print(" boundary matrix (cached): it's {} by {}.".format(mat.nrows(), mat.ncols()))
12331232
else:
12341233
# 'current' is the list of cells in dimension n
12351234
#
@@ -1271,7 +1270,7 @@ def chain_complex(self, subcomplex=None, augmented=False,
12711270
else:
12721271
differentials[dim] = mat.change_ring(base_ring)
12731272
if verbose:
1274-
print(" boundary matrix computed: it's %s by %s." % (mat.nrows(), mat.ncols()))
1273+
print(" boundary matrix computed: it's {} by {}.".format(mat.nrows(), mat.ncols()))
12751274
# finally, return the chain complex
12761275
if cochain:
12771276
return ChainComplex(data=differentials, base_ring=base_ring,
@@ -1452,7 +1451,7 @@ def suspension(self, n=1):
14521451

14531452
def product(self, other):
14541453
r"""
1455-
The product of this cubical complex with another one.
1454+
Return the product of this cubical complex with another one.
14561455
14571456
:param other: another cubical complex
14581457
@@ -1463,10 +1462,7 @@ def product(self, other):
14631462
sage: RP2.product(S1).homology()[1] # long time: 5 seconds
14641463
Z x C2
14651464
"""
1466-
facets = []
1467-
for f in self._facets:
1468-
for g in other._facets:
1469-
facets.append(f.product(g))
1465+
facets = [f.product(g) for f in self._facets for g in other._facets]
14701466
return CubicalComplex(facets)
14711467

14721468
def disjoint_union(self, other):
@@ -1490,11 +1486,12 @@ def disjoint_union(self, other):
14901486
embedded_left = len(tuple(self.maximal_cells()[0]))
14911487
embedded_right = len(tuple(other.maximal_cells()[0]))
14921488
zero = [0] * max(embedded_left, embedded_right)
1493-
facets = []
1494-
for f in self.maximal_cells():
1495-
facets.append(Cube([[0, 0]]).product(f._translate(zero)))
1496-
for f in other.maximal_cells():
1497-
facets.append(Cube([[1, 1]]).product(f._translate(zero)))
1489+
C00 = Cube([[0, 0]])
1490+
facets = [C00.product(f._translate(zero))
1491+
for f in self.maximal_cells()]
1492+
C11 = Cube([[1, 1]])
1493+
facets.extend(C11.product(f._translate(zero))
1494+
for f in other.maximal_cells())
14981495
return CubicalComplex(facets)
14991496

15001497
def wedge(self, other):
@@ -1528,11 +1525,9 @@ def wedge(self, other):
15281525
translate_right = [-a[0] for a in other.maximal_cells()[0]]
15291526
point_right = Cube([[0, 0]] * embedded_left)
15301527

1531-
facets = []
1532-
for f in self.maximal_cells():
1533-
facets.append(f._translate(translate_left))
1534-
for f in other.maximal_cells():
1535-
facets.append(point_right.product(f._translate(translate_right)))
1528+
facets = [f._translate(translate_left) for f in self.maximal_cells()]
1529+
facets.extend(point_right.product(f._translate(translate_right))
1530+
for f in other.maximal_cells())
15361531
return CubicalComplex(facets)
15371532

15381533
def connected_sum(self, other):
@@ -1794,7 +1789,7 @@ def _string_constants(self):
17941789
return ('Cubical', 'cube', 'cubes')
17951790

17961791

1797-
class CubicalComplexExamples():
1792+
class CubicalComplexExamples:
17981793
r"""
17991794
Some examples of cubical complexes.
18001795

src/sage/topology/delta_complex.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def store_bdry(simplex, faces):
367367
for j in range(d+1):
368368
if not all(faces[s[j]][i] == faces[s[i]][j-1] for i in range(j)):
369369
msg = "simplicial identity d_i d_j = d_{j-1} d_i fails"
370-
msg += " for j=%s, in dimension %s" % (j, d)
370+
msg += " for j={}, in dimension {}".format(j, d)
371371
raise ValueError(msg)
372372
# self._cells_dict: dictionary indexed by dimension d: for
373373
# each d, have list or tuple of simplices, and for each
@@ -453,7 +453,7 @@ def subcomplex(self, data):
453453
try:
454454
cells_to_add = set(new_data[d-1]) # begin to populate the (d-1)-cells
455455
except KeyError:
456-
cells_to_add = set([])
456+
cells_to_add = set()
457457
for x in d_cells:
458458
if d+1 in new_dict:
459459
old = new_dict[d+1]
@@ -757,11 +757,8 @@ def n_skeleton(self, n):
757757
"""
758758
if n >= self.dimension():
759759
return self
760-
else:
761-
data = []
762-
for d in range(n+1):
763-
data.append(self._cells_dict[d])
764-
return DeltaComplex(data)
760+
data = [self._cells_dict[d] for d in range(n + 1)]
761+
return DeltaComplex(data)
765762

766763
def graph(self):
767764
r"""
@@ -1106,16 +1103,12 @@ def wedge(self, right):
11061103
"""
11071104
data = self.disjoint_union(right).cells()
11081105
left_verts = len(self.n_cells(0))
1109-
translate = {}
1110-
for i in range(left_verts):
1111-
translate[i] = i
1106+
translate = {i: i for i in range(left_verts)}
11121107
translate[left_verts] = 0
11131108
for i in range(left_verts + 1, left_verts + len(right.n_cells(0))):
1114-
translate[i] = i-1
1109+
translate[i] = i - 1
11151110
data[0] = data[0][:-1]
1116-
edges = []
1117-
for e in data[1]:
1118-
edges.append([translate[a] for a in e])
1111+
edges = [[translate[a] for a in e] for e in data[1]]
11191112
data[1] = edges
11201113
return DeltaComplex(data)
11211114

@@ -1444,7 +1437,7 @@ def _is_glued(self, idx=-1, dim=None):
14441437
while not_glued and i > 0:
14451438
# count the (i-1) cells and compare to (n+1) choose i.
14461439
old_faces = i_faces
1447-
i_faces = set([])
1440+
i_faces = set()
14481441
all_cells = self.n_cells(i)
14491442
for face in old_faces:
14501443
i_faces.update(all_cells[face])
@@ -1472,7 +1465,7 @@ def face_poset(self):
14721465
for n in range(dim, 0, -1):
14731466
idx = 0
14741467
for s in self.n_cells(n):
1475-
covers[(n, idx)] = list(set([(n-1, i) for i in s]))
1468+
covers[(n, idx)] = list({(n-1, i) for i in s})
14761469
idx += 1
14771470
# deal with vertices separately: they have no covers (in the
14781471
# dual poset).
@@ -1622,7 +1615,7 @@ def _string_constants(self):
16221615
return ('Delta', 'simplex', 'simplices')
16231616

16241617

1625-
class DeltaComplexExamples():
1618+
class DeltaComplexExamples:
16261619
r"""
16271620
Some examples of `\Delta`-complexes.
16281621

src/sage/topology/moment_angle_complex.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,8 @@ def _cubical_complex_union(c1, c2):
6363
sage: union(C1, C1) == C1
6464
True
6565
"""
66-
facets = []
67-
for f in c1.maximal_cells():
68-
facets.append(f)
69-
for f in c2.maximal_cells():
70-
facets.append(f)
66+
facets = list(c1.maximal_cells())
67+
facets.extend(c2.maximal_cells())
7168
return CubicalComplex(facets)
7269

7370

src/sage/topology/simplicial_complex.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,8 +1343,8 @@ def faces(self, subcomplex=None):
13431343
sub_facets = {}
13441344
dimension = max([face.dimension() for face in self._facets])
13451345
for i in range(-1, dimension + 1):
1346-
Faces[i] = set([])
1347-
sub_facets[i] = set([])
1346+
Faces[i] = set()
1347+
sub_facets[i] = set()
13481348
for f in self._facets:
13491349
dim = f.dimension()
13501350
Faces[dim].add(f)
@@ -2085,7 +2085,7 @@ def wedge(self, right, rename_vertices=True, is_mutable=True):
20852085
def chain_complex(self, subcomplex=None, augmented=False,
20862086
verbose=False, check=False, dimensions=None,
20872087
base_ring=ZZ, cochain=False):
2088-
"""
2088+
r"""
20892089
The chain complex associated to this simplicial complex.
20902090
20912091
:param dimensions: if ``None``, compute the chain complex in all
@@ -2187,7 +2187,7 @@ def chain_complex(self, subcomplex=None, augmented=False,
21872187
differentials[n] = self._complex[(n, subcomplex)].change_ring(base_ring)
21882188
mat = differentials[n]
21892189
if verbose:
2190-
print(" boundary matrix (cached): it's %s by %s." % (mat.nrows(), mat.ncols()))
2190+
print(" boundary matrix (cached): it's {} by {}.".format(mat.nrows(), mat.ncols()))
21912191
else:
21922192
# 'current' is the list of faces in dimension n
21932193
#
@@ -2226,7 +2226,7 @@ def chain_complex(self, subcomplex=None, augmented=False,
22262226
self._complex[(n, subcomplex)] = mat
22272227
differentials[n] = mat.change_ring(base_ring)
22282228
if verbose:
2229-
print(" boundary matrix computed: it's %s by %s." % (mat.nrows(), mat.ncols()))
2229+
print(" boundary matrix computed: it's {} by {}.".format(mat.nrows(), mat.ncols()))
22302230
# now for the cochain complex, compute the last dimension by
22312231
# hand, and don't cache it.
22322232
if cochain:
@@ -2942,8 +2942,8 @@ def connected_sum(self, other, is_mutable=True):
29422942
keep_left = self._facets[0]
29432943
keep_right = other._facets[0]
29442944
# construct the set of facets:
2945-
left = set(self._facets).difference(set([keep_left]))
2946-
right = set(other._facets).difference(set([keep_right]))
2945+
left = set(self._facets).difference({keep_left})
2946+
right = set(other._facets).difference({keep_right})
29472947
facet_set = ([[rename_vertex(v, keep=list(keep_left))
29482948
for v in face] for face in left]
29492949
+ [[rename_vertex(v, keep=list(keep_right), left=False)
@@ -3067,7 +3067,7 @@ def is_cohen_macaulay(self, base_ring=QQ, ncpus=0):
30673067
from sage.parallel.ncpus import ncpus as get_ncpus
30683068
ncpus = get_ncpus()
30693069

3070-
facs = [x for x in self.face_iterator()]
3070+
facs = list(self.face_iterator())
30713071
n = len(facs)
30723072
facs_divided = [[] for i in range(ncpus)]
30733073
for i in range(n):
@@ -3957,7 +3957,7 @@ def _enlarge_subcomplex(self, subcomplex, verbose=False):
39573957
print(f" looping through {len(faces)} facets")
39583958
for f in faces:
39593959
f_set = f.set()
3960-
int_facets = set(a.set().intersection(f_set) for a in new_facets)
3960+
int_facets = {a.set().intersection(f_set) for a in new_facets}
39613961
intersection = SimplicialComplex(int_facets)
39623962
if not intersection._facets[0].is_empty():
39633963
if (len(intersection._facets) == 1 or
@@ -3970,7 +3970,7 @@ def _enlarge_subcomplex(self, subcomplex, verbose=False):
39703970
for f in remove_these:
39713971
faces.remove(f)
39723972
if verbose:
3973-
print(" now constructing a simplicial complex with %s vertices and %s facets" % (len(self.vertices()), len(new_facets)))
3973+
print(" now constructing a simplicial complex with {} vertices and {} facets".format(len(self.vertices()), len(new_facets)))
39743974
L = SimplicialComplex(new_facets, maximality_check=False,
39753975
is_immutable=self._is_immutable)
39763976
self.__enlarged[subcomplex] = L
@@ -4181,7 +4181,7 @@ def fundamental_group(self, base_point=None, simplify=True):
41814181
# simplicial complex, so convert the edges to frozensets so we
41824182
# don't have to worry about it. Convert spanning_tree to a set
41834183
# to make lookup faster.
4184-
spanning_tree = set(frozenset((u, v)) for u, v, _ in G.min_spanning_tree())
4184+
spanning_tree = {frozenset((u, v)) for u, v, _ in G.min_spanning_tree()}
41854185
gens = [e for e in G.edge_iterator(labels=False)
41864186
if frozenset(e) not in spanning_tree]
41874187
if not gens:
@@ -4192,7 +4192,7 @@ def fundamental_group(self, base_point=None, simplify=True):
41924192
rels = []
41934193
for f in self._n_cells_sorted(2):
41944194
bdry = [tuple(e) for e in f.faces()]
4195-
z = dict()
4195+
z = {}
41964196
for i in range(3):
41974197
x = frozenset(bdry[i])
41984198
if x in spanning_tree:
@@ -5049,7 +5049,7 @@ def is_golod(self) -> bool:
50495049
sage: Y.is_golod()
50505050
True
50515051
"""
5052-
H = list(a+b for (a, b) in self.bigraded_betti_numbers())
5052+
H = [a+b for (a, b) in self.bigraded_betti_numbers()]
50535053
if 0 in H:
50545054
H.remove(0)
50555055

src/sage/topology/simplicial_complex_examples.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def matching(A, B):
181181
"""
182182
answer = []
183183
if len(A) == 0 or len(B) == 0:
184-
return [set([])]
184+
return [set()]
185185
for v in A:
186186
for w in B:
187187
for M in matching(set(A).difference([v]), set(B).difference([w])):
@@ -476,7 +476,7 @@ def SurfaceOfGenus(g, orientable=True):
476476

477477

478478
def MooreSpace(q):
479-
"""
479+
r"""
480480
Triangulation of the mod `q` Moore space.
481481
482482
INPUT:
@@ -842,7 +842,7 @@ def RealProjectiveSpace(n):
842842
V = set(range(0, n+2))
843843
S = Sphere(n).barycentric_subdivision()
844844
X = S.facets()
845-
facets = set([])
845+
facets = set()
846846
for f in X:
847847
new = []
848848
for v in f:
@@ -1485,7 +1485,7 @@ def ShiftedComplex(generators):
14851485
from sage.combinat.partition import Partitions
14861486
Facets = []
14871487
for G in generators:
1488-
G = list(reversed(sorted(G)))
1488+
G = sorted(G, reverse=True)
14891489
L = len(G)
14901490
for k in range(L * (L+1) // 2, sum(G) + 1):
14911491
for P in Partitions(k, length=L, max_slope=-1, outer=G):

0 commit comments

Comments
 (0)