MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Permutations

  • To: mathgroup at
  • Subject: [mg65564] Re: Permutations
  • From: Paul Abbott <paul at>
  • Date: Fri, 7 Apr 2006 06:14:43 -0400 (EDT)
  • Organization: The University of Western Australia
  • References: <e12s2k$j79$>
  • Sender: owner-wri-mathgroup at

In article <e12s2k$j79$1 at>,
 "King, Peter R" <peter.king at> wrote:

> I wish to be able to construct permutations by listing those positions
> that are unaltered, all other pairs swapping. So if I start from
> {a,b,c,d,e,f} and apply the rule {1,4} I get {a,c,b,d,f,e}. Note that
> this is the only kind of permutation that I need. ie there is always at
> least one pair of positions that aren't changed and in all other
> positions neighbouring pairs are swapped (the only other kind of
> permutation is where all pairs cross, I guess I can call this {} and it
> gives {b,a,d,c,f,e}).
> I am sure there is a trivial way of doing this (clearly I can construct
> matrices and do it by multiplication of the vector - or I can extract
> elements and swap the postions "by hand" but these seem unnecessarily
> tedious). Any thoughts out there?

I wonder what the application is? That may give a hint as to the best 
solution. Anyway, here is one solution:

 perm[x_List, {p_, q_}] :=   
   Module[{y = Flatten[Reverse /@ Partition[
     Complement[Range[Length[x]], {p, q}], 2]]},    
       x[[Insert[Insert[y, p, p], q, q]]]]

along with

 perm[x_List, {}]:= x[[Flatten[Reverse/@Partition[Range[Length[x]],2]]]]


 perm[{a,b,c,d,e,f}, {1, 4}]

 {a, c, b, d, f, e}

 perm[{a,b,c,d,e,f}, {2, 5}]

 {c, b, a, f, e, d}

 perm[{a,b,c,d,e,f}, {}]

 {b, a, d, c, f, e}


Paul Abbott                                      Phone:  61 8 6488 2734
School of Physics, M013                            Fax: +61 8 6488 1014
The University of Western Australia         (CRICOS Provider No 00126G)    

  • Prev by Date: Re: ListDensityPlot and GraphicsArray
  • Next by Date: Re: Sphere
  • Previous by thread: Re: Permutations
  • Next by thread: Re: Permutations