Skip to content

Commit

Permalink
sagemathgh-37017: cosmetic little change in topology/ (some ruff UP a…
Browse files Browse the repository at this point in the history
…nd 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: sagemath#37017
Reported by: Frédéric Chapoton
Reviewer(s): Frédéric Chapoton, Tobias Diez
  • Loading branch information
Release Manager committed Jan 16, 2024
2 parents 90ed189 + edcd5b8 commit 63fbac6
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 93 deletions.
2 changes: 1 addition & 1 deletion src/sage/topology/cell_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ def _repr_(self):
for dim in self.cells():
cells += len(self.cells()[dim])
if cells != 1:
cells_string = " and %s %s" % (cells, cells_name)
cells_string = " and {} {}".format(cells, cells_name)
else:
cells_string = " and 1 %s" % cell_name
return Name + " complex " + vertex_string + cells_string
49 changes: 22 additions & 27 deletions src/sage/topology/cubical_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,13 +545,12 @@ def _triangulation_(self):
if self.dimension() < 0: # the empty cube
return [Simplex(())] # the empty simplex
v = tuple([max(j) for j in self.tuple()])
Sv = Simplex((v,))
if self.dimension() == 0: # just v
return [Simplex((v,))]
simplices = []
for i in range(self.dimension()):
for S in self.face(i, upper=False)._triangulation_():
simplices.append(S.join(Simplex((v,)), rename_vertices=False))
return simplices
return [Sv]
return [S.join(Sv, rename_vertices=False)
for i in range(self.dimension())
for S in self.face(i, upper=False)._triangulation_()]

def alexander_whitney(self, dim):
r"""
Expand Down Expand Up @@ -719,7 +718,7 @@ def _repr_(self):
sage: C1._repr_()
'[1,1] x [2,3] x [4,5]'
"""
s = ("[%s,%s]" % (str(x), str(y)) for x, y in self.__tuple)
s = ("[{},{}]".format(str(x), str(y)) for x, y in self.__tuple)
return " x ".join(s)

def _latex_(self):
Expand Down Expand Up @@ -1077,8 +1076,8 @@ def cells(self, subcomplex=None):
dimension = max([cube.dimension() for cube in self._facets])
# initialize the lists: add each maximal cube to Cells and sub_facets
for i in range(-1, dimension+1):
Cells[i] = set([])
sub_facets[i] = set([])
Cells[i] = set()
sub_facets[i] = set()
for f in self._facets:
Cells[f.dimension()].add(f)
if subcomplex is not None:
Expand Down Expand Up @@ -1229,7 +1228,7 @@ def chain_complex(self, subcomplex=None, augmented=False,
differentials[dim] = self._complex[(dim, subcomplex)].change_ring(base_ring)
mat = differentials[dim]
if verbose:
print(" boundary matrix (cached): it's %s by %s." % (mat.nrows(), mat.ncols()))
print(" boundary matrix (cached): it's {} by {}.".format(mat.nrows(), mat.ncols()))
else:
# 'current' is the list of cells in dimension n
#
Expand Down Expand Up @@ -1271,7 +1270,7 @@ def chain_complex(self, subcomplex=None, augmented=False,
else:
differentials[dim] = mat.change_ring(base_ring)
if verbose:
print(" boundary matrix computed: it's %s by %s." % (mat.nrows(), mat.ncols()))
print(" boundary matrix computed: it's {} by {}.".format(mat.nrows(), mat.ncols()))
# finally, return the chain complex
if cochain:
return ChainComplex(data=differentials, base_ring=base_ring,
Expand Down Expand Up @@ -1452,7 +1451,7 @@ def suspension(self, n=1):

def product(self, other):
r"""
The product of this cubical complex with another one.
Return the product of this cubical complex with another one.
:param other: another cubical complex
Expand All @@ -1463,10 +1462,7 @@ def product(self, other):
sage: RP2.product(S1).homology()[1] # long time: 5 seconds
Z x C2
"""
facets = []
for f in self._facets:
for g in other._facets:
facets.append(f.product(g))
facets = [f.product(g) for f in self._facets for g in other._facets]
return CubicalComplex(facets)

def disjoint_union(self, other):
Expand All @@ -1490,11 +1486,12 @@ def disjoint_union(self, other):
embedded_left = len(tuple(self.maximal_cells()[0]))
embedded_right = len(tuple(other.maximal_cells()[0]))
zero = [0] * max(embedded_left, embedded_right)
facets = []
for f in self.maximal_cells():
facets.append(Cube([[0, 0]]).product(f._translate(zero)))
for f in other.maximal_cells():
facets.append(Cube([[1, 1]]).product(f._translate(zero)))
C00 = Cube([[0, 0]])
facets = [C00.product(f._translate(zero))
for f in self.maximal_cells()]
C11 = Cube([[1, 1]])
facets.extend(C11.product(f._translate(zero))
for f in other.maximal_cells())
return CubicalComplex(facets)

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

facets = []
for f in self.maximal_cells():
facets.append(f._translate(translate_left))
for f in other.maximal_cells():
facets.append(point_right.product(f._translate(translate_right)))
facets = [f._translate(translate_left) for f in self.maximal_cells()]
facets.extend(point_right.product(f._translate(translate_right))
for f in other.maximal_cells())
return CubicalComplex(facets)

def connected_sum(self, other):
Expand Down Expand Up @@ -1794,7 +1789,7 @@ def _string_constants(self):
return ('Cubical', 'cube', 'cubes')


class CubicalComplexExamples():
class CubicalComplexExamples:
r"""
Some examples of cubical complexes.
Expand Down
27 changes: 10 additions & 17 deletions src/sage/topology/delta_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def store_bdry(simplex, faces):
for j in range(d+1):
if not all(faces[s[j]][i] == faces[s[i]][j-1] for i in range(j)):
msg = "simplicial identity d_i d_j = d_{j-1} d_i fails"
msg += " for j=%s, in dimension %s" % (j, d)
msg += " for j={}, in dimension {}".format(j, d)
raise ValueError(msg)
# self._cells_dict: dictionary indexed by dimension d: for
# each d, have list or tuple of simplices, and for each
Expand Down Expand Up @@ -453,7 +453,7 @@ def subcomplex(self, data):
try:
cells_to_add = set(new_data[d-1]) # begin to populate the (d-1)-cells
except KeyError:
cells_to_add = set([])
cells_to_add = set()
for x in d_cells:
if d+1 in new_dict:
old = new_dict[d+1]
Expand Down Expand Up @@ -757,11 +757,8 @@ def n_skeleton(self, n):
"""
if n >= self.dimension():
return self
else:
data = []
for d in range(n+1):
data.append(self._cells_dict[d])
return DeltaComplex(data)
data = [self._cells_dict[d] for d in range(n + 1)]
return DeltaComplex(data)

def graph(self):
r"""
Expand Down Expand Up @@ -1106,16 +1103,12 @@ def wedge(self, right):
"""
data = self.disjoint_union(right).cells()
left_verts = len(self.n_cells(0))
translate = {}
for i in range(left_verts):
translate[i] = i
translate = {i: i for i in range(left_verts)}
translate[left_verts] = 0
for i in range(left_verts + 1, left_verts + len(right.n_cells(0))):
translate[i] = i-1
translate[i] = i - 1
data[0] = data[0][:-1]
edges = []
for e in data[1]:
edges.append([translate[a] for a in e])
edges = [[translate[a] for a in e] for e in data[1]]
data[1] = edges
return DeltaComplex(data)

Expand Down Expand Up @@ -1444,7 +1437,7 @@ def _is_glued(self, idx=-1, dim=None):
while not_glued and i > 0:
# count the (i-1) cells and compare to (n+1) choose i.
old_faces = i_faces
i_faces = set([])
i_faces = set()
all_cells = self.n_cells(i)
for face in old_faces:
i_faces.update(all_cells[face])
Expand Down Expand Up @@ -1472,7 +1465,7 @@ def face_poset(self):
for n in range(dim, 0, -1):
idx = 0
for s in self.n_cells(n):
covers[(n, idx)] = list(set([(n-1, i) for i in s]))
covers[(n, idx)] = list({(n-1, i) for i in s})
idx += 1
# deal with vertices separately: they have no covers (in the
# dual poset).
Expand Down Expand Up @@ -1622,7 +1615,7 @@ def _string_constants(self):
return ('Delta', 'simplex', 'simplices')


class DeltaComplexExamples():
class DeltaComplexExamples:
r"""
Some examples of `\Delta`-complexes.
Expand Down
7 changes: 2 additions & 5 deletions src/sage/topology/moment_angle_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,8 @@ def _cubical_complex_union(c1, c2):
sage: union(C1, C1) == C1
True
"""
facets = []
for f in c1.maximal_cells():
facets.append(f)
for f in c2.maximal_cells():
facets.append(f)
facets = list(c1.maximal_cells())
facets.extend(c2.maximal_cells())
return CubicalComplex(facets)


Expand Down
26 changes: 13 additions & 13 deletions src/sage/topology/simplicial_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1343,8 +1343,8 @@ def faces(self, subcomplex=None):
sub_facets = {}
dimension = max([face.dimension() for face in self._facets])
for i in range(-1, dimension + 1):
Faces[i] = set([])
sub_facets[i] = set([])
Faces[i] = set()
sub_facets[i] = set()
for f in self._facets:
dim = f.dimension()
Faces[dim].add(f)
Expand Down Expand Up @@ -2085,7 +2085,7 @@ def wedge(self, right, rename_vertices=True, is_mutable=True):
def chain_complex(self, subcomplex=None, augmented=False,
verbose=False, check=False, dimensions=None,
base_ring=ZZ, cochain=False):
"""
r"""
The chain complex associated to this simplicial complex.
:param dimensions: if ``None``, compute the chain complex in all
Expand Down Expand Up @@ -2187,7 +2187,7 @@ def chain_complex(self, subcomplex=None, augmented=False,
differentials[n] = self._complex[(n, subcomplex)].change_ring(base_ring)
mat = differentials[n]
if verbose:
print(" boundary matrix (cached): it's %s by %s." % (mat.nrows(), mat.ncols()))
print(" boundary matrix (cached): it's {} by {}.".format(mat.nrows(), mat.ncols()))
else:
# 'current' is the list of faces in dimension n
#
Expand Down Expand Up @@ -2226,7 +2226,7 @@ def chain_complex(self, subcomplex=None, augmented=False,
self._complex[(n, subcomplex)] = mat
differentials[n] = mat.change_ring(base_ring)
if verbose:
print(" boundary matrix computed: it's %s by %s." % (mat.nrows(), mat.ncols()))
print(" boundary matrix computed: it's {} by {}.".format(mat.nrows(), mat.ncols()))
# now for the cochain complex, compute the last dimension by
# hand, and don't cache it.
if cochain:
Expand Down Expand Up @@ -2942,8 +2942,8 @@ def connected_sum(self, other, is_mutable=True):
keep_left = self._facets[0]
keep_right = other._facets[0]
# construct the set of facets:
left = set(self._facets).difference(set([keep_left]))
right = set(other._facets).difference(set([keep_right]))
left = set(self._facets).difference({keep_left})
right = set(other._facets).difference({keep_right})
facet_set = ([[rename_vertex(v, keep=list(keep_left))
for v in face] for face in left]
+ [[rename_vertex(v, keep=list(keep_right), left=False)
Expand Down Expand Up @@ -3067,7 +3067,7 @@ def is_cohen_macaulay(self, base_ring=QQ, ncpus=0):
from sage.parallel.ncpus import ncpus as get_ncpus
ncpus = get_ncpus()

facs = [x for x in self.face_iterator()]
facs = list(self.face_iterator())
n = len(facs)
facs_divided = [[] for i in range(ncpus)]
for i in range(n):
Expand Down Expand Up @@ -3957,7 +3957,7 @@ def _enlarge_subcomplex(self, subcomplex, verbose=False):
print(f" looping through {len(faces)} facets")
for f in faces:
f_set = f.set()
int_facets = set(a.set().intersection(f_set) for a in new_facets)
int_facets = {a.set().intersection(f_set) for a in new_facets}
intersection = SimplicialComplex(int_facets)
if not intersection._facets[0].is_empty():
if (len(intersection._facets) == 1 or
Expand All @@ -3970,7 +3970,7 @@ def _enlarge_subcomplex(self, subcomplex, verbose=False):
for f in remove_these:
faces.remove(f)
if verbose:
print(" now constructing a simplicial complex with %s vertices and %s facets" % (len(self.vertices()), len(new_facets)))
print(" now constructing a simplicial complex with {} vertices and {} facets".format(len(self.vertices()), len(new_facets)))
L = SimplicialComplex(new_facets, maximality_check=False,
is_immutable=self._is_immutable)
self.__enlarged[subcomplex] = L
Expand Down Expand Up @@ -4181,7 +4181,7 @@ def fundamental_group(self, base_point=None, simplify=True):
# simplicial complex, so convert the edges to frozensets so we
# don't have to worry about it. Convert spanning_tree to a set
# to make lookup faster.
spanning_tree = set(frozenset((u, v)) for u, v, _ in G.min_spanning_tree())
spanning_tree = {frozenset((u, v)) for u, v, _ in G.min_spanning_tree()}
gens = [e for e in G.edge_iterator(labels=False)
if frozenset(e) not in spanning_tree]
if not gens:
Expand All @@ -4192,7 +4192,7 @@ def fundamental_group(self, base_point=None, simplify=True):
rels = []
for f in self._n_cells_sorted(2):
bdry = [tuple(e) for e in f.faces()]
z = dict()
z = {}
for i in range(3):
x = frozenset(bdry[i])
if x in spanning_tree:
Expand Down Expand Up @@ -5049,7 +5049,7 @@ def is_golod(self) -> bool:
sage: Y.is_golod()
True
"""
H = list(a+b for (a, b) in self.bigraded_betti_numbers())
H = [a+b for (a, b) in self.bigraded_betti_numbers()]
if 0 in H:
H.remove(0)

Expand Down
8 changes: 4 additions & 4 deletions src/sage/topology/simplicial_complex_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def matching(A, B):
"""
answer = []
if len(A) == 0 or len(B) == 0:
return [set([])]
return [set()]
for v in A:
for w in B:
for M in matching(set(A).difference([v]), set(B).difference([w])):
Expand Down Expand Up @@ -476,7 +476,7 @@ def SurfaceOfGenus(g, orientable=True):


def MooreSpace(q):
"""
r"""
Triangulation of the mod `q` Moore space.
INPUT:
Expand Down Expand Up @@ -842,7 +842,7 @@ def RealProjectiveSpace(n):
V = set(range(0, n+2))
S = Sphere(n).barycentric_subdivision()
X = S.facets()
facets = set([])
facets = set()
for f in X:
new = []
for v in f:
Expand Down Expand Up @@ -1485,7 +1485,7 @@ def ShiftedComplex(generators):
from sage.combinat.partition import Partitions
Facets = []
for G in generators:
G = list(reversed(sorted(G)))
G = sorted(G, reverse=True)
L = len(G)
for k in range(L * (L+1) // 2, sum(G) + 1):
for P in Partitions(k, length=L, max_slope=-1, outer=G):
Expand Down
Loading

0 comments on commit 63fbac6

Please sign in to comment.