home *** CD-ROM | disk | FTP | other *** search
-
- (* :Name: DiscreteMath`Permutations` *)
-
- (* :Title: Elementary Operations on Permutaions
- *)
-
- (*: Summary: This package contains various functions for doing elementary
- operations on permutations.
- *)
-
- (* :Mathematica Version: 2.0 *)
-
- (* Copyright 1988 Wolfram Research Inc. *)
-
- (** Elementary Operations on Permutations **)
-
- BeginPackage["DiscreteMath`Permutations`"]
-
- PermutationQ::usage =
- "PermutationQ[e] yields True if e is a list representing a permutation."
-
- ToCycles::usage =
- "ToCycles[p] writes the permutation p as a list of cyclic
- permutations."
-
- FromCycles::usage =
- "FromCycles[{p1,p2,..}] gives the permutation that corresponds to
- a list of cycles."
-
- RandomPermutation::usage =
- "RandomPermutation[n] gives a random permutation of n elements."
-
- Ordering::usage =
- "Ordering[list] gives the permutation that puts the elements of
- list in order."
-
- Begin["`private`"]
-
- PermutationQ[e_] := TrueQ[ Sort[e] == Range[Length[e]] ]
-
- (**
- ToCycles[perm_?PermutationQ] :=
- Block[{a, t, n, l, i, len},
- len = Length[perm];
- a = {} ;
- t = Table[True, {len}];
- For[i=1, i<=len, i++,
- If[t[[i]],
- For[n = perm[[i]]; l = {},
- t[[n]],
- n = perm[[n]],
- t[[n]] = False; AppendTo[l, n]
- ];
- AppendTo[a, l]
- ]
- ] ;
- Return[a]
- ]
- **)
-
- ToCycles[perm_List] :=
- Take[#, Position[Rest[#], First[#]] [[1,1]]]& /@
- Last[FoldList[
- If[MemberQ[Flatten[#1], #2],
- #1,
- Append[#1,
- NestList[perm[[#]]&, #2, Length[perm]]]]&,
- {}, perm]]
-
-
- FromCycles[cyc_List] :=
- Last /@ Sort[Transpose[Flatten /@ {RotateRight /@ cyc, cyc}]]
-
-
- RandomPermutation[n_Integer?Positive] :=
- Block[{t},
- t = Array[{Random[], #} &, n];
- t = Sort[t];
- Map[ #[[2]] &, t ]
- ]
-
- Ordering[list_List] :=
- Map[Last, Sort[Transpose[{list, Range[Length[list]]}]]]
-
- End[]
- EndPackage[ ]
-
- Null
-