106
106
from sage .categories .finite_lattice_posets import FiniteLatticePosets
107
107
from sage .graphs .digraph import DiGraph
108
108
from sage .rings .integer import Integer
109
+ from sage .sets .non_negative_integers import NonNegativeIntegers
110
+
111
+
112
+ def check_int (n , minimum = 0 ):
113
+ """
114
+ Check that ``n`` is an integer at least equal to ``minimum``.
115
+
116
+ This is a boilerplate function ensuring input safety.
117
+
118
+ INPUT:
119
+
120
+ - ``n`` -- anything
121
+
122
+ - ``minimum`` -- an optional integer (default: 0)
123
+
124
+ EXAMPLES::
125
+
126
+ sage: from sage.combinat.posets.poset_examples import check_int
127
+ sage: check_int(6, 3)
128
+ 6
129
+ sage: check_int(6)
130
+ 6
131
+
132
+ sage: check_int(-1)
133
+ Traceback (most recent call last):
134
+ ...
135
+ ValueError: number of elements must be a non-negative integer, not -1
136
+
137
+ sage: check_int(1, 3)
138
+ Traceback (most recent call last):
139
+ ...
140
+ ValueError: number of elements must be an integer at least 3, not 1
141
+
142
+ sage: check_int('junk')
143
+ Traceback (most recent call last):
144
+ ...
145
+ ValueError: number of elements must be a non-negative integer, not junk
146
+ """
147
+ if minimum == 0 :
148
+ msg = "a non-negative integer"
149
+ else :
150
+ msg = f"an integer at least { minimum } "
151
+ if n not in NonNegativeIntegers () or n < minimum :
152
+ raise ValueError ("number of elements must be " + msg + f", not { n } " )
153
+ return Integer (n )
109
154
110
155
111
156
class Posets (metaclass = ClasscallMetaclass ):
@@ -155,12 +200,7 @@ def __classcall__(cls, n=None):
155
200
"""
156
201
if n is None :
157
202
return sage .categories .posets .Posets ()
158
- try :
159
- n = Integer (n )
160
- except TypeError :
161
- raise TypeError ("number of elements must be an integer, not {0}" .format (n ))
162
- if n < 0 :
163
- raise ValueError ("number of elements must be non-negative, not {0}" .format (n ))
203
+ n = check_int (n )
164
204
return FinitePosets_n (n )
165
205
166
206
@staticmethod
@@ -203,12 +243,7 @@ def BooleanLattice(n, facade=None, use_subsets=False):
203
243
sage: list(posets.BooleanLattice(1, use_subsets=True))
204
244
[{}, {1}]
205
245
"""
206
- try :
207
- n = Integer (n )
208
- except TypeError :
209
- raise TypeError ("number of elements must be an integer, not {0}" .format (n ))
210
- if n < 0 :
211
- raise ValueError ("number of elements must be non-negative, not {0}" .format (n ))
246
+ n = check_int (n )
212
247
if n == 0 :
213
248
if use_subsets :
214
249
from sage .sets .set import Set
@@ -282,12 +317,7 @@ def ChainPoset(n, facade=None):
282
317
sage: C.cover_relations()
283
318
[[0, 1]]
284
319
"""
285
- try :
286
- n = Integer (n )
287
- except TypeError :
288
- raise TypeError ("number of elements must be an integer, not {0}" .format (n ))
289
- if n < 0 :
290
- raise ValueError ("number of elements must be non-negative, not {0}" .format (n ))
320
+ n = check_int (n )
291
321
D = DiGraph ([range (n ), [[x , x + 1 ] for x in range (n - 1 )]],
292
322
format = 'vertices_and_edges' )
293
323
return FiniteLatticePoset (hasse_diagram = D ,
@@ -335,12 +365,7 @@ def AntichainPoset(n, facade=None):
335
365
sage: C.cover_relations()
336
366
[]
337
367
"""
338
- try :
339
- n = Integer (n )
340
- except TypeError :
341
- raise TypeError ("number of elements must be an integer, not {0}" .format (n ))
342
- if n < 0 :
343
- raise ValueError ("number of elements must be non-negative, not {0}" .format (n ))
368
+ n = check_int (n )
344
369
return Poset ((range (n ), []), facade = facade )
345
370
346
371
@staticmethod
@@ -398,12 +423,7 @@ def DiamondPoset(n, facade=None):
398
423
sage: posets.DiamondPoset(7)
399
424
Finite lattice containing 7 elements
400
425
"""
401
- try :
402
- n = Integer (n )
403
- except TypeError :
404
- raise TypeError ("number of elements must be an integer, not {0}" .format (n ))
405
- if n <= 2 :
406
- raise ValueError ("n must be an integer at least 3" )
426
+ n = check_int (n , 3 )
407
427
c = [[n - 1 ] for x in range (n )]
408
428
c [0 ] = [x for x in range (1 , n - 1 )]
409
429
c [n - 1 ] = []
@@ -435,12 +455,7 @@ def Crown(n, facade=None):
435
455
sage: posets.Crown(3)
436
456
Finite poset containing 6 elements
437
457
"""
438
- try :
439
- n = Integer (n )
440
- except TypeError :
441
- raise TypeError ("number of elements must be an integer, not {0}" .format (n ))
442
- if n < 2 :
443
- raise ValueError ("n must be an integer at least 2" )
458
+ n = check_int (n , 2 )
444
459
D = {i : [i + n , i + n + 1 ] for i in range (n - 1 )}
445
460
D [n - 1 ] = [n , n + n - 1 ]
446
461
return FinitePoset (hasse_diagram = DiGraph (D ), category = FinitePosets (),
@@ -478,12 +493,7 @@ def DivisorLattice(n, facade=None):
478
493
Finite lattice containing 1 elements with distinguished linear extension
479
494
"""
480
495
from sage .arith .misc import divisors , is_prime
481
- try :
482
- n = Integer (n )
483
- except TypeError :
484
- raise TypeError ("number of elements must be an integer, not {0}" .format (n ))
485
- if n <= 0 :
486
- raise ValueError ("n must be a positive integer" )
496
+ n = check_int (n , 1 )
487
497
Div_n = divisors (n )
488
498
hasse = DiGraph ([Div_n , lambda a , b : b % a == 0 and is_prime (b // a )])
489
499
return FiniteLatticePoset (hasse , elements = Div_n , facade = facade ,
@@ -623,9 +633,7 @@ def IntegerPartitionsDominanceOrder(n):
623
633
[[4, 2], [5, 1]],
624
634
[[5, 1], [6]]]
625
635
"""
626
- from sage .rings .semirings .non_negative_integer_semiring import NN
627
- if n not in NN :
628
- raise ValueError ('n must be an integer' )
636
+ n = check_int (n )
629
637
from sage .combinat .partition import Partitions , Partition
630
638
return LatticePoset ((Partitions (n ), Partition .dominates )).dual ()
631
639
@@ -663,14 +671,7 @@ def PowerPoset(n):
663
671
0
664
672
"""
665
673
# Todo: Make this faster.
666
-
667
- try :
668
- n = Integer (n )
669
- except TypeError :
670
- raise TypeError ("parameter n must be an integer, not {0}" .format (n ))
671
- if n < 0 :
672
- raise ValueError ("parameter n must be non-negative, not {0}" .format (n ))
673
-
674
+ n = check_int (n )
674
675
all_pos_n = set ()
675
676
Pn = list (Posets (n ))
676
677
for P in Pn :
@@ -759,16 +760,6 @@ def RandomPoset(n, p):
759
760
760
761
TESTS::
761
762
762
- sage: posets.RandomPoset('junk', 0.5)
763
- Traceback (most recent call last):
764
- ...
765
- TypeError: number of elements must be an integer, not junk
766
-
767
- sage: posets.RandomPoset(-6, 0.5)
768
- Traceback (most recent call last):
769
- ...
770
- ValueError: number of elements must be non-negative, not -6
771
-
772
763
sage: posets.RandomPoset(6, 'garbage')
773
764
Traceback (most recent call last):
774
765
...
@@ -783,13 +774,7 @@ def RandomPoset(n, p):
783
774
Finite poset containing 0 elements
784
775
"""
785
776
from sage .misc .prandom import random
786
-
787
- try :
788
- n = Integer (n )
789
- except (TypeError , ValueError ):
790
- raise TypeError ("number of elements must be an integer, not {0}" .format (n ))
791
- if n < 0 :
792
- raise ValueError ("number of elements must be non-negative, not {0}" .format (n ))
777
+ n = check_int (n )
793
778
try :
794
779
p = float (p )
795
780
except (TypeError , ValueError ):
@@ -856,16 +841,6 @@ def RandomLattice(n, p, properties=None):
856
841
857
842
TESTS::
858
843
859
- sage: posets.RandomLattice('junk', 0.5)
860
- Traceback (most recent call last):
861
- ...
862
- TypeError: number of elements must be an integer, not junk
863
-
864
- sage: posets.RandomLattice(-6, 0.5)
865
- Traceback (most recent call last):
866
- ...
867
- ValueError: number of elements must be non-negative, not -6
868
-
869
844
sage: posets.RandomLattice(6, 'garbage')
870
845
Traceback (most recent call last):
871
846
...
@@ -885,13 +860,7 @@ def RandomLattice(n, p, properties=None):
885
860
Finite lattice containing 0 elements
886
861
"""
887
862
from copy import copy
888
-
889
- try :
890
- n = Integer (n )
891
- except TypeError :
892
- raise TypeError ("number of elements must be an integer, not {0}" .format (n ))
893
- if n < 0 :
894
- raise ValueError ("number of elements must be non-negative, not {0}" .format (n ))
863
+ n = check_int (n )
895
864
try :
896
865
p = float (p )
897
866
except Exception :
@@ -973,10 +942,8 @@ def SetPartitions(n):
973
942
sage: posets.SetPartitions(4)
974
943
Finite lattice containing 15 elements
975
944
"""
976
- from sage .rings .semirings .non_negative_integer_semiring import NN
977
- if n not in NN :
978
- raise ValueError ('n must be an integer' )
979
945
from sage .combinat .set_partition import SetPartitions
946
+ n = check_int (n )
980
947
S = SetPartitions (n )
981
948
982
949
def covers (x ):
@@ -1083,12 +1050,7 @@ def StandardExample(n, facade=None):
1083
1050
sage: P(4) < P(3), P(4) > P(3)
1084
1051
(False, False)
1085
1052
"""
1086
- try :
1087
- n = Integer (n )
1088
- except TypeError :
1089
- raise TypeError ("dimension must be an integer, not {0}" .format (n ))
1090
- if n < 2 :
1091
- raise ValueError ("dimension must be at least 2, not {0}" .format (n ))
1053
+ n = check_int (n , 2 )
1092
1054
return Poset ((range (2 * n ), [[i , j + n ] for i in range (n )
1093
1055
for j in range (n ) if i != j ]),
1094
1056
facade = facade )
@@ -1244,17 +1206,20 @@ def TetrahedralPoset(n, *colors, **labels):
1244
1206
sage: tet = posets.TetrahedralPoset(3, 'green','yellow','blue','orange')
1245
1207
sage: ji.is_isomorphic(tet)
1246
1208
True
1209
+
1210
+ TESTS::
1211
+
1212
+ sage: posets.TetrahedralPoset(4,'scarlet')
1213
+ Traceback (most recent call last):
1214
+ ...
1215
+ ValueError: color input must be among: 'green', 'red', 'yellow',
1216
+ 'orange', 'silver', and 'blue'
1247
1217
"""
1218
+ n = check_int (n , 2 )
1248
1219
n = n - 1
1249
- try :
1250
- n = Integer (n )
1251
- except TypeError :
1252
- raise TypeError ("n must be an integer" )
1253
- if n < 2 :
1254
- raise ValueError ("n must be greater than 2" )
1255
1220
for c in colors :
1256
1221
if c not in ('green' , 'red' , 'yellow' , 'orange' , 'silver' , 'blue' ):
1257
- raise ValueError ("color input must be from the following : 'green', 'red', 'yellow', 'orange', 'silver', and 'blue'" )
1222
+ raise ValueError ("color input must be among : 'green', 'red', 'yellow', 'orange', 'silver', and 'blue'" )
1258
1223
elem = [(i , j , k ) for i in range (n )
1259
1224
for j in range (n - i ) for k in range (n - i - j )]
1260
1225
rels = []
@@ -1421,12 +1386,7 @@ def UpDownPoset(n, m=1):
1421
1386
sage: P = posets.UpDownPoset(0); P
1422
1387
Finite poset containing 0 elements
1423
1388
"""
1424
- try :
1425
- n = Integer (n )
1426
- except TypeError :
1427
- raise TypeError ("number of elements must be an integer, not {0}" .format (n ))
1428
- if n < 0 :
1429
- raise ValueError ("number of elements must be non-negative, not {0}" .format (n ))
1389
+ n = check_int (n )
1430
1390
try :
1431
1391
m = Integer (m )
1432
1392
except TypeError :
@@ -1581,12 +1541,7 @@ def YoungFibonacci(n):
1581
1541
from sage .categories .finite_posets import FinitePosets
1582
1542
from sage .combinat .words .word import Word
1583
1543
1584
- try :
1585
- n = Integer (n )
1586
- except TypeError :
1587
- raise TypeError ("number of elements must be an integer, not {0}" .format (n ))
1588
- if n < 0 :
1589
- raise ValueError ("number of elements must be non-negative, not {0}" .format (n ))
1544
+ n = check_int (n )
1590
1545
1591
1546
if n == 0 :
1592
1547
return MeetSemilattice ({'' : []})
@@ -1632,12 +1587,7 @@ def DoubleTailedDiamond(n):
1632
1587
sage: P.cover_relations()
1633
1588
[[1, 2], [2, 3], [2, 4], [3, 5], [4, 5], [5, 6]]
1634
1589
"""
1635
- try :
1636
- n = Integer (n )
1637
- except TypeError :
1638
- raise TypeError ("number of elements must be an integer, not {}" .format (n ))
1639
- if n <= 0 :
1640
- raise ValueError ("number of elements must be nonnegative, not {}" .format (n ))
1590
+ n = check_int (n , 1 )
1641
1591
1642
1592
edges = [(i , i + 1 ) for i in range (1 , n )]
1643
1593
edges .extend ([(n , n + 1 ), (n , n + 2 ), (n + 1 , n + 3 ), (n + 2 , n + 3 )])
@@ -1680,12 +1630,7 @@ def PermutationPattern(n):
1680
1630
sage: posets.PermutationPattern(2)
1681
1631
Finite poset containing 3 elements
1682
1632
"""
1683
- try :
1684
- n = Integer (n )
1685
- except TypeError :
1686
- raise TypeError ("number of elements must be an integer, not {}" .format (n ))
1687
- if n <= 0 :
1688
- raise ValueError ("number of elements must be nonnegative, not {}" .format (n ))
1633
+ n = check_int (n , 1 )
1689
1634
elem = []
1690
1635
for i in range (1 , n + 1 ):
1691
1636
elem += Permutations (i )
@@ -1841,6 +1786,7 @@ def RibbonPoset(n, descents):
1841
1786
sage: sorted(R.cover_relations())
1842
1787
[[0, 1], [2, 1], [3, 2], [3, 4]]
1843
1788
"""
1789
+ n = check_int (n )
1844
1790
return Mobile (DiGraph ([list (range (n )),
1845
1791
[(i + 1 , i ) if i in descents else (i , i + 1 )
1846
1792
for i in range (n - 1 )]]))
0 commit comments