(* ::Package:: *)

(************************************************************************)
(* This file was generated automatically by the Mathematica front end.  *)
(* It contains Initialization cells from a Notebook file, which         *)
(* typically will have the same name as this file except ending in      *)
(* ".nb" instead of ".m".                                               *)
(*                                                                      *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs.  Doing so is equivalent   *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end.                                                                 *)
(*                                                                      *)
(* DO NOT EDIT THIS FILE.  This entire file is regenerated              *)
(* automatically each time the parent Notebook file is saved in the     *)
(* Mathematica front end.  Any changes you make to this file will be    *)
(* overwritten.                                                         *)
(************************************************************************)



(* ::Input::Initialization:: *)
BeginPackage["Perm`"]


(* ::Input::Initialization:: *)
Perm::usage = "Perm[2,5,6,4] means the permutation that maps 1\[Rule]2, 2\[Rule]5, 3\[Rule]6, 4\[Rule]4. \n Perm[2,3,1\!\(\*SuperscriptBox[\(]\), \(-1\)]\) return the inverse of Perm[2,3,1]. \n Perm[c_Cycles, n_Integer]: converts a permutaion in cycle form to a permutation with head Perm where n is the size of the set being permutated. "


(* ::Input::Initialization:: *)
IdentityPermutation::usage="IdentityPermutation[n_Integer] returns the identity permutation."


(* ::Input::Initialization:: *)
PermutationsQ::usage="PermutationsQ[\[Sigma]_Perm] gives True if \[Sigma] is a permutation, and False otherwise."


(* ::Input::Initialization:: *)
Pivot::usage="Pivot[\[Sigma]_Perm] returns the minimum of the element moved by \[Sigma]. This element is called the pivot of \[Sigma]."


(* ::Input::Initialization:: *)
ElementsMoved::usage= "ElementsMoved[\[Sigma]_Perm] returns the elements moved by \[Sigma] "


(* ::Input::Initialization:: *)
Cycle::usage="Cycle[p_Perm] converts permutaions with head Perm to cycle form with head Cycles."


(* ::Input::Initialization:: *)
Begin["`private`"]


(* ::Input::Initialization:: *)
PermutationsQ[\[Sigma]_Perm]:=Sort[List@@\[Sigma]]=== Range[Length[\[Sigma]]];


(* ::Input::Initialization:: *)
\[Sigma]_Perm \[SmallCircle] \[Tau]_Perm /; Length[\[Sigma]] == Length[\[Tau]]:= \[Sigma][[List@@\[Tau]]];


(* ::Input::Initialization:: *)
Perm/:(\[Sigma]_Perm)^-1/;PermutationsQ[\[Sigma]] := (
\[Tau] = \[Sigma];
\[Tau][[\[Sigma][[#]]]]&/@Perm@@Range[Length[\[Sigma]]]
)


(* ::Input::Initialization:: *)
IdentityPermutation[n_Integer]:= Perm@@Range[n]


(* ::Input::Initialization:: *)
Perm[Cycles[{{}}],n_Integer]:= IdentityPermutation[n]
Perm[c_Cycles, n_Integer]:= Module[{s,v},
s = List@@c[[1]];
v = ConstantArray[0,n];
For[i=1, i<= Length[s], i++,
{If[Position[s[[i]],#][[1,1]]=== Length[s[[i]]],  v[[#]]=s[[i]][[1]], v[[#]] = s[[i]][[Position[s[[i]],#][[1,1]]+1]]] }&/@s[[i]]
];
If[#===0, v[[Position[v, #][[1,1]]]]= Position[v, #][[1,1]]]&/@v;
Return[Perm@@v]
]


(* ::Input::Initialization:: *)
Cycle[p_Perm]/;PermutationQ[\[Sigma]]:= Module[{ls,cyc, tmp},
ls = List@@p;
tmp = ls/. i_Integer:> {Position[ls, i][[1,1]],i}/.{l_,l_}:> {} ;
cyc = tmp//.{r___,{k_,g___, l_},s___, {l_,w___, m_}, t___}:>{r,s,{k,g, l, w, m},t};
Cycles@@{DeleteDuplicates[#]&/@DeleteCases[cyc, {}]}
]


(* ::Input::Initialization:: *)
ElementsMoved[\[Sigma]_Perm]/;PermutationsQ[\[Sigma]]:=(
elmv =  Position[\[Sigma], #]&/@Select[List@@\[Sigma],Position[\[Sigma],#][[1,1]]!= # &]//Flatten;
elmv
)


(* ::Input::Initialization:: *)
Pivot[\[Sigma]_Perm]/;PermutationsQ[\[Sigma]]:=(
spt =  Position[\[Sigma], #]&/@Select[List@@\[Sigma],Position[\[Sigma],#][[1,1]]!= # &]//Flatten;
If[spt!= {}, Return[spt//Min]]
)


(* ::Input::Initialization:: *)
End[];EndPackage[]



