Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. RankPermutation::usage = "RankPermutation[p] gives the rank of permutation p in lexicographic order."
  2. RankPermutation[{1}] := 0
  3. RankPermutation[{}] := 0
  4. RankPermutation[p_?PermutationQ] :=
  5. Block[{$RecursionLimit = Infinity},
  6. (p[[1]]-1) (Length[Rest[p]]!) +
  7. RankPermutation[ Map[(If[#>p[[1]], #-1, #])&, Rest[p]]]
  8. ]
  9.  
  10. UP[r_Integer, n_Integer] :=
  11. Module[{r1 = r, q = n!},
  12. Table[r1 = Mod[r1, q];
  13. q = q/(n - i + 1);
  14. Quotient[r1, q] + 1,
  15. {i, n}
  16. ]
  17. ]
  18.  
  19. UnrankPermutation::usage = "UnrankPermutation[r, l] gives the rth permutation in the lexicographic list of permutations of list l. "
  20. UnrankPermutation[r_Integer, {}] := {}
  21. UnrankPermutation[r_Integer, l_List] :=
  22. Module[{s = l, k, t, p = UP[Mod[r, Length[l]!], Length[l]]},
  23. Table[k = s[[t = p[[i]] ]];
  24. s = Delete[s, t];
  25. k,
  26. {i, Length[ p ]}
  27. ]
  28. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement