@@ -290,10 +290,8 @@ def store_bdry(simplex, faces):
290
290
pass
291
291
else :
292
292
if isinstance (data , (list , tuple )):
293
- dim = 0
294
- for s in data :
293
+ for dim , s in enumerate (data ):
295
294
new_data [dim ] = s
296
- dim += 1
297
295
elif isinstance (data , dict ):
298
296
if all (isinstance (a , (int , Integer )) for a in data ):
299
297
# a dictionary indexed by integers
@@ -316,10 +314,7 @@ def store_bdry(simplex, faces):
316
314
new_delayed = {}
317
315
current = {}
318
316
for x in old_data_by_dim [dim ]:
319
- if x in data :
320
- bdry = data [x ]
321
- else :
322
- bdry = True
317
+ bdry = data .get (x , True )
323
318
if isinstance (bdry , Simplex ):
324
319
# case 1
325
320
# value is a simplex, so x is glued to the old
@@ -353,7 +348,7 @@ def store_bdry(simplex, faces):
353
348
current [x ] = store_bdry (x , x .faces ())
354
349
old_delayed = new_delayed
355
350
if dim > 0 :
356
- old_data_by_dim [dim - 1 ].extend (old_delayed . keys () )
351
+ old_data_by_dim [dim - 1 ].extend (old_delayed )
357
352
else :
358
353
raise ValueError ("data is not a list, tuple, or dictionary" )
359
354
for n in new_data :
@@ -435,7 +430,7 @@ def subcomplex(self, data):
435
430
# in self which are not in the subcomplex.
436
431
new_data = {}
437
432
# max_dim: maximum dimension of cells being added
438
- max_dim = max (data . keys () )
433
+ max_dim = max (data )
439
434
# cells_to_add: in each dimension, add these cells to
440
435
# new_dict. start with the cells given in new_data and add
441
436
# faces of cells one dimension higher.
@@ -471,7 +466,7 @@ def subcomplex(self, data):
471
466
sub ._is_subcomplex_of = {self : new_data }
472
467
return sub
473
468
474
- def __hash__ (self ):
469
+ def __hash__ (self ) -> int :
475
470
r"""
476
471
TESTS::
477
472
@@ -482,7 +477,7 @@ def __hash__(self):
482
477
"""
483
478
return hash (frozenset (self ._cells_dict .items ()))
484
479
485
- def __eq__ (self , right ):
480
+ def __eq__ (self , right ) -> bool :
486
481
r"""
487
482
Two `\Delta`-complexes are equal, according to this, if they have
488
483
the same ``_cells_dict``.
@@ -499,7 +494,7 @@ def __eq__(self, right):
499
494
"""
500
495
return self ._cells_dict == right ._cells_dict
501
496
502
- def __ne__ (self , other ):
497
+ def __ne__ (self , other ) -> bool :
503
498
r"""
504
499
Return ``True`` if ``self`` and ``other`` are not equal.
505
500
@@ -556,15 +551,15 @@ def cells(self, subcomplex=None):
556
551
return cells
557
552
if subcomplex ._is_subcomplex_of is None or self not in subcomplex ._is_subcomplex_of :
558
553
if subcomplex == self :
559
- for d in range (- 1 , max (cells . keys ()) + 1 ):
554
+ for d in range (- 1 , max (cells ) + 1 ):
560
555
l = len (cells [d ])
561
- cells [d ] = [None ]* l # get rid of all cells
556
+ cells [d ] = [None ] * l # get rid of all cells
562
557
return cells
563
558
else :
564
559
raise ValueError ("this is not a subcomplex of self" )
565
560
else :
566
561
subcomplex_cells = subcomplex ._is_subcomplex_of [self ]
567
- for d in range (max (subcomplex_cells . keys () ) + 1 ):
562
+ for d in range (max (subcomplex_cells ) + 1 ):
568
563
L = list (cells [d ])
569
564
for c in subcomplex_cells [d ]:
570
565
L [c ] = None
@@ -638,33 +633,30 @@ def chain_complex(self, subcomplex=None, augmented=False,
638
633
augmented = False
639
634
640
635
differentials = {}
641
- if augmented :
642
- empty_simplex = 1 # number of (-1)-dimensional simplices
643
- else :
644
- empty_simplex = 0
636
+ # number of (-1)-dimensional simplices
637
+ empty_simplex = 1 if augmented else 0
638
+
645
639
vertices = self .n_cells (0 , subcomplex = subcomplex )
646
640
old = vertices
647
641
old_real = [x for x in old if x is not None ] # remove faces not in subcomplex
648
642
n = len (old_real )
649
- differentials [0 ] = matrix (base_ring , empty_simplex , n , n * empty_simplex * [1 ])
643
+ differentials [0 ] = matrix (base_ring , empty_simplex , n ,
644
+ n * empty_simplex * [1 ])
650
645
# current is list of simplices in dimension dim
651
646
# current_real is list of simplices in dimension dim, with None filtered out
652
647
# old is list of simplices in dimension dim-1
653
648
# old_real is list of simplices in dimension dim-1, with None filtered out
654
- for dim in range (1 , self .dimension ()+ 1 ):
649
+ for dim in range (1 , self .dimension () + 1 ):
655
650
current = list (self .n_cells (dim , subcomplex = subcomplex ))
656
651
current_real = [x for x in current if x is not None ]
657
- i = 0
658
652
i_real = 0
659
653
translate = {}
660
- for s in old :
654
+ for i , s in enumerate ( old ) :
661
655
if s is not None :
662
656
translate [i ] = i_real
663
657
i_real += 1
664
- i += 1
665
658
mat_dict = {}
666
- col = 0
667
- for s in current_real :
659
+ for col , s in enumerate (current_real ):
668
660
sign = 1
669
661
for row in s :
670
662
if old [row ] is not None :
@@ -674,14 +666,13 @@ def chain_complex(self, subcomplex=None, augmented=False,
674
666
else :
675
667
mat_dict [(actual_row , col )] = sign
676
668
sign *= - 1
677
- col += 1
678
669
differentials [dim ] = matrix (base_ring , len (old_real ), len (current_real ), mat_dict )
679
670
old = current
680
671
old_real = current_real
681
672
if cochain :
682
673
cochain_diffs = {}
683
674
for dim in differentials :
684
- cochain_diffs [dim - 1 ] = differentials [dim ].transpose ()
675
+ cochain_diffs [dim - 1 ] = differentials [dim ].transpose ()
685
676
return ChainComplex (data = cochain_diffs , degree = 1 ,
686
677
base_ring = base_ring , check = check )
687
678
else :
@@ -975,30 +966,24 @@ def product(self, other):
975
966
# vertices: the vertices in the product are of the form (v,w)
976
967
# for v a vertex in self, w a vertex in other
977
968
vertices = []
978
- l_idx = 0
979
- for v in self .n_cells (0 ):
980
- r_idx = 0
981
- for w in other .n_cells (0 ):
969
+ for l_idx , v in enumerate (self .n_cells (0 )):
970
+ for r_idx , w in enumerate (other .n_cells (0 )):
982
971
# one vertex for each pair (v,w)
983
972
# store its indices in bdries; store its boundary in vertices
984
973
bdries [(0 , l_idx , 0 , r_idx , ((0 , 0 ),))] = len (vertices )
985
974
vertices .append (()) # add new vertex (simplex with empty bdry)
986
- r_idx += 1
987
- l_idx += 1
988
975
data .append (tuple (vertices ))
989
976
# dim of the product:
990
977
maxdim = self .dimension () + other .dimension ()
991
978
# d-cells, d>0: these are obtained by taking products of cells
992
979
# of dimensions k and n, where n+k >= d and n <= d, k <= d.
993
980
simplices = []
994
981
new = {}
995
- for d in range (1 , maxdim + 1 ):
996
- for k in range (d + 1 ):
997
- for n in range (d - k , d + 1 ):
998
- k_idx = 0
999
- for k_cell in self .n_cells (k ):
1000
- n_idx = 0
1001
- for n_cell in other .n_cells (n ):
982
+ for d in range (1 , maxdim + 1 ):
983
+ for k in range (d + 1 ):
984
+ for n in range (d - k , d + 1 ):
985
+ for k_idx , k_cell in enumerate (self .n_cells (k )):
986
+ for n_idx , n_cell in enumerate (other .n_cells (n )):
1002
987
# find d-dimensional faces in product of
1003
988
# k_cell and n_cell. to avoid repetition,
1004
989
# only look for faces which use all
@@ -1049,8 +1034,6 @@ def product(self, other):
1049
1034
n_face_dim , n_face_idx ,
1050
1035
face_path )])
1051
1036
simplices .append (tuple (bdry_list ))
1052
- n_idx += 1
1053
- k_idx += 1
1054
1037
# add d-simplices to data, store d-simplices in bdries,
1055
1038
# reset simplices
1056
1039
data .append (tuple (simplices ))
@@ -1081,9 +1064,9 @@ def disjoint_union(self, right):
1081
1064
# len(self.n_cells(n-1)) to it
1082
1065
for n in range (dim , 0 , - 1 ):
1083
1066
data [n ] = list (self .n_cells (n ))
1084
- translate = len (self .n_cells (n - 1 ))
1067
+ translate = len (self .n_cells (n - 1 ))
1085
1068
for f in right .n_cells (n ):
1086
- data [n ].append (tuple ([a + translate for a in f ]))
1069
+ data [n ].append (tuple ([a + translate for a in f ]))
1087
1070
data [0 ] = self .n_cells (0 ) + right .n_cells (0 )
1088
1071
return DeltaComplex (data )
1089
1072
@@ -1194,23 +1177,22 @@ def connected_sum(self, other):
1194
1177
glued = copy (renaming )
1195
1178
# process_later: cells one dim lower to be added to data
1196
1179
process_later = []
1197
- old_idx = 0
1198
- new_idx = len (data [n - 1 ])
1180
+ new_idx = len (data [n - 1 ])
1199
1181
# build 'renaming'
1200
- for s in right_cells [n - 1 ] :
1182
+ for old_idx , s in enumerate ( right_cells [n - 1 ]) :
1201
1183
if old_idx not in renaming :
1202
1184
process_later .append (s )
1203
1185
renaming [old_idx ] = new_idx
1204
1186
new_idx += 1
1205
- old_idx += 1
1206
1187
# reindex all simplices to be processed and add them to data
1207
1188
for s in process_now :
1208
1189
data [n ].append (tuple ([renaming [i ] for i in s ]))
1209
1190
# set up for next loop, one dimension down
1210
1191
renaming = {}
1211
1192
process_now = process_later
1212
1193
for f in glued :
1213
- renaming .update (dict (zip (right_cells [n - 1 ][f ], data [n - 1 ][glued [f ]])))
1194
+ renaming .update (dict (zip (right_cells [n - 1 ][f ],
1195
+ data [n - 1 ][glued [f ]])))
1214
1196
# deal with vertices separately. we just need to add enough
1215
1197
# vertices: all the vertices from Right, minus the number
1216
1198
# being glued, which should be dim+1, the number of vertices
@@ -1302,7 +1284,7 @@ def elementary_subdivision(self, idx=-1):
1302
1284
cells_dict [0 ].append (())
1303
1285
# added_cells: dict indexed by (n-1)-cells, with value the
1304
1286
# corresponding new n-cell.
1305
- added_cells = {(): len (cells_dict [0 ])- 1 }
1287
+ added_cells = {(): len (cells_dict [0 ]) - 1 }
1306
1288
for n in range (dim ):
1307
1289
new_cells = {}
1308
1290
# for each n-cell in the standard simplex, add an
@@ -1316,15 +1298,15 @@ def elementary_subdivision(self, idx=-1):
1316
1298
cell = []
1317
1299
for i in simplex :
1318
1300
if n > 0 :
1319
- bdry = tuple (std_cells [n - 1 ][i ])
1301
+ bdry = tuple (std_cells [n - 1 ][i ])
1320
1302
else :
1321
1303
bdry = ()
1322
1304
cell .append (added_cells [bdry ])
1323
1305
# last face is the image of the old simplex)
1324
1306
cell .append (pi [n ][simplex ])
1325
1307
cell = tuple (cell )
1326
- cells_dict [n + 1 ].append (cell )
1327
- new_cells [simplex ] = len (cells_dict [n + 1 ])- 1
1308
+ cells_dict [n + 1 ].append (cell )
1309
+ new_cells [simplex ] = len (cells_dict [n + 1 ]) - 1
1328
1310
added_cells = new_cells
1329
1311
return DeltaComplex (cells_dict )
1330
1312
@@ -1397,7 +1379,8 @@ def _epi_from_standard_simplex(self, idx=-1, dim=None):
1397
1379
faces_dict = {}
1398
1380
for cell in n_cells :
1399
1381
if n > 1 :
1400
- faces = [tuple (simplex_cells [n - 1 ][cell [j ]]) for j in range (n + 1 )]
1382
+ faces = [tuple (simplex_cells [n - 1 ][cell [j ]])
1383
+ for j in range (n + 1 )]
1401
1384
one_cell = dict (zip (faces , self_cells [n ][n_cells [cell ]]))
1402
1385
else :
1403
1386
temp = dict (zip (cell , self_cells [n ][n_cells [cell ]]))
@@ -1407,7 +1390,7 @@ def _epi_from_standard_simplex(self, idx=-1, dim=None):
1407
1390
for j in one_cell :
1408
1391
if j not in faces_dict :
1409
1392
faces_dict [j ] = one_cell [j ]
1410
- mapping [n - 1 ] = faces_dict
1393
+ mapping [n - 1 ] = faces_dict
1411
1394
return mapping
1412
1395
1413
1396
def _is_glued (self , idx = - 1 , dim = None ):
@@ -1450,16 +1433,16 @@ def _is_glued(self, idx=-1, dim=None):
1450
1433
i = self .dimension () - 1
1451
1434
i_faces = set (simplex )
1452
1435
# if there are enough i_faces, then no gluing is evident so far
1453
- not_glued = (len (i_faces ) == binomial (dim + 1 , i + 1 ))
1436
+ not_glued = (len (i_faces ) == binomial (dim + 1 , i + 1 ))
1454
1437
while not_glued and i > 0 :
1455
1438
# count the (i-1) cells and compare to (n+1) choose i.
1456
1439
old_faces = i_faces
1457
1440
i_faces = set ()
1458
1441
all_cells = self .n_cells (i )
1459
1442
for face in old_faces :
1460
1443
i_faces .update (all_cells [face ])
1461
- not_glued = (len (i_faces ) == binomial (dim + 1 , i ))
1462
- i = i - 1
1444
+ not_glued = (len (i_faces ) == binomial (dim + 1 , i ))
1445
+ i -= 1
1463
1446
return not not_glued
1464
1447
1465
1448
def face_poset (self ):
@@ -1480,16 +1463,12 @@ def face_poset(self):
1480
1463
covers = {}
1481
1464
# store each n-simplex as a pair (n, idx).
1482
1465
for n in range (dim , 0 , - 1 ):
1483
- idx = 0
1484
- for s in self .n_cells (n ):
1485
- covers [(n , idx )] = list ({(n - 1 , i ) for i in s })
1486
- idx += 1
1466
+ for idx , s in enumerate (self .n_cells (n )):
1467
+ covers [(n , idx )] = [(n - 1 , i ) for i in set (s )]
1487
1468
# deal with vertices separately: they have no covers (in the
1488
1469
# dual poset).
1489
- idx = 0
1490
- for s in self .n_cells (0 ):
1470
+ for idx , s in enumerate (self .n_cells (0 )):
1491
1471
covers [(0 , idx )] = []
1492
- idx += 1
1493
1472
return Poset (Poset (covers ).hasse_diagram ().reverse ())
1494
1473
1495
1474
# implement using the definition? the simplices are obtained by
@@ -1674,7 +1653,8 @@ def Sphere(self, n):
1674
1653
"""
1675
1654
if n == 1 :
1676
1655
return DeltaComplex ([[()], [(0 , 0 )]])
1677
- return DeltaComplex ({Simplex (n ): True , Simplex (range (1 , n + 2 )): Simplex (n )})
1656
+ return DeltaComplex ({Simplex (n ): True ,
1657
+ Simplex (range (1 , n + 2 )): Simplex (n )})
1678
1658
1679
1659
def Torus (self ):
1680
1660
r"""
0 commit comments