Advertisement
mate2code

Inverse permutation giving negative results in Python

Nov 15th, 2016
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.67 KB | None | 0 0
  1. # The inverse ``~p`` of a SymPy permutation ``p`` gives the expected result when accessed like ``i^~p``, but ``~p(i) gives a negative result. This script converts these weird results into normal permutations, and shows the resulting pattern.
  2. # https://en.wikiversity.org/wiki/Permutation_notation#SymPy
  3.  
  4. from itertools import permutations
  5. from sympy.combinatorics import Permutation
  6. from bidict import bidict
  7. from math import factorial
  8.  
  9. for n in range(2, 6):
  10.     print('\n============================== ', n, ' ==============================\n')
  11.     fact_n = factorial(n)
  12.  
  13.     perms_iter = permutations([i for i in range(n)])
  14.     perms = bidict()
  15.  
  16.     for i, perm in enumerate(perms_iter):
  17.         perms[fact_n-1-i] = Permutation(perm[::-1])  # create reverse colexicographic order
  18.  
  19.     converted_list = []
  20.  
  21.     for index1 in range(fact_n):
  22.         perm1 = perms[index1]
  23.         for index2 in range(fact_n):
  24.             perm2 = perms[index2]
  25.             if ~perm2 == perm1:
  26.                 weird_perm = [~perm2(i) for i in range(n)]  # the result with negative numbers
  27.                 converted_perm = [n+weird_perm[i] for i in range(n)]
  28.                 converted_perm_object = Permutation(converted_perm)
  29.                 for index3 in range(fact_n):
  30.                     perm3 = perms[index3]
  31.                     if perm3 == converted_perm_object:
  32.                         converted_list.append(index3)
  33.                         print(index1, converted_perm, index3)
  34.  
  35.     print('\n', converted_list)
  36.  
  37.  
  38. """
  39. ==============================  2  ==============================
  40.  
  41. 0 [1, 0] 1
  42. 1 [0, 1] 0
  43.  
  44. [1, 0]
  45.  
  46. ==============================  3  ==============================
  47.  
  48. 0 [2, 1, 0] 5
  49. 1 [1, 2, 0] 4
  50. 2 [2, 0, 1] 3
  51. 3 [1, 0, 2] 1
  52. 4 [0, 2, 1] 2
  53. 5 [0, 1, 2] 0
  54.  
  55. [5, 4, 3, 1, 2, 0]
  56.  
  57. ==============================  4  ==============================
  58.  
  59. 0 [3, 2, 1, 0] 23
  60. 1 [2, 3, 1, 0] 22
  61. 2 [3, 1, 2, 0] 21
  62. 3 [2, 1, 3, 0] 19
  63. 4 [1, 3, 2, 0] 20
  64. 5 [1, 2, 3, 0] 18
  65. 6 [3, 2, 0, 1] 17
  66. 7 [2, 3, 0, 1] 16
  67. 8 [3, 1, 0, 2] 11
  68. 9 [2, 1, 0, 3] 5
  69. 10 [1, 3, 0, 2] 10
  70. 11 [1, 2, 0, 3] 4
  71. 12 [3, 0, 2, 1] 15
  72. 13 [2, 0, 3, 1] 13
  73. 14 [3, 0, 1, 2] 9
  74. 15 [2, 0, 1, 3] 3
  75. 16 [1, 0, 3, 2] 7
  76. 17 [1, 0, 2, 3] 1
  77. 18 [0, 3, 2, 1] 14
  78. 19 [0, 2, 3, 1] 12
  79. 20 [0, 3, 1, 2] 8
  80. 21 [0, 2, 1, 3] 2
  81. 22 [0, 1, 3, 2] 6
  82. 23 [0, 1, 2, 3] 0
  83.  
  84. [23, 22, 21, 19, 20, 18, 17, 16, 11, 5, 10, 4, 15, 13, 9, 3, 7, 1, 14, 12, 8, 2, 6, 0]
  85.  
  86. ==============================  5  ==============================
  87.  
  88. 0 [4, 3, 2, 1, 0] 119
  89. 1 [3, 4, 2, 1, 0] 118
  90. 2 [4, 2, 3, 1, 0] 117
  91. 3 [3, 2, 4, 1, 0] 115
  92. 4 [2, 4, 3, 1, 0] 116
  93. 5 [2, 3, 4, 1, 0] 114
  94. 6 [4, 3, 1, 2, 0] 113
  95. 7 [3, 4, 1, 2, 0] 112
  96. 8 [4, 2, 1, 3, 0] 107
  97. 9 [3, 2, 1, 4, 0] 101
  98. 10 [2, 4, 1, 3, 0] 106
  99. 11 [2, 3, 1, 4, 0] 100
  100. 12 [4, 1, 3, 2, 0] 111
  101. 13 [3, 1, 4, 2, 0] 109
  102. 14 [4, 1, 2, 3, 0] 105
  103. 15 [3, 1, 2, 4, 0] 99
  104. 16 [2, 1, 4, 3, 0] 103
  105. 17 [2, 1, 3, 4, 0] 97
  106. 18 [1, 4, 3, 2, 0] 110
  107. 19 [1, 3, 4, 2, 0] 108
  108. 20 [1, 4, 2, 3, 0] 104
  109. 21 [1, 3, 2, 4, 0] 98
  110. 22 [1, 2, 4, 3, 0] 102
  111. 23 [1, 2, 3, 4, 0] 96
  112. 24 [4, 3, 2, 0, 1] 95
  113. 25 [3, 4, 2, 0, 1] 94
  114. 26 [4, 2, 3, 0, 1] 93
  115. 27 [3, 2, 4, 0, 1] 91
  116. 28 [2, 4, 3, 0, 1] 92
  117. 29 [2, 3, 4, 0, 1] 90
  118. 30 [4, 3, 1, 0, 2] 71
  119. 31 [3, 4, 1, 0, 2] 70
  120. 32 [4, 2, 1, 0, 3] 47
  121. 33 [3, 2, 1, 0, 4] 23
  122. 34 [2, 4, 1, 0, 3] 46
  123. 35 [2, 3, 1, 0, 4] 22
  124. 36 [4, 1, 3, 0, 2] 69
  125. 37 [3, 1, 4, 0, 2] 67
  126. 38 [4, 1, 2, 0, 3] 45
  127. 39 [3, 1, 2, 0, 4] 21
  128. 40 [2, 1, 4, 0, 3] 43
  129. 41 [2, 1, 3, 0, 4] 19
  130. 42 [1, 4, 3, 0, 2] 68
  131. 43 [1, 3, 4, 0, 2] 66
  132. 44 [1, 4, 2, 0, 3] 44
  133. 45 [1, 3, 2, 0, 4] 20
  134. 46 [1, 2, 4, 0, 3] 42
  135. 47 [1, 2, 3, 0, 4] 18
  136. 48 [4, 3, 0, 2, 1] 89
  137. 49 [3, 4, 0, 2, 1] 88
  138. 50 [4, 2, 0, 3, 1] 83
  139. 51 [3, 2, 0, 4, 1] 77
  140. 52 [2, 4, 0, 3, 1] 82
  141. 53 [2, 3, 0, 4, 1] 76
  142. 54 [4, 3, 0, 1, 2] 65
  143. 55 [3, 4, 0, 1, 2] 64
  144. 56 [4, 2, 0, 1, 3] 41
  145. 57 [3, 2, 0, 1, 4] 17
  146. 58 [2, 4, 0, 1, 3] 40
  147. 59 [2, 3, 0, 1, 4] 16
  148. 60 [4, 1, 0, 3, 2] 59
  149. 61 [3, 1, 0, 4, 2] 53
  150. 62 [4, 1, 0, 2, 3] 35
  151. 63 [3, 1, 0, 2, 4] 11
  152. 64 [2, 1, 0, 4, 3] 29
  153. 65 [2, 1, 0, 3, 4] 5
  154. 66 [1, 4, 0, 3, 2] 58
  155. 67 [1, 3, 0, 4, 2] 52
  156. 68 [1, 4, 0, 2, 3] 34
  157. 69 [1, 3, 0, 2, 4] 10
  158. 70 [1, 2, 0, 4, 3] 28
  159. 71 [1, 2, 0, 3, 4] 4
  160. 72 [4, 0, 3, 2, 1] 87
  161. 73 [3, 0, 4, 2, 1] 85
  162. 74 [4, 0, 2, 3, 1] 81
  163. 75 [3, 0, 2, 4, 1] 75
  164. 76 [2, 0, 4, 3, 1] 79
  165. 77 [2, 0, 3, 4, 1] 73
  166. 78 [4, 0, 3, 1, 2] 63
  167. 79 [3, 0, 4, 1, 2] 61
  168. 80 [4, 0, 2, 1, 3] 39
  169. 81 [3, 0, 2, 1, 4] 15
  170. 82 [2, 0, 4, 1, 3] 37
  171. 83 [2, 0, 3, 1, 4] 13
  172. 84 [4, 0, 1, 3, 2] 57
  173. 85 [3, 0, 1, 4, 2] 51
  174. 86 [4, 0, 1, 2, 3] 33
  175. 87 [3, 0, 1, 2, 4] 9
  176. 88 [2, 0, 1, 4, 3] 27
  177. 89 [2, 0, 1, 3, 4] 3
  178. 90 [1, 0, 4, 3, 2] 55
  179. 91 [1, 0, 3, 4, 2] 49
  180. 92 [1, 0, 4, 2, 3] 31
  181. 93 [1, 0, 3, 2, 4] 7
  182. 94 [1, 0, 2, 4, 3] 25
  183. 95 [1, 0, 2, 3, 4] 1
  184. 96 [0, 4, 3, 2, 1] 86
  185. 97 [0, 3, 4, 2, 1] 84
  186. 98 [0, 4, 2, 3, 1] 80
  187. 99 [0, 3, 2, 4, 1] 74
  188. 100 [0, 2, 4, 3, 1] 78
  189. 101 [0, 2, 3, 4, 1] 72
  190. 102 [0, 4, 3, 1, 2] 62
  191. 103 [0, 3, 4, 1, 2] 60
  192. 104 [0, 4, 2, 1, 3] 38
  193. 105 [0, 3, 2, 1, 4] 14
  194. 106 [0, 2, 4, 1, 3] 36
  195. 107 [0, 2, 3, 1, 4] 12
  196. 108 [0, 4, 1, 3, 2] 56
  197. 109 [0, 3, 1, 4, 2] 50
  198. 110 [0, 4, 1, 2, 3] 32
  199. 111 [0, 3, 1, 2, 4] 8
  200. 112 [0, 2, 1, 4, 3] 26
  201. 113 [0, 2, 1, 3, 4] 2
  202. 114 [0, 1, 4, 3, 2] 54
  203. 115 [0, 1, 3, 4, 2] 48
  204. 116 [0, 1, 4, 2, 3] 30
  205. 117 [0, 1, 3, 2, 4] 6
  206. 118 [0, 1, 2, 4, 3] 24
  207. 119 [0, 1, 2, 3, 4] 0
  208.  
  209. [119, 118, 117, 115, 116, 114, 113, 112, 107, 101, 106, 100, 111, 109, 105, 99, 103, 97, 110, 108, 104, 98, 102, 96, 95, 94, 93, 91, 92, 90, 71, 70, 47, 23, 46, 22, 69, 67, 45, 21, 43, 19, 68, 66, 44, 20, 42, 18, 89, 88, 83, 77, 82, 76, 65, 64, 41, 17, 40, 16, 59, 53, 35, 11, 29, 5, 58, 52, 34, 10, 28, 4, 87, 85, 81, 75, 79, 73, 63, 61, 39, 15, 37, 13, 57, 51, 33, 9, 27, 3, 55, 49, 31, 7, 25, 1, 86, 84, 80, 74, 78, 72, 62, 60, 38, 14, 36, 12, 56, 50, 32, 8, 26, 2, 54, 48, 30, 6, 24, 0]
  210. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement