Skip to content

Commit 4a8454f

Browse files
committed
store inverse of permutation
1 parent 7df0790 commit 4a8454f

File tree

1 file changed

+44
-41
lines changed

1 file changed

+44
-41
lines changed

src/sage/geometry/lattice_polytope.py

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3210,54 +3210,54 @@ def index_of_max(iterable):
32103210
return m
32113211

32123212
n_s = 1
3213-
permutations = {0 : [S_f.one(), S_v.one()]}
3213+
permutations_inv = {0 : [S_f.one(), S_v.one()]}
32143214
for j in range(n_v):
32153215
m = index_of_max([PM[0][i] for i in range(j, n_v)])
32163216
if m > 0:
3217-
permutations[0][1] = PGE(S_v, j + 1, m + j + 1) * permutations[0][1]
3217+
permutations_inv[0][1] = permutations_inv[0][1] * PGE(S_v, j + 1, m + j + 1)
32183218
first_row = list(PM[0])
32193219

32203220
# Arrange other rows one by one and compare with first row
32213221
for k in range(1, n_f):
32223222
# Error for k == 1 already!
3223-
permutations[n_s] = [S_f.one(), S_v.one()]
3224-
m = index_of_max(PM[k, tuple((permutations[n_s][1].inverse())(j+1)-1 for j in range(n_v))])
3223+
permutations_inv[n_s] = [S_f.one(), S_v.one()]
3224+
m = index_of_max(PM[k, tuple((permutations_inv[n_s][1])(j+1)-1 for j in range(n_v))])
32253225
if m > 0:
3226-
permutations[n_s][1] = PGE(S_v, 1, m+1) * permutations[n_s][1]
3227-
d = (PM[k, (permutations[n_s][1].inverse())(1)-1]
3228-
- permutations[0][1](first_row)[0])
3226+
permutations_inv[n_s][1] = permutations_inv[n_s][1] * PGE(S_v, 1, m+1)
3227+
d = (PM[k, (permutations_inv[n_s][1])(1)-1]
3228+
- (permutations_inv[0][1].inverse())(first_row)[0])
32293229
if d < 0:
32303230
# The largest elt of this row is smaller than largest elt
32313231
# in 1st row, so nothing to do
32323232
continue
32333233
# otherwise:
32343234
for i in range(1, n_v):
3235-
m = index_of_max(PM[k, tuple((permutations[n_s][1].inverse())(j+1)-1 for j in range(i,n_v))])
3235+
m = index_of_max(PM[k, tuple((permutations_inv[n_s][1])(j+1)-1 for j in range(i,n_v))])
32363236
if m > 0:
3237-
permutations[n_s][1] = PGE(S_v, i + 1, m + i + 1) \
3238-
* permutations[n_s][1]
3237+
permutations_inv[n_s][1] = permutations_inv[n_s][1] \
3238+
* PGE(S_v, i + 1, m + i + 1)
32393239
if d == 0:
3240-
d = (PM[k, (permutations[n_s][1].inverse())(i+1)-1]
3241-
-permutations[0][1](first_row)[i])
3240+
d = (PM[k, (permutations_inv[n_s][1])(i+1)-1]
3241+
-(permutations_inv[0][1].inverse())(first_row)[i])
32423242
if d < 0:
32433243
break
32443244
if d < 0:
32453245
# This row is smaller than 1st row, so nothing to do
3246-
del permutations[n_s]
3246+
del permutations_inv[n_s]
32473247
continue
3248-
permutations[n_s][0] = PGE(S_f, 1, k + 1) * permutations[n_s][0]
3248+
permutations_inv[n_s][0] = permutations_inv[n_s][0] * PGE(S_f, 1, k + 1)
32493249
if d == 0:
32503250
# This row is the same, so we have a symmetry!
32513251
n_s += 1
32523252
else:
32533253
# This row is larger, so it becomes the first row and
32543254
# the symmetries reset.
32553255
first_row = list(PM[k])
3256-
permutations = {0: permutations[n_s]}
3256+
permutations_inv = {0: permutations_inv[n_s]}
32573257
n_s = 1
3258-
permutations = {k:permutations[k] for k in permutations if k < n_s}
3258+
permutations_inv = {k:permutations_inv[k] for k in permutations_inv if k < n_s}
32593259

3260-
b = tuple(PM[(permutations[0][0].inverse())(1)-1, (permutations[0][1].inverse())(j+1)-1] for j in range(n_v))
3260+
b = tuple(PM[(permutations_inv[0][0])(1)-1, (permutations_inv[0][1])(j+1)-1] for j in range(n_v))
32613261
# Work out the restrictions the current permutations
32623262
# place on other permutations as a automorphisms
32633263
# of the first row
@@ -3275,7 +3275,7 @@ def index_of_max(iterable):
32753275
# We determine the other rows of PM_max in turn by use of perms and
32763276
# aut on previous rows.
32773277
for l in range(1, n_f - 1):
3278-
n_s = len(permutations)
3278+
n_s = len(permutations_inv)
32793279
n_s_bar = n_s
32803280
cf = 0
32813281
l_r = [0]*n_v
@@ -3285,42 +3285,42 @@ def index_of_max(iterable):
32853285
# number of local permutations associated with current global
32863286
n_p = 0
32873287
ccf = cf
3288-
permutations_bar = {0: copy(permutations[k])}
3288+
permutations_inv_bar = {0: copy(permutations_inv[k])}
32893289
# We look for the line with the maximal entry in the first
32903290
# subsymmetry block, i.e. we are allowed to swap elements
32913291
# between 0 and S(0)
32923292
for s in range(l, n_f):
32933293
for j in range(1, S[0]):
3294-
v = tuple(PM[(permutations_bar[n_p][0].inverse())(s+1)-1, (permutations_bar[n_p][1].inverse())(j+1)-1] for j in range(n_v))
3294+
v = tuple(PM[(permutations_inv_bar[n_p][0])(s+1)-1, (permutations_inv_bar[n_p][1])(j+1)-1] for j in range(n_v))
32953295
if v[0] < v[j]:
3296-
permutations_bar[n_p][1] = PGE(S_v, 1, j + 1) * permutations_bar[n_p][1]
3296+
permutations_inv_bar[n_p][1] = permutations_inv_bar[n_p][1] * PGE(S_v, 1, j + 1)
32973297
if ccf == 0:
3298-
l_r[0] = PM[(permutations_bar[n_p][0].inverse())(s+1)-1, (permutations_bar[n_p][1].inverse())(1)-1]
3299-
permutations_bar[n_p][0] = PGE(S_f, l + 1, s + 1) * permutations_bar[n_p][0]
3298+
l_r[0] = PM[(permutations_inv_bar[n_p][0])(s+1)-1, (permutations_inv_bar[n_p][1])(1)-1]
3299+
permutations_inv_bar[n_p][0] = permutations_inv_bar[n_p][0] * PGE(S_f, l + 1, s + 1)
33003300
n_p += 1
33013301
ccf = 1
3302-
permutations_bar[n_p] = copy(permutations[k])
3302+
permutations_inv_bar[n_p] = copy(permutations_inv[k])
33033303
else:
3304-
d1 = PM[(permutations_bar[n_p][0].inverse())(s+1)-1, (permutations_bar[n_p][1].inverse())(1)-1]
3304+
d1 = PM[(permutations_inv_bar[n_p][0])(s+1)-1, (permutations_inv_bar[n_p][1])(1)-1]
33053305
d = d1 - l_r[0]
33063306
if d < 0:
33073307
# We move to the next line
33083308
continue
33093309
elif d==0:
33103310
# Maximal values agree, so possible symmetry
3311-
permutations_bar[n_p][0] = PGE(S_f, l + 1, s + 1) * permutations_bar[n_p][0]
3311+
permutations_inv_bar[n_p][0] = permutations_inv_bar[n_p][0] * PGE(S_f, l + 1, s + 1)
33123312
n_p += 1
3313-
permutations_bar[n_p] = copy(permutations[k])
3313+
permutations_inv_bar[n_p] = copy(permutations_inv[k])
33143314
else:
33153315
# We found a greater maximal value for first entry.
33163316
# It becomes our new reference:
33173317
l_r[0] = d1
3318-
permutations_bar[n_p][0] = PGE(S_f, l + 1, s + 1) * permutations_bar[n_p][0]
3318+
permutations_inv_bar[n_p][0] = permutations_inv_bar[n_p][0] * PGE(S_f, l + 1, s + 1)
33193319
# Forget previous work done
33203320
cf = 0
3321-
permutations_bar = {0:copy(permutations_bar[n_p])}
3321+
permutations_inv_bar = {0:copy(permutations_inv_bar[n_p])}
33223322
n_p = 1
3323-
permutations_bar[n_p] = copy(permutations[k])
3323+
permutations_inv_bar[n_p] = copy(permutations_inv[k])
33243324
n_s = k + 1
33253325
# Check if the permutations found just now work
33263326
# with other elements
@@ -3337,20 +3337,20 @@ def index_of_max(iterable):
33373337
s -= 1
33383338
# Find the largest value in this symmetry block
33393339
for j in range(c + 1, h):
3340-
v = tuple(PM[(permutations_bar[s][0].inverse())(l+1)-1, (permutations_bar[s][1].inverse())(j+1)-1] for j in range(n_v))
3340+
v = tuple(PM[(permutations_inv_bar[s][0])(l+1)-1, (permutations_inv_bar[s][1])(j+1)-1] for j in range(n_v))
33413341
if (v[c] < v[j]):
3342-
permutations_bar[s][1] = PGE(S_v, c + 1, j + 1) * permutations_bar[s][1]
3342+
permutations_inv_bar[s][1] = permutations_inv_bar[s][1] * PGE(S_v, c + 1, j + 1)
33433343
if ccf == 0:
33443344
# Set reference and carry on to next permutation
3345-
l_r[c] = PM[(permutations_bar[s][0].inverse())(l+1)-1, (permutations_bar[s][1].inverse())(c+1)-1]
3345+
l_r[c] = PM[(permutations_inv_bar[s][0])(l+1)-1, (permutations_inv_bar[s][1])(c+1)-1]
33463346
ccf = 1
33473347
else:
3348-
d1 = PM[(permutations_bar[s][0].inverse())(l+1)-1, (permutations_bar[s][1].inverse())(c+1)-1]
3348+
d1 = PM[(permutations_inv_bar[s][0])(l+1)-1, (permutations_inv_bar[s][1])(c+1)-1]
33493349
d = d1 - l_r[c]
33503350
if d < 0:
33513351
n_p -= 1
33523352
if s < n_p:
3353-
permutations_bar[s] = copy(permutations_bar[n_p])
3353+
permutations_inv_bar[s] = copy(permutations_inv_bar[n_p])
33543354
elif d > 0:
33553355
# The current case leads to a smaller matrix,
33563356
# hence this case becomes our new reference
@@ -3360,21 +3360,21 @@ def index_of_max(iterable):
33603360
n_s = k + 1
33613361
# Update permutations
33623362
if (n_s - 1) > k:
3363-
permutations[k] = copy(permutations[n_s - 1])
3363+
permutations_inv[k] = copy(permutations_inv[n_s - 1])
33643364
n_s -= 1
33653365
for s in range(n_p):
3366-
permutations[n_s] = copy(permutations_bar[s])
3366+
permutations_inv[n_s] = copy(permutations_inv_bar[s])
33673367
n_s += 1
33683368
cf = n_s
3369-
permutations = {k: permutations[k] for k in permutations if k < n_s}
3369+
permutations_inv = {k: permutations_inv[k] for k in permutations_inv if k < n_s}
33703370
# If the automorphisms are not already completely restricted,
33713371
# update them
33723372
if S != list(range(1, n_v + 1)):
33733373
# Take the old automorphisms and update by
33743374
# the restrictions the last worked out
33753375
# row imposes.
33763376
c = 0
3377-
M = tuple(PM[(permutations[0][0].inverse())(l+1)-1, (permutations[0][1].inverse())(j+1)-1] for j in range(n_v))
3377+
M = tuple(PM[(permutations_inv[0][0])(l+1)-1, (permutations_inv[0][1])(j+1)-1] for j in range(n_v))
33783378
while c < n_v:
33793379
s = S[c] + 1
33803380
S[c] = c + 1
@@ -3387,9 +3387,12 @@ def index_of_max(iterable):
33873387
S[c] = c + 1
33883388
c += 1
33893389
# Now we have the perms, we construct PM_max using one of them
3390-
PM_max = PM.with_permuted_rows_and_columns(*permutations[0])
3390+
PM_max = PM.with_permuted_rows_and_columns(permutations_inv[0][0].inverse(),permutations_inv[0][1].inverse())
33913391
if check:
3392-
return (PM_max, permutations)
3392+
for p in permutations_inv.keys():
3393+
permutaiton_inv[p][0] = permutaiton_inv[p][0].inverse()
3394+
permutaiton_inv[p][1] = permutaiton_inv[p][1].inverse()
3395+
return (PM_max, permutations_inv)
33933396
else:
33943397
return PM_max
33953398

0 commit comments

Comments
 (0)