Maple: Generating a set of permutations.

209 Views Asked by At

I want to create all permutations of the following expression.

$a_1 a_2 a_3 ..a_n a_n'.. a_3' a_2' a_1'$

Where a variable can only be swapped with it's prime partner.

Since there are n variables each with 2 possible positions I can see that there must be $2^n$ permutations.

I've not used Maple for 5 years so the more detailed the answer the better.

Ideally I would like the output to be a vector as I'm going to be creating a matrix by combining the output of this vector with another vector.

Any help greatly appreciated. Thanks, Ben

1

There are 1 best solutions below

3
On BEST ANSWER

Here is a code fragment to help you get started:

P :=
proc(n)
local res, ind, d, perm;

    res := [];

    for ind from 2^n to 2*2^n-1 do
        d := convert(ind, base, 2)[1..n];

        perm :=
        [seq(`if`(d[p]=0,
                  a[p], b[p]), p=1..n),
         seq(`if`(d[n+1-p]=0,
                  b[n+1-p], a[n+1-p]), p=1..n)];

        res := [op(res), perm];
    od;

    res;
end;

Sample run is

> P(4);
[[a[1], a[2], a[3], a[4], b[4], b[3], b[2], b[1]],

    [b[1], a[2], a[3], a[4], b[4], b[3], b[2], a[1]],

    [a[1], b[2], a[3], a[4], b[4], b[3], a[2], b[1]],

    [b[1], b[2], a[3], a[4], b[4], b[3], a[2], a[1]],

    [a[1], a[2], b[3], a[4], b[4], a[3], b[2], b[1]],

    [b[1], a[2], b[3], a[4], b[4], a[3], b[2], a[1]],

    [a[1], b[2], b[3], a[4], b[4], a[3], a[2], b[1]],

    [b[1], b[2], b[3], a[4], b[4], a[3], a[2], a[1]],

    [a[1], a[2], a[3], b[4], a[4], b[3], b[2], b[1]],

    [b[1], a[2], a[3], b[4], a[4], b[3], b[2], a[1]],

    [a[1], b[2], a[3], b[4], a[4], b[3], a[2], b[1]],

    [b[1], b[2], a[3], b[4], a[4], b[3], a[2], a[1]],

    [a[1], a[2], b[3], b[4], a[4], a[3], b[2], b[1]],

    [b[1], a[2], b[3], b[4], a[4], a[3], b[2], a[1]],

    [a[1], b[2], b[3], b[4], a[4], a[3], a[2], b[1]],

    [b[1], b[2], b[3], b[4], a[4], a[3], a[2], a[1]]]