48
48
from sage .rings .integer import Integer
49
49
50
50
51
- def is_gale_ryser (r ,s ):
51
+ def is_gale_ryser (r , s ):
52
52
r"""
53
53
Tests whether the given sequences satisfy the condition
54
54
of the Gale-Ryser theorem.
@@ -314,20 +314,20 @@ def gale_ryser_theorem(p1, p2, algorithm="gale",
314
314
"""
315
315
from sage .matrix .constructor import matrix
316
316
317
- if not is_gale_ryser (p1 ,p2 ):
317
+ if not is_gale_ryser (p1 , p2 ):
318
318
return False
319
319
320
- if algorithm == "ryser" : # ryser's algorithm
320
+ if algorithm == "ryser" : # ryser's algorithm
321
321
from sage .combinat .permutation import Permutation
322
322
323
323
# Sorts the sequences if they are not, and remembers the permutation
324
324
# applied
325
- tmp = sorted (enumerate (p1 ), reverse = True , key = lambda x :x [1 ])
325
+ tmp = sorted (enumerate (p1 ), reverse = True , key = lambda x : x [1 ])
326
326
r = [x [1 ] for x in tmp ]
327
327
r_permutation = [x - 1 for x in Permutation ([x [0 ]+ 1 for x in tmp ]).inverse ()]
328
328
m = len (r )
329
329
330
- tmp = sorted (enumerate (p2 ), reverse = True , key = lambda x :x [1 ])
330
+ tmp = sorted (enumerate (p2 ), reverse = True , key = lambda x : x [1 ])
331
331
s = [x [1 ] for x in tmp ]
332
332
s_permutation = [x - 1 for x in Permutation ([x [0 ]+ 1 for x in tmp ]).inverse ()]
333
333
@@ -340,12 +340,12 @@ def gale_ryser_theorem(p1, p2, algorithm="gale",
340
340
k = i + 1
341
341
while k < m and r [i ] == r [k ]:
342
342
k += 1
343
- if t >= k - i : # == number rows of the same length
343
+ if t >= k - i : # == number rows of the same length
344
344
for j in range (i , k ):
345
345
r [j ] -= 1
346
346
c [j ] = 1
347
347
t -= k - i
348
- else : # Remove the t last rows of that length
348
+ else : # Remove the t last rows of that length
349
349
for j in range (k - t , k ):
350
350
r [j ] -= 1
351
351
c [j ] = 1
@@ -366,17 +366,17 @@ def gale_ryser_theorem(p1, p2, algorithm="gale",
366
366
k1 , k2 = len (p1 ), len (p2 )
367
367
p = MixedIntegerLinearProgram (solver = solver )
368
368
b = p .new_variable (binary = True )
369
- for (i ,c ) in enumerate (p1 ):
370
- p .add_constraint (p .sum ([b [i ,j ] for j in range (k2 )]) == c )
371
- for (i ,c ) in enumerate (p2 ):
372
- p .add_constraint (p .sum ([b [j ,i ] for j in range (k1 )]) == c )
369
+ for (i , c ) in enumerate (p1 ):
370
+ p .add_constraint (p .sum ([b [i , j ] for j in range (k2 )]) == c )
371
+ for (i , c ) in enumerate (p2 ):
372
+ p .add_constraint (p .sum ([b [j , i ] for j in range (k1 )]) == c )
373
373
p .set_objective (None )
374
374
p .solve ()
375
375
b = p .get_values (b , convert = ZZ , tolerance = integrality_tolerance )
376
376
M = [[0 ]* k2 for i in range (k1 )]
377
377
for i in range (k1 ):
378
378
for j in range (k2 ):
379
- M [i ][j ] = b [i ,j ]
379
+ M [i ][j ] = b [i , j ]
380
380
return matrix (M )
381
381
382
382
else :
@@ -780,6 +780,7 @@ def __contains__(self, x):
780
780
return False
781
781
return True
782
782
783
+
783
784
class IntegerVectors_all (UniqueRepresentation , IntegerVectors ):
784
785
"""
785
786
Class of all integer vectors.
@@ -917,11 +918,11 @@ def rank(self, x):
917
918
"""
918
919
if sum (x ) != self .n :
919
920
raise ValueError ("argument is not a member of IntegerVectors({},{})" .format (self .n , None ))
920
-
921
+
921
922
n , k = self .n , len (x )
922
923
923
924
r = binomial (k + n - 1 , n + 1 )
924
-
925
+
925
926
for i in range (k - 1 ):
926
927
k -= 1
927
928
n -= x [i ]
@@ -944,29 +945,29 @@ def unrank(self, x):
944
945
sage: IntegerVectors(n=10).unrank(10)
945
946
[1, 9]
946
947
"""
947
- ptr = 0
948
- rtn = [self .n ]
948
+ ptr = 0
949
+ rtn = [self .n ]
949
950
while self .rank (rtn ) < x :
950
951
rtn .append (0 )
951
952
rtn .pop ()
952
-
953
+
953
954
while True :
954
955
if self .rank (rtn ) < x :
955
- rtn [ptr + 1 ]= rtn [ptr ]
956
- rtn [ptr ]= 0
957
- ptr += 1
956
+ rtn [ptr + 1 ] = rtn [ptr ]
957
+ rtn [ptr ] = 0
958
+ ptr += 1
958
959
elif self .rank (rtn ) > x :
959
- rtn [ptr ]-= 1
960
- rtn [ptr - 1 ]+= 1
960
+ rtn [ptr ] -= 1
961
+ rtn [ptr - 1 ] += 1
961
962
else :
962
963
return self ._element_constructor_ (rtn )
963
964
964
965
def cardinality (self ):
965
966
"""
966
967
Return the cardinality of ``self``.
967
-
968
+
968
969
EXAMPLES::
969
-
970
+
970
971
sage: IntegerVectors(n=0).cardinality()
971
972
1
972
973
sage: IntegerVectors(n=10).cardinality()
@@ -977,6 +978,7 @@ def cardinality(self):
977
978
else :
978
979
return PlusInfinity ()
979
980
981
+
980
982
class IntegerVectors_k (UniqueRepresentation , IntegerVectors ):
981
983
"""
982
984
Integer vectors of length `k`.
@@ -1068,16 +1070,16 @@ def rank(self, x):
1068
1070
raise ValueError ("argument is not a member of IntegerVectors({},{})" .format (None , self .k ))
1069
1071
1070
1072
n , k = sum (x ), self .k
1071
-
1073
+
1072
1074
r = sum (binomial (k + i - 1 , k - 1 ) for i in range (n ))
1073
-
1075
+
1074
1076
for i in range (k - 1 ):
1075
1077
k -= 1
1076
1078
n -= x [i ]
1077
1079
r += binomial (k + n - 1 , n - 1 )
1078
1080
1079
1081
return r
1080
-
1082
+
1081
1083
def unrank (self , x ):
1082
1084
"""
1083
1085
Return the element at given rank x.
@@ -1097,28 +1099,28 @@ def unrank(self, x):
1097
1099
raise IndexError (f"Index { x } is out of range for the IntegerVector." )
1098
1100
else :
1099
1101
n , ptr = 0 , 0
1100
- rtn = [0 ]* self .k
1102
+ rtn = [0 ]* self .k
1101
1103
while self .rank (rtn ) <= x :
1102
- n += 1
1103
- rtn [ptr ]= n
1104
- rtn [ptr ]-= 1
1104
+ n += 1
1105
+ rtn [ptr ] = n
1106
+ rtn [ptr ] -= 1
1105
1107
while True :
1106
1108
if self .rank (rtn ) < x :
1107
- rtn [ptr + 1 ]= rtn [ptr ]
1108
- rtn [ptr ]= 0
1109
- ptr += 1
1109
+ rtn [ptr + 1 ] = rtn [ptr ]
1110
+ rtn [ptr ] = 0
1111
+ ptr += 1
1110
1112
elif self .rank (rtn ) > x :
1111
- rtn [ptr ]-= 1
1112
- rtn [ptr - 1 ]+= 1
1113
+ rtn [ptr ] -= 1
1114
+ rtn [ptr - 1 ] += 1
1113
1115
else :
1114
1116
return self ._element_constructor_ (rtn )
1115
-
1117
+
1116
1118
def cardinality (self ):
1117
1119
"""
1118
1120
Return the cardinality of ``self``.
1119
-
1121
+
1120
1122
EXAMPLES::
1121
-
1123
+
1122
1124
sage: IntegerVectors(k=0).cardinality()
1123
1125
1
1124
1126
sage: IntegerVectors(k=10).cardinality()
@@ -1127,8 +1129,9 @@ def cardinality(self):
1127
1129
if self .k == 0 :
1128
1130
return Integer (1 )
1129
1131
else :
1130
- return PlusInfinity ()
1131
-
1132
+ return PlusInfinity ()
1133
+
1134
+
1132
1135
class IntegerVectors_nk (UniqueRepresentation , IntegerVectors ):
1133
1136
"""
1134
1137
Integer vectors of length `k` that sum to `n`.
@@ -1170,11 +1173,11 @@ def _list_rec(self, n, k):
1170
1173
res = []
1171
1174
1172
1175
if k == 1 :
1173
- return [ (n , ) ]
1176
+ return [(n , )]
1174
1177
1175
1178
for nbar in range (n + 1 ):
1176
1179
n_diff = n - nbar
1177
- for rest in self ._list_rec ( nbar , k - 1 ):
1180
+ for rest in self ._list_rec (nbar , k - 1 ):
1178
1181
res .append ((n_diff ,) + rest )
1179
1182
return res
1180
1183
@@ -1342,24 +1345,24 @@ def unrank(self, x):
1342
1345
if x >= len (self ):
1343
1346
raise IndexError (f"Index { x } is out of range for the IntegerVector." )
1344
1347
else :
1345
- ptr = 0
1346
- rtn = [0 ]* self .k
1347
- rtn [ptr ]= self .n
1348
+ ptr = 0
1349
+ rtn = [0 ]* self .k
1350
+ rtn [ptr ] = self .n
1348
1351
while True :
1349
- if self .rank (rtn )< x :
1350
- rtn [ptr + 1 ]= rtn [ptr ]
1351
- rtn [ptr ]= 0
1352
- ptr += 1
1353
- elif self .rank (rtn )> x :
1354
- rtn [ptr ]-= 1
1355
- rtn [ptr - 1 ]+= 1
1352
+ if self .rank (rtn ) < x :
1353
+ rtn [ptr + 1 ] = rtn [ptr ]
1354
+ rtn [ptr ] = 0
1355
+ ptr += 1
1356
+ elif self .rank (rtn ) > x :
1357
+ rtn [ptr ] -= 1
1358
+ rtn [ptr - 1 ] += 1
1356
1359
else :
1357
1360
return self ._element_constructor_ (rtn )
1358
-
1361
+
1359
1362
def cardinality (self ):
1360
1363
"""
1361
1364
Return the cardinality of ``self``.
1362
-
1365
+
1363
1366
EXAMPLES::
1364
1367
1365
1368
sage: IntegerVectors(3,5).cardinality()
@@ -1372,6 +1375,7 @@ def cardinality(self):
1372
1375
n , k = self .n , self .k
1373
1376
return Integer (binomial (n + k - 1 , n ))
1374
1377
1378
+
1375
1379
class IntegerVectors_nnondescents (UniqueRepresentation , IntegerVectors ):
1376
1380
r"""
1377
1381
Integer vectors graded by two parameters.
@@ -1522,16 +1526,16 @@ def __init__(self, n=None, k=None, **constraints):
1522
1526
del constraints ['inner' ]
1523
1527
self .constraints = constraints
1524
1528
1525
- if n is not None :
1529
+ if n is not None :
1526
1530
if k is not None or 'max_length' in constraints :
1527
1531
category = FiniteEnumeratedSets ()
1528
1532
else :
1529
1533
category = EnumeratedSets ()
1530
- elif k is not None and 'max_part' in constraints : # n is None
1534
+ elif k is not None and 'max_part' in constraints : # n is None
1531
1535
category = FiniteEnumeratedSets ()
1532
1536
else :
1533
1537
category = EnumeratedSets ()
1534
- IntegerVectors .__init__ (self , category = category ) # placeholder category
1538
+ IntegerVectors .__init__ (self , category = category ) # placeholder category
1535
1539
1536
1540
def _repr_ (self ):
1537
1541
"""
0 commit comments