87
87
[-1 0 2 1]
88
88
[ 2 1 3 -1]
89
89
[ 0 3 -1 2]
90
- sage: T1.nr_filled_cells ()
90
+ sage: T1.n_filled_cells ()
91
91
12
92
92
sage: genus(T1, T2)
93
93
1
148
148
149
149
150
150
class LatinSquare :
151
- def __init__ (self , * args ):
151
+ def __init__ (self , * args ) -> None :
152
152
"""
153
153
Latin squares.
154
154
@@ -203,7 +203,7 @@ def dumps(self):
203
203
from sage .misc .persist import dumps
204
204
return dumps (self .square )
205
205
206
- def __str__ (self ):
206
+ def __str__ (self ) -> str :
207
207
"""
208
208
The string representation of a latin square is the same as the
209
209
underlying matrix.
@@ -216,7 +216,7 @@ def __str__(self):
216
216
"""
217
217
return str (self .square )
218
218
219
- def __repr__ (self ):
219
+ def __repr__ (self ) -> str :
220
220
"""
221
221
The representation of a latin square is the same as the underlying
222
222
matrix.
@@ -241,13 +241,9 @@ def __getitem__(self, rc):
241
241
sage: B[1, 1]
242
242
2
243
243
"""
244
+ return self .square [* rc ]
244
245
245
- r = rc [0 ]
246
- c = rc [1 ]
247
-
248
- return self .square [r , c ]
249
-
250
- def __setitem__ (self , rc , val ):
246
+ def __setitem__ (self , rc , val ) -> None :
251
247
"""
252
248
If L is a LatinSquare then this method allows us to set L[r, c].
253
249
@@ -259,13 +255,9 @@ def __setitem__(self, rc, val):
259
255
sage: B[1, 1]
260
256
10
261
257
"""
258
+ self .square [* rc ] = val
262
259
263
- r = rc [0 ]
264
- c = rc [1 ]
265
-
266
- self .square [r , c ] = val
267
-
268
- def set_immutable (self ):
260
+ def set_immutable (self ) -> None :
269
261
"""
270
262
A latin square is immutable if the underlying matrix is immutable.
271
263
@@ -277,10 +269,9 @@ def set_immutable(self):
277
269
{[0 1]
278
270
[2 3]: 0}
279
271
"""
280
-
281
272
self .square .set_immutable ()
282
273
283
- def __hash__ (self ):
274
+ def __hash__ (self ) -> int :
284
275
"""
285
276
The hash of a latin square is precisely the hash of the underlying
286
277
matrix.
@@ -295,7 +286,7 @@ def __hash__(self):
295
286
"""
296
287
return hash (self .square )
297
288
298
- def __eq__ (self , Q ):
289
+ def __eq__ (self , Q ) -> bool :
299
290
"""
300
291
Two latin squares are equal if the underlying matrices are equal.
301
292
@@ -309,7 +300,6 @@ def __eq__(self, Q):
309
300
sage: A == B
310
301
True
311
302
"""
312
-
313
303
return self .square == Q .square
314
304
315
305
def __copy__ (self ):
@@ -329,7 +319,7 @@ def __copy__(self):
329
319
C .square = copy (self .square )
330
320
return C
331
321
332
- def clear_cells (self ):
322
+ def clear_cells (self ) -> None :
333
323
"""
334
324
Mark every cell in ``self`` as being empty.
335
325
@@ -391,7 +381,7 @@ def column(self, x):
391
381
"""
392
382
return self .square .column (x )
393
383
394
- def list (self ):
384
+ def list (self ) -> list :
395
385
"""
396
386
Convert the latin square into a list, in a row-wise manner.
397
387
@@ -403,25 +393,23 @@ def list(self):
403
393
"""
404
394
return self .square .list ()
405
395
406
- def nr_filled_cells (self ):
396
+ def n_filled_cells (self ) -> int :
407
397
"""
408
398
Return the number of filled cells (i.e. cells with a positive
409
399
value) in the partial latin square ``self``.
410
400
411
401
EXAMPLES::
412
402
413
403
sage: from sage.combinat.matrices.latin import *
414
- sage: LatinSquare(matrix([[0, -1], [-1, 0]])).nr_filled_cells ()
404
+ sage: LatinSquare(matrix([[0, -1], [-1, 0]])).n_filled_cells ()
415
405
2
416
406
"""
417
- s = 0
418
- for r in range (self .nrows ()):
419
- for c in range (self .ncols ()):
420
- if self [r , c ] >= 0 :
421
- s += 1
422
- return s
407
+ return sum (1 for r in range (self .nrows ()) for c in range (self .ncols ())
408
+ if self [r , c ] >= 0 )
409
+
410
+ nr_filled_cells = n_filled_cells
423
411
424
- def actual_row_col_sym_sizes (self ):
412
+ def actual_row_col_sym_sizes (self ) -> tuple :
425
413
"""
426
414
Bitrades sometimes end up in partial latin squares with unused
427
415
rows, columns, or symbols. This function works out the actual
@@ -448,7 +436,7 @@ def actual_row_col_sym_sizes(self):
448
436
"""
449
437
row_max = self .nrows ()
450
438
col_max = self .ncols ()
451
- sym_max = self .nr_distinct_symbols ()
439
+ sym_max = self .n_distinct_symbols ()
452
440
453
441
while self .is_empty_row (row_max - 1 ):
454
442
row_max -= 1
@@ -457,7 +445,7 @@ def actual_row_col_sym_sizes(self):
457
445
458
446
return row_max , col_max , sym_max
459
447
460
- def is_empty_column (self , c ):
448
+ def is_empty_column (self , c ) -> bool :
461
449
"""
462
450
Check if column c of the partial latin square ``self`` is empty.
463
451
@@ -471,9 +459,9 @@ def is_empty_column(self, c):
471
459
sage: L.is_empty_column(0)
472
460
True
473
461
"""
474
- return list ( set ( self .column (c ))) == [ - 1 ]
462
+ return all ( cf == - 1 for cf in self .column (c ))
475
463
476
- def is_empty_row (self , r ):
464
+ def is_empty_row (self , r ) -> bool :
477
465
"""
478
466
Check if row r of the partial latin square ``self`` is empty.
479
467
@@ -487,29 +475,31 @@ def is_empty_row(self, r):
487
475
sage: L.is_empty_row(0)
488
476
True
489
477
"""
490
- return list ( set ( self .row (r ))) == [ - 1 ]
478
+ return all ( cf == - 1 for cf in self .row (r ))
491
479
492
- def nr_distinct_symbols (self ):
480
+ def n_distinct_symbols (self ) -> int :
493
481
"""
494
482
Return the number of distinct symbols in the partial latin square
495
483
``self``.
496
484
497
485
EXAMPLES::
498
486
499
487
sage: from sage.combinat.matrices.latin import *
500
- sage: back_circulant(5).nr_distinct_symbols ()
488
+ sage: back_circulant(5).n_distinct_symbols ()
501
489
5
502
490
sage: L = LatinSquare(10)
503
- sage: L.nr_distinct_symbols ()
491
+ sage: L.n_distinct_symbols ()
504
492
0
505
493
sage: L[0, 0] = 0
506
494
sage: L[0, 1] = 1
507
- sage: L.nr_distinct_symbols ()
495
+ sage: L.n_distinct_symbols ()
508
496
2
509
497
"""
510
498
symbols = set (flatten ([list (x ) for x in list (self .square )]))
511
499
return sum (1 for x in symbols if x >= 0 )
512
500
501
+ nr_distinct_symbols = n_distinct_symbols
502
+
513
503
def apply_isotopism (self , row_perm , col_perm , sym_perm ):
514
504
"""
515
505
An isotopism is a permutation of the rows, columns, and symbols of
@@ -641,7 +631,7 @@ def top_left_empty_cell(self):
641
631
642
632
return None
643
633
644
- def is_partial_latin_square (self ):
634
+ def is_partial_latin_square (self ) -> bool :
645
635
"""
646
636
``self`` is a partial latin square if it is an n by n matrix, and each
647
637
symbol in [0, 1, ..., n-1] appears at most once in each row, and at
@@ -702,7 +692,7 @@ def is_partial_latin_square(self):
702
692
703
693
return True
704
694
705
- def is_latin_square (self ):
695
+ def is_latin_square (self ) -> bool :
706
696
"""
707
697
``self`` is a latin square if it is an n by n matrix, and each symbol
708
698
in [0, 1, ..., n-1] appears exactly once in each row, and exactly
@@ -817,7 +807,7 @@ def random_empty_cell(self):
817
807
818
808
return [rc [0 ], rc [1 ]]
819
809
820
- def is_uniquely_completable (self ):
810
+ def is_uniquely_completable (self ) -> bool :
821
811
"""
822
812
Return ``True`` if the partial latin square ``self`` has exactly one
823
813
completion to a latin square. This is just a wrapper for the
@@ -842,10 +832,9 @@ def is_uniquely_completable(self):
842
832
sage: G.is_uniquely_completable()
843
833
False
844
834
"""
845
-
846
835
return self .dlxcpp_has_unique_completion ()
847
836
848
- def is_completable (self ):
837
+ def is_completable (self ) -> bool :
849
838
"""
850
839
Return ``True`` if the partial latin square can be completed to a
851
840
latin square.
@@ -913,7 +902,6 @@ def gcs(self):
913
902
[ 6 -1 4 -1 2 -1 0 -1]
914
903
[-1 -1 -1 -1 -1 -1 -1 -1]
915
904
"""
916
-
917
905
n = self .nrows ()
918
906
919
907
from copy import copy
@@ -929,7 +917,7 @@ def gcs(self):
929
917
930
918
return G
931
919
932
- def dlxcpp_has_unique_completion (self ):
920
+ def dlxcpp_has_unique_completion (self ) -> bool :
933
921
"""
934
922
Check if the partial latin square ``self`` of order n can be embedded
935
923
in precisely one latin square of order n.
@@ -948,7 +936,7 @@ def dlxcpp_has_unique_completion(self):
948
936
"""
949
937
return len (dlxcpp_find_completions (self , nr_to_find = 2 )) == 1
950
938
951
- def vals_in_row (self , r ):
939
+ def vals_in_row (self , r ) -> dict :
952
940
"""
953
941
Return a dictionary with key e if and only if row r of ``self`` has
954
942
the symbol e.
@@ -961,7 +949,6 @@ def vals_in_row(self, r):
961
949
sage: back_circulant(3).vals_in_row(0)
962
950
{0: True, 1: True, 2: True}
963
951
"""
964
-
965
952
n = self .ncols ()
966
953
vals_in_row = {}
967
954
@@ -972,7 +959,7 @@ def vals_in_row(self, r):
972
959
973
960
return vals_in_row
974
961
975
- def vals_in_col (self , c ):
962
+ def vals_in_col (self , c ) -> dict :
976
963
"""
977
964
Return a dictionary with key e if and only if column c of ``self`` has
978
965
the symbol e.
@@ -995,7 +982,7 @@ def vals_in_col(self, c):
995
982
996
983
return vals_in_col
997
984
998
- def latex (self ):
985
+ def latex (self ) -> str :
999
986
r"""
1000
987
Return LaTeX code for the latin square.
1001
988
@@ -1163,7 +1150,6 @@ def disjoint_mate_dlxcpp_rows_and_map(self, allow_subtrade):
1163
1150
(15, 27, 43): (2, 3, 3),
1164
1151
(15, 31, 47): (3, 3, 3)})
1165
1152
"""
1166
-
1167
1153
assert self .nrows () == self .ncols ()
1168
1154
1169
1155
n = self .nrows ()
@@ -1254,10 +1240,10 @@ def find_disjoint_mates(self, nr_to_find=None, allow_subtrade=False):
1254
1240
1255
1241
dlx_rows , cmap = self .disjoint_mate_dlxcpp_rows_and_map (allow_subtrade )
1256
1242
1257
- nr_found = 0
1243
+ n_found = 0
1258
1244
1259
1245
for x in DLXCPP (dlx_rows ):
1260
- nr_found += 1
1246
+ n_found += 1
1261
1247
1262
1248
from copy import deepcopy
1263
1249
Q = deepcopy (self )
@@ -1270,10 +1256,10 @@ def find_disjoint_mates(self, nr_to_find=None, allow_subtrade=False):
1270
1256
1271
1257
yield Q
1272
1258
1273
- if nr_to_find is not None and nr_found >= nr_to_find :
1259
+ if nr_to_find is not None and n_found >= nr_to_find :
1274
1260
return
1275
1261
1276
- def contained_in (self , Q ):
1262
+ def contained_in (self , Q ) -> bool :
1277
1263
r"""
1278
1264
Return ``True`` if ``self`` is a subset of `Q`.
1279
1265
@@ -1324,7 +1310,7 @@ def genus(T1, T2):
1324
1310
3
1325
1311
"""
1326
1312
cells_map , t1 , t2 , t3 = tau123 (T1 , T2 )
1327
- return (len (t1 .to_cycles ()) + len (t2 .to_cycles ()) + len (t3 .to_cycles ()) - T1 .nr_filled_cells () - 2 ) // (- 2 )
1313
+ return (len (t1 .to_cycles ()) + len (t2 .to_cycles ()) + len (t3 .to_cycles ()) - T1 .n_filled_cells () - 2 ) // (- 2 )
1328
1314
1329
1315
1330
1316
def tau123 (T1 , T2 ):
@@ -1434,7 +1420,7 @@ def tau123(T1, T2):
1434
1420
1435
1421
The product t1\*t2\*t3 is the identity, i.e. it fixes every point::
1436
1422
1437
- sage: len((t1*t2*t3).fixed_points()) == T1.nr_filled_cells ()
1423
+ sage: len((t1*t2*t3).fixed_points()) == T1.n_filled_cells ()
1438
1424
True
1439
1425
"""
1440
1426
assert is_bitrade (T1 , T2 )
@@ -2592,11 +2578,11 @@ def tau_to_bitrade(t1, t2, t3):
2592
2578
for r in range (len (c1 )):
2593
2579
for c in range (len (c2 )):
2594
2580
for s in range (len (c3 )):
2595
- nr_common = len (reduce (set .intersection ,
2581
+ n_common = len (reduce (set .intersection ,
2596
2582
[set (c1 [r ]), set (c2 [c ]), set (c3 [s ])]))
2597
- assert nr_common in [0 , 1 ]
2583
+ assert n_common in [0 , 1 ]
2598
2584
2599
- if nr_common == 1 :
2585
+ if n_common == 1 :
2600
2586
T1 [r , c ] = s
2601
2587
2602
2588
for cycle in c1 :
0 commit comments