Re: Permutations
- To: mathgroup at smc.vnet.net
- Subject: [mg65564] Re: Permutations
- From: Paul Abbott <paul at physics.uwa.edu.au>
- Date: Fri, 7 Apr 2006 06:14:43 -0400 (EDT)
- Organization: The University of Western Australia
- References: <e12s2k$j79$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In article <e12s2k$j79$1 at smc.vnet.net>, "King, Peter R" <peter.king at imperial.ac.uk> 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]]]] Tests: 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} Cheers, Paul _______________________________________________________________________ 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) AUSTRALIA http://physics.uwa.edu.au/~paul