Skip to content

Commit 2af204b

Browse files
committed
provide alias in Latin squares
1 parent f4adc25 commit 2af204b

File tree

1 file changed

+47
-61
lines changed

1 file changed

+47
-61
lines changed

src/sage/combinat/matrices/latin.py

Lines changed: 47 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
[-1 0 2 1]
8888
[ 2 1 3 -1]
8989
[ 0 3 -1 2]
90-
sage: T1.nr_filled_cells()
90+
sage: T1.n_filled_cells()
9191
12
9292
sage: genus(T1, T2)
9393
1
@@ -148,7 +148,7 @@
148148

149149

150150
class LatinSquare:
151-
def __init__(self, *args):
151+
def __init__(self, *args) -> None:
152152
"""
153153
Latin squares.
154154
@@ -203,7 +203,7 @@ def dumps(self):
203203
from sage.misc.persist import dumps
204204
return dumps(self.square)
205205

206-
def __str__(self):
206+
def __str__(self) -> str:
207207
"""
208208
The string representation of a latin square is the same as the
209209
underlying matrix.
@@ -216,7 +216,7 @@ def __str__(self):
216216
"""
217217
return str(self.square)
218218

219-
def __repr__(self):
219+
def __repr__(self) -> str:
220220
"""
221221
The representation of a latin square is the same as the underlying
222222
matrix.
@@ -241,13 +241,9 @@ def __getitem__(self, rc):
241241
sage: B[1, 1]
242242
2
243243
"""
244+
return self.square[*rc]
244245

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:
251247
"""
252248
If L is a LatinSquare then this method allows us to set L[r, c].
253249
@@ -259,13 +255,9 @@ def __setitem__(self, rc, val):
259255
sage: B[1, 1]
260256
10
261257
"""
258+
self.square[*rc] = val
262259

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:
269261
"""
270262
A latin square is immutable if the underlying matrix is immutable.
271263
@@ -277,10 +269,9 @@ def set_immutable(self):
277269
{[0 1]
278270
[2 3]: 0}
279271
"""
280-
281272
self.square.set_immutable()
282273

283-
def __hash__(self):
274+
def __hash__(self) -> int:
284275
"""
285276
The hash of a latin square is precisely the hash of the underlying
286277
matrix.
@@ -295,7 +286,7 @@ def __hash__(self):
295286
"""
296287
return hash(self.square)
297288

298-
def __eq__(self, Q):
289+
def __eq__(self, Q) -> bool:
299290
"""
300291
Two latin squares are equal if the underlying matrices are equal.
301292
@@ -309,7 +300,6 @@ def __eq__(self, Q):
309300
sage: A == B
310301
True
311302
"""
312-
313303
return self.square == Q.square
314304

315305
def __copy__(self):
@@ -329,7 +319,7 @@ def __copy__(self):
329319
C.square = copy(self.square)
330320
return C
331321

332-
def clear_cells(self):
322+
def clear_cells(self) -> None:
333323
"""
334324
Mark every cell in ``self`` as being empty.
335325
@@ -391,7 +381,7 @@ def column(self, x):
391381
"""
392382
return self.square.column(x)
393383

394-
def list(self):
384+
def list(self) -> list:
395385
"""
396386
Convert the latin square into a list, in a row-wise manner.
397387
@@ -403,25 +393,23 @@ def list(self):
403393
"""
404394
return self.square.list()
405395

406-
def nr_filled_cells(self):
396+
def n_filled_cells(self) -> int:
407397
"""
408398
Return the number of filled cells (i.e. cells with a positive
409399
value) in the partial latin square ``self``.
410400
411401
EXAMPLES::
412402
413403
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()
415405
2
416406
"""
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
423411

424-
def actual_row_col_sym_sizes(self):
412+
def actual_row_col_sym_sizes(self) -> tuple:
425413
"""
426414
Bitrades sometimes end up in partial latin squares with unused
427415
rows, columns, or symbols. This function works out the actual
@@ -448,7 +436,7 @@ def actual_row_col_sym_sizes(self):
448436
"""
449437
row_max = self.nrows()
450438
col_max = self.ncols()
451-
sym_max = self.nr_distinct_symbols()
439+
sym_max = self.n_distinct_symbols()
452440

453441
while self.is_empty_row(row_max-1):
454442
row_max -= 1
@@ -457,7 +445,7 @@ def actual_row_col_sym_sizes(self):
457445

458446
return row_max, col_max, sym_max
459447

460-
def is_empty_column(self, c):
448+
def is_empty_column(self, c) -> bool:
461449
"""
462450
Check if column c of the partial latin square ``self`` is empty.
463451
@@ -471,9 +459,9 @@ def is_empty_column(self, c):
471459
sage: L.is_empty_column(0)
472460
True
473461
"""
474-
return list(set(self.column(c))) == [-1]
462+
return all(cf == -1 for cf in self.column(c))
475463

476-
def is_empty_row(self, r):
464+
def is_empty_row(self, r) -> bool:
477465
"""
478466
Check if row r of the partial latin square ``self`` is empty.
479467
@@ -487,29 +475,31 @@ def is_empty_row(self, r):
487475
sage: L.is_empty_row(0)
488476
True
489477
"""
490-
return list(set(self.row(r))) == [-1]
478+
return all(cf == -1 for cf in self.row(r))
491479

492-
def nr_distinct_symbols(self):
480+
def n_distinct_symbols(self) -> int:
493481
"""
494482
Return the number of distinct symbols in the partial latin square
495483
``self``.
496484
497485
EXAMPLES::
498486
499487
sage: from sage.combinat.matrices.latin import *
500-
sage: back_circulant(5).nr_distinct_symbols()
488+
sage: back_circulant(5).n_distinct_symbols()
501489
5
502490
sage: L = LatinSquare(10)
503-
sage: L.nr_distinct_symbols()
491+
sage: L.n_distinct_symbols()
504492
0
505493
sage: L[0, 0] = 0
506494
sage: L[0, 1] = 1
507-
sage: L.nr_distinct_symbols()
495+
sage: L.n_distinct_symbols()
508496
2
509497
"""
510498
symbols = set(flatten([list(x) for x in list(self.square)]))
511499
return sum(1 for x in symbols if x >= 0)
512500

501+
nr_distinct_symbols = n_distinct_symbols
502+
513503
def apply_isotopism(self, row_perm, col_perm, sym_perm):
514504
"""
515505
An isotopism is a permutation of the rows, columns, and symbols of
@@ -641,7 +631,7 @@ def top_left_empty_cell(self):
641631

642632
return None
643633

644-
def is_partial_latin_square(self):
634+
def is_partial_latin_square(self) -> bool:
645635
"""
646636
``self`` is a partial latin square if it is an n by n matrix, and each
647637
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):
702692

703693
return True
704694

705-
def is_latin_square(self):
695+
def is_latin_square(self) -> bool:
706696
"""
707697
``self`` is a latin square if it is an n by n matrix, and each symbol
708698
in [0, 1, ..., n-1] appears exactly once in each row, and exactly
@@ -817,7 +807,7 @@ def random_empty_cell(self):
817807

818808
return [rc[0], rc[1]]
819809

820-
def is_uniquely_completable(self):
810+
def is_uniquely_completable(self) -> bool:
821811
"""
822812
Return ``True`` if the partial latin square ``self`` has exactly one
823813
completion to a latin square. This is just a wrapper for the
@@ -842,10 +832,9 @@ def is_uniquely_completable(self):
842832
sage: G.is_uniquely_completable()
843833
False
844834
"""
845-
846835
return self.dlxcpp_has_unique_completion()
847836

848-
def is_completable(self):
837+
def is_completable(self) -> bool:
849838
"""
850839
Return ``True`` if the partial latin square can be completed to a
851840
latin square.
@@ -913,7 +902,6 @@ def gcs(self):
913902
[ 6 -1 4 -1 2 -1 0 -1]
914903
[-1 -1 -1 -1 -1 -1 -1 -1]
915904
"""
916-
917905
n = self.nrows()
918906

919907
from copy import copy
@@ -929,7 +917,7 @@ def gcs(self):
929917

930918
return G
931919

932-
def dlxcpp_has_unique_completion(self):
920+
def dlxcpp_has_unique_completion(self) -> bool:
933921
"""
934922
Check if the partial latin square ``self`` of order n can be embedded
935923
in precisely one latin square of order n.
@@ -948,7 +936,7 @@ def dlxcpp_has_unique_completion(self):
948936
"""
949937
return len(dlxcpp_find_completions(self, nr_to_find=2)) == 1
950938

951-
def vals_in_row(self, r):
939+
def vals_in_row(self, r) -> dict:
952940
"""
953941
Return a dictionary with key e if and only if row r of ``self`` has
954942
the symbol e.
@@ -961,7 +949,6 @@ def vals_in_row(self, r):
961949
sage: back_circulant(3).vals_in_row(0)
962950
{0: True, 1: True, 2: True}
963951
"""
964-
965952
n = self.ncols()
966953
vals_in_row = {}
967954

@@ -972,7 +959,7 @@ def vals_in_row(self, r):
972959

973960
return vals_in_row
974961

975-
def vals_in_col(self, c):
962+
def vals_in_col(self, c) -> dict:
976963
"""
977964
Return a dictionary with key e if and only if column c of ``self`` has
978965
the symbol e.
@@ -995,7 +982,7 @@ def vals_in_col(self, c):
995982

996983
return vals_in_col
997984

998-
def latex(self):
985+
def latex(self) -> str:
999986
r"""
1000987
Return LaTeX code for the latin square.
1001988
@@ -1163,7 +1150,6 @@ def disjoint_mate_dlxcpp_rows_and_map(self, allow_subtrade):
11631150
(15, 27, 43): (2, 3, 3),
11641151
(15, 31, 47): (3, 3, 3)})
11651152
"""
1166-
11671153
assert self.nrows() == self.ncols()
11681154

11691155
n = self.nrows()
@@ -1254,10 +1240,10 @@ def find_disjoint_mates(self, nr_to_find=None, allow_subtrade=False):
12541240

12551241
dlx_rows, cmap = self.disjoint_mate_dlxcpp_rows_and_map(allow_subtrade)
12561242

1257-
nr_found = 0
1243+
n_found = 0
12581244

12591245
for x in DLXCPP(dlx_rows):
1260-
nr_found += 1
1246+
n_found += 1
12611247

12621248
from copy import deepcopy
12631249
Q = deepcopy(self)
@@ -1270,10 +1256,10 @@ def find_disjoint_mates(self, nr_to_find=None, allow_subtrade=False):
12701256

12711257
yield Q
12721258

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:
12741260
return
12751261

1276-
def contained_in(self, Q):
1262+
def contained_in(self, Q) -> bool:
12771263
r"""
12781264
Return ``True`` if ``self`` is a subset of `Q`.
12791265
@@ -1324,7 +1310,7 @@ def genus(T1, T2):
13241310
3
13251311
"""
13261312
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)
13281314

13291315

13301316
def tau123(T1, T2):
@@ -1434,7 +1420,7 @@ def tau123(T1, T2):
14341420
14351421
The product t1\*t2\*t3 is the identity, i.e. it fixes every point::
14361422
1437-
sage: len((t1*t2*t3).fixed_points()) == T1.nr_filled_cells()
1423+
sage: len((t1*t2*t3).fixed_points()) == T1.n_filled_cells()
14381424
True
14391425
"""
14401426
assert is_bitrade(T1, T2)
@@ -2592,11 +2578,11 @@ def tau_to_bitrade(t1, t2, t3):
25922578
for r in range(len(c1)):
25932579
for c in range(len(c2)):
25942580
for s in range(len(c3)):
2595-
nr_common = len(reduce(set.intersection,
2581+
n_common = len(reduce(set.intersection,
25962582
[set(c1[r]), set(c2[c]), set(c3[s])]))
2597-
assert nr_common in [0, 1]
2583+
assert n_common in [0, 1]
25982584

2599-
if nr_common == 1:
2585+
if n_common == 1:
26002586
T1[r, c] = s
26012587

26022588
for cycle in c1:

0 commit comments

Comments
 (0)