Advertisement
mate2code

Permutations and inversion related vectors

Dec 14th, 2016
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. # Creating the table of 4-element permutations and all the inversion related vectors in
  2. # https://en.wikiversity.org/wiki/Inversion_(discrete_mathematics)#Example:_All_permutations_of_four_elements
  3.  
  4. from itertools import permutations
  5.  
  6.  
  7. def based_0_or_1(perm):
  8.     # returns 0 if the permutation is 0-based and 1 if it is 1-based
  9.     length = len(perm)
  10.     if sorted(perm) == list(range(length)):
  11.         return 0
  12.     elif sorted(perm) == list(range(1, length+1)):
  13.         return 1
  14.     else:
  15.         print('Input is no 0-based or 1-based permutation.')
  16.  
  17.  
  18. def zeroify(perm):
  19.     # returns a 0-based permutation
  20.     base = based_0_or_1(perm)
  21.     if base == 0:
  22.         return perm
  23.     if base == 1:
  24.         return [i-1 for i in perm]
  25.  
  26.  
  27. def l_inv_count(perm):
  28.     length = len(perm)
  29.     result = [0] * length
  30.     for i in range(length):
  31.         count = 0
  32.         for k in range(i):
  33.             if perm[k] > perm[i]:
  34.                 count += 1
  35.         result[i] = count
  36.     return result
  37.  
  38.  
  39. def r_inv_count(perm):
  40.     length = len(perm)
  41.     result = [0] * length
  42.     for i in range(length):
  43.         count = 0
  44.         for k in range(i+1, length):
  45.             if perm[k] < perm[i]:
  46.                 count += 1
  47.         result[i] = count
  48.     return result
  49.  
  50.  
  51. def inv_vect(perm_in):
  52.     perm = zeroify(perm_in)
  53.     length = len(perm)
  54.     result = [0] * length
  55.     for i in range(length):
  56.         count = 0
  57.         for k in range(i):
  58.             if perm[k] > perm[i]:
  59.                 count += 1
  60.         result[perm[i]] = count
  61.     return result
  62.  
  63.  
  64. def l2s(l):
  65.     # turns [1, 2, 3] to '123'
  66.     s = ''
  67.     for e in l:
  68.         s += str(e)
  69.     return s
  70.  
  71.  
  72. inv = [0, 1, 2, 4, 3, 5, 6, 7, 12, 18, 13, 19, 8, 10, 14, 20, 16, 22, 9, 11, 15, 21, 17, 23]
  73.  
  74.  
  75. perms_iter = permutations([1, 2, 3, 4])
  76. perms_dict = {}
  77.  
  78. for i, perm in enumerate(perms_iter):
  79.     perms_dict[23-i] = perm[::-1]  # create reverse colexicographic order
  80.  
  81.  
  82. def c(abbrs):
  83.     # turns c('rs') to ' ||style="color:red;font-size:50%;"| '
  84.     abbr_to_code = {
  85.         'b': 'font-weight:bold;',
  86.         'x': 'font-size:40%;',
  87.         's': 'font-size:50%;',
  88.         'l': 'font-size:110%;',
  89.         't': 'border-left:3px solid darkgray;',
  90.         'r': 'color:red;'
  91.     }
  92.     res = ' ||style="'
  93.     for abbr in abbrs:
  94.         res += abbr_to_code[abbr]
  95.     res += '"| '
  96.     return res
  97.  
  98.  
  99. table = ''
  100. for i in range(24):
  101.     perm = perms_dict[i]
  102.     p = l2s(perm)
  103.     l = l2s(l_inv_count(perm)[1:])
  104.     r = l2s(r_inv_count(perm)[:-1])
  105.     v = l2s(inv_vect(perm)[:-1])
  106.     invnum = str(sum(inv_vect(perm)))
  107.  
  108.     if i in [1, 2, 5, 6, 14, 21]:
  109.         numcolor = '33eb2a'  # green
  110.     elif i in [9, 10, 13, 17, 18, 22]:
  111.         numcolor = 'ffbb00'  # orange
  112.     else:
  113.         numcolor = 'ffffff'  # white
  114.  
  115.     if i in [0, 7, 16, 23]:
  116.         numweight = 'font-weight:bold;'
  117.     else:
  118.         numweight = ''
  119.  
  120.     start = '\n|- style="height:38px"\n|style="font-size:110%;background-color:#' + numcolor + ';' + numweight \
  121.             + 'letter-spacing:0;"| ' + str(i) \
  122.             + ' || [[File:4-el perm matrix ' + str(i).zfill(2) + '.svg|30px]]'
  123.  
  124.     z = '<span style="opacity: .3;">0</span>'
  125.  
  126.     row = start \
  127.           + c('b') + p + c('bx') + p[::-1] \
  128.           + c('t') + '[[File:4-el perm invset ' + str(inv[i]).zfill(2) + '.svg|55px]]' \
  129.           + c('tr') + v+z + c('rs') + z+v[::-1] \
  130.           + c('trs') + l[::-1]+z + c('r') + z+l \
  131.           + c('t') + '[[File:4-el perm invset ' + str(i).zfill(2) + '.svg|55px]]' \
  132.           + c('tr') + r+z + c('rs') + z+r[::-1] \
  133.           + c('trl') + invnum
  134.  
  135.     table += row
  136.  
  137. print(table)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement