[SciPy-User] permutate elements - unique order

Robert Kern robert.kern at gmail.com
Mon Feb 14 11:18:46 EST 2011


On Mon, Feb 14, 2011 at 09:38, Ferdinand <Ferdinand.Gruebler at gmx.de> wrote:
> Oh sorry, I made a mistake ...
>
> of course abc conflicts with cba
>
> It has to be:
>
>
> function(a,b,c)
>
> a   b   c
> b   c   a
> c   a   b

Ah, you want a Latin Square.

http://en.wikipedia.org/wiki/Latin_square

An easy (but boring) construction is just to shift each row by one element.

[~]
|3> def ezlatin(n):
..>     rows = []
..>     base = np.arange(n)
..>     for i in range(n):
..>         rows.append(np.roll(base, i))
..>     return np.array(rows)

[~]
|4> ezlatin(3)
array([[0, 1, 2],
       [2, 0, 1],
       [1, 2, 0]])

[~]
|5> ezlatin(4)
array([[0, 1, 2, 3],
       [3, 0, 1, 2],
       [2, 3, 0, 1],
       [1, 2, 3, 0]])

[~]
|6> ezlatin(5)
array([[0, 1, 2, 3, 4],
       [4, 0, 1, 2, 3],
       [3, 4, 0, 1, 2],
       [2, 3, 4, 0, 1],
       [1, 2, 3, 4, 0]])


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the SciPy-User mailing list