Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RankPermutation::usage = "RankPermutation[p] gives the rank of permutation p in lexicographic order."
- RankPermutation[{1}] := 0
- RankPermutation[{}] := 0
- RankPermutation[p_?PermutationQ] :=
- Block[{$RecursionLimit = Infinity},
- (p[[1]]-1) (Length[Rest[p]]!) +
- RankPermutation[ Map[(If[#>p[[1]], #-1, #])&, Rest[p]]]
- ]
- UP[r_Integer, n_Integer] :=
- Module[{r1 = r, q = n!},
- Table[r1 = Mod[r1, q];
- q = q/(n - i + 1);
- Quotient[r1, q] + 1,
- {i, n}
- ]
- ]
- UnrankPermutation::usage = "UnrankPermutation[r, l] gives the rth permutation in the lexicographic list of permutations of list l. "
- UnrankPermutation[r_Integer, {}] := {}
- UnrankPermutation[r_Integer, l_List] :=
- Module[{s = l, k, t, p = UP[Mod[r, Length[l]!], Length[l]]},
- Table[k = s[[t = p[[i]] ]];
- s = Delete[s, t];
- k,
- {i, Length[ p ]}
- ]
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement