# Generate all dihedral transformations of a matrix?

Given a matrix, e.g.

matrix = Table[a[i, j], {i, 1, 3}, {j, 1, 3}];

I would like to have a function that takes matrix as input and returns a list of matrices representing all possible dihedral transformations of matrix (all possible simultaneous reflections and rotations along all columns and/or all rows). Of course I could write a monstrosity of Do routines, but I am wondering if there is a neat and quick way to do that in Mathematica? Thanks for any suggestion!

EDIT:

Forgot to mention: There should also be reflections (transposition) along the diagonal and anti-diagonal in the action of the group!

Perhaps you could use PermutationGroup and Permute. Here is a PermutationGroup representation:

grp = PermutationGroup[{
Cycles[{{1,2,3},{4,5,6},{7,8,9}}], Cycles[{{1,2},{4,5},{7,8}}], (*columns*)
Cycles[{{1,4,7},{2,5,8},{3,6,9}}], Cycles[{{1,4},{2,5},{3,6}}], (*rows*)
Cycles[{{2,4},{3,7},{6,8}}] (*rotations*)
}];

Then use Permute to get all of the transformations:

toMatrix[list_] := Partition[list, Sqrt[Length@list]]

Grid @ Partition[
toMatrix /@ Permute[{"a", "b", "c", "d", "e", "f", "g", "h", "q"}, grp],
6
] //TeXForm

$\begin{array}{cccccc} \left( \begin{array}{ccc} \text{a} & \text{b} & \text{c} \\ \text{d} & \text{e} & \text{f} \\ \text{g} & \text{h} & \text{q} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{a} & \text{b} & \text{c} \\ \text{g} & \text{h} & \text{q} \\ \text{d} & \text{e} & \text{f} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{a} & \text{c} & \text{b} \\ \text{d} & \text{f} & \text{e} \\ \text{g} & \text{q} & \text{h} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{a} & \text{c} & \text{b} \\ \text{g} & \text{q} & \text{h} \\ \text{d} & \text{f} & \text{e} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{a} & \text{d} & \text{g} \\ \text{b} & \text{e} & \text{h} \\ \text{c} & \text{f} & \text{q} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{a} & \text{d} & \text{g} \\ \text{c} & \text{f} & \text{q} \\ \text{b} & \text{e} & \text{h} \\ \end{array} \right) \\ \left( \begin{array}{ccc} \text{a} & \text{g} & \text{d} \\ \text{b} & \text{h} & \text{e} \\ \text{c} & \text{q} & \text{f} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{a} & \text{g} & \text{d} \\ \text{c} & \text{q} & \text{f} \\ \text{b} & \text{h} & \text{e} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{b} & \text{a} & \text{c} \\ \text{e} & \text{d} & \text{f} \\ \text{h} & \text{g} & \text{q} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{b} & \text{a} & \text{c} \\ \text{h} & \text{g} & \text{q} \\ \text{e} & \text{d} & \text{f} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{b} & \text{c} & \text{a} \\ \text{e} & \text{f} & \text{d} \\ \text{h} & \text{q} & \text{g} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{b} & \text{c} & \text{a} \\ \text{h} & \text{q} & \text{g} \\ \text{e} & \text{f} & \text{d} \\ \end{array} \right) \\ \left( \begin{array}{ccc} \text{b} & \text{e} & \text{h} \\ \text{a} & \text{d} & \text{g} \\ \text{c} & \text{f} & \text{q} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{b} & \text{e} & \text{h} \\ \text{c} & \text{f} & \text{q} \\ \text{a} & \text{d} & \text{g} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{b} & \text{h} & \text{e} \\ \text{a} & \text{g} & \text{d} \\ \text{c} & \text{q} & \text{f} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{b} & \text{h} & \text{e} \\ \text{c} & \text{q} & \text{f} \\ \text{a} & \text{g} & \text{d} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{c} & \text{a} & \text{b} \\ \text{f} & \text{d} & \text{e} \\ \text{q} & \text{g} & \text{h} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{c} & \text{a} & \text{b} \\ \text{q} & \text{g} & \text{h} \\ \text{f} & \text{d} & \text{e} \\ \end{array} \right) \\ \left( \begin{array}{ccc} \text{c} & \text{b} & \text{a} \\ \text{f} & \text{e} & \text{d} \\ \text{q} & \text{h} & \text{g} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{c} & \text{b} & \text{a} \\ \text{q} & \text{h} & \text{g} \\ \text{f} & \text{e} & \text{d} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{c} & \text{f} & \text{q} \\ \text{a} & \text{d} & \text{g} \\ \text{b} & \text{e} & \text{h} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{c} & \text{f} & \text{q} \\ \text{b} & \text{e} & \text{h} \\ \text{a} & \text{d} & \text{g} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{c} & \text{q} & \text{f} \\ \text{a} & \text{g} & \text{d} \\ \text{b} & \text{h} & \text{e} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{c} & \text{q} & \text{f} \\ \text{b} & \text{h} & \text{e} \\ \text{a} & \text{g} & \text{d} \\ \end{array} \right) \\ \left( \begin{array}{ccc} \text{d} & \text{a} & \text{g} \\ \text{e} & \text{b} & \text{h} \\ \text{f} & \text{c} & \text{q} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{d} & \text{a} & \text{g} \\ \text{f} & \text{c} & \text{q} \\ \text{e} & \text{b} & \text{h} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{d} & \text{e} & \text{f} \\ \text{a} & \text{b} & \text{c} \\ \text{g} & \text{h} & \text{q} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{d} & \text{e} & \text{f} \\ \text{g} & \text{h} & \text{q} \\ \text{a} & \text{b} & \text{c} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{d} & \text{f} & \text{e} \\ \text{a} & \text{c} & \text{b} \\ \text{g} & \text{q} & \text{h} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{d} & \text{f} & \text{e} \\ \text{g} & \text{q} & \text{h} \\ \text{a} & \text{c} & \text{b} \\ \end{array} \right) \\ \left( \begin{array}{ccc} \text{d} & \text{g} & \text{a} \\ \text{e} & \text{h} & \text{b} \\ \text{f} & \text{q} & \text{c} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{d} & \text{g} & \text{a} \\ \text{f} & \text{q} & \text{c} \\ \text{e} & \text{h} & \text{b} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{e} & \text{b} & \text{h} \\ \text{d} & \text{a} & \text{g} \\ \text{f} & \text{c} & \text{q} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{e} & \text{b} & \text{h} \\ \text{f} & \text{c} & \text{q} \\ \text{d} & \text{a} & \text{g} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{e} & \text{d} & \text{f} \\ \text{b} & \text{a} & \text{c} \\ \text{h} & \text{g} & \text{q} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{e} & \text{d} & \text{f} \\ \text{h} & \text{g} & \text{q} \\ \text{b} & \text{a} & \text{c} \\ \end{array} \right) \\ \left( \begin{array}{ccc} \text{e} & \text{f} & \text{d} \\ \text{b} & \text{c} & \text{a} \\ \text{h} & \text{q} & \text{g} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{e} & \text{f} & \text{d} \\ \text{h} & \text{q} & \text{g} \\ \text{b} & \text{c} & \text{a} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{e} & \text{h} & \text{b} \\ \text{d} & \text{g} & \text{a} \\ \text{f} & \text{q} & \text{c} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{e} & \text{h} & \text{b} \\ \text{f} & \text{q} & \text{c} \\ \text{d} & \text{g} & \text{a} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{f} & \text{c} & \text{q} \\ \text{d} & \text{a} & \text{g} \\ \text{e} & \text{b} & \text{h} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{f} & \text{c} & \text{q} \\ \text{e} & \text{b} & \text{h} \\ \text{d} & \text{a} & \text{g} \\ \end{array} \right) \\ \left( \begin{array}{ccc} \text{f} & \text{d} & \text{e} \\ \text{c} & \text{a} & \text{b} \\ \text{q} & \text{g} & \text{h} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{f} & \text{d} & \text{e} \\ \text{q} & \text{g} & \text{h} \\ \text{c} & \text{a} & \text{b} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{f} & \text{e} & \text{d} \\ \text{c} & \text{b} & \text{a} \\ \text{q} & \text{h} & \text{g} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{f} & \text{e} & \text{d} \\ \text{q} & \text{h} & \text{g} \\ \text{c} & \text{b} & \text{a} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{f} & \text{q} & \text{c} \\ \text{d} & \text{g} & \text{a} \\ \text{e} & \text{h} & \text{b} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{f} & \text{q} & \text{c} \\ \text{e} & \text{h} & \text{b} \\ \text{d} & \text{g} & \text{a} \\ \end{array} \right) \\ \left( \begin{array}{ccc} \text{g} & \text{a} & \text{d} \\ \text{h} & \text{b} & \text{e} \\ \text{q} & \text{c} & \text{f} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{g} & \text{a} & \text{d} \\ \text{q} & \text{c} & \text{f} \\ \text{h} & \text{b} & \text{e} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{g} & \text{d} & \text{a} \\ \text{h} & \text{e} & \text{b} \\ \text{q} & \text{f} & \text{c} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{g} & \text{d} & \text{a} \\ \text{q} & \text{f} & \text{c} \\ \text{h} & \text{e} & \text{b} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{g} & \text{h} & \text{q} \\ \text{a} & \text{b} & \text{c} \\ \text{d} & \text{e} & \text{f} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{g} & \text{h} & \text{q} \\ \text{d} & \text{e} & \text{f} \\ \text{a} & \text{b} & \text{c} \\ \end{array} \right) \\ \left( \begin{array}{ccc} \text{g} & \text{q} & \text{h} \\ \text{a} & \text{c} & \text{b} \\ \text{d} & \text{f} & \text{e} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{g} & \text{q} & \text{h} \\ \text{d} & \text{f} & \text{e} \\ \text{a} & \text{c} & \text{b} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{h} & \text{b} & \text{e} \\ \text{g} & \text{a} & \text{d} \\ \text{q} & \text{c} & \text{f} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{h} & \text{b} & \text{e} \\ \text{q} & \text{c} & \text{f} \\ \text{g} & \text{a} & \text{d} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{h} & \text{e} & \text{b} \\ \text{g} & \text{d} & \text{a} \\ \text{q} & \text{f} & \text{c} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{h} & \text{e} & \text{b} \\ \text{q} & \text{f} & \text{c} \\ \text{g} & \text{d} & \text{a} \\ \end{array} \right) \\ \left( \begin{array}{ccc} \text{h} & \text{g} & \text{q} \\ \text{b} & \text{a} & \text{c} \\ \text{e} & \text{d} & \text{f} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{h} & \text{g} & \text{q} \\ \text{e} & \text{d} & \text{f} \\ \text{b} & \text{a} & \text{c} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{h} & \text{q} & \text{g} \\ \text{b} & \text{c} & \text{a} \\ \text{e} & \text{f} & \text{d} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{h} & \text{q} & \text{g} \\ \text{e} & \text{f} & \text{d} \\ \text{b} & \text{c} & \text{a} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{q} & \text{c} & \text{f} \\ \text{g} & \text{a} & \text{d} \\ \text{h} & \text{b} & \text{e} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{q} & \text{c} & \text{f} \\ \text{h} & \text{b} & \text{e} \\ \text{g} & \text{a} & \text{d} \\ \end{array} \right) \\ \left( \begin{array}{ccc} \text{q} & \text{f} & \text{c} \\ \text{g} & \text{d} & \text{a} \\ \text{h} & \text{e} & \text{b} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{q} & \text{f} & \text{c} \\ \text{h} & \text{e} & \text{b} \\ \text{g} & \text{d} & \text{a} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{q} & \text{g} & \text{h} \\ \text{c} & \text{a} & \text{b} \\ \text{f} & \text{d} & \text{e} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{q} & \text{g} & \text{h} \\ \text{f} & \text{d} & \text{e} \\ \text{c} & \text{a} & \text{b} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{q} & \text{h} & \text{g} \\ \text{c} & \text{b} & \text{a} \\ \text{f} & \text{e} & \text{d} \\ \end{array} \right) & \left( \begin{array}{ccc} \text{q} & \text{h} & \text{g} \\ \text{f} & \text{e} & \text{d} \\ \text{c} & \text{b} & \text{a} \\ \end{array} \right) \\ \end{array}$

Let's denote a matrix by a simple notation:

M[a, b, c, d, e, f, g, h, q]

such that the matrix form is recovered by the substitution:

subM = {M[x__] :> MatrixForm[Partition[{x}, Sqrt[Length[{x}]]]]};
M[a, b, c, d, e, f, g, h, q] /. subM

Rotations and reflections of rows is one dihedral group $D_{2n}$ with $2n$ elements. Rotations and reflections of columns is another copy of dihedral group $D_{2n}$. Finally, transposition along the diagonals (or, when combined with a row or column reflection, equivalently simply rotations by 90 degrees) are a superimposed $S_2$ group with 2 elements. This means we have a semi-direct product $S_2\rtimes D_{2n}$ with $2(2n)^2$ elements in total.

In our example $n=3$, so that we get $72$ elements. One can generate an invariant object from an M as follows:

invar[m_] := Block[{tmp, res, n, pref},
pref = m /. M[xx__] -> 1;
n = Sqrt[Length[m /. M -> List]];
tmp = Partition[m/pref /. M -> List, n];
res = Table[M[RotateRight[tmp, i]], {i, 0, n - 1}] //DeleteDuplicates;
res = ((Table[ M[Transpose[RotateRight[Transpose[(#[[1]])], i]]], {i, 0, n - 1}] &) /@ res) // Flatten // DeleteDuplicates;
res = {res, (Reverse@# & /@ # & /@ res)} // Flatten // DeleteDuplicates;
res = {res, (Transpose@Reverse@Transpose@# & /@ # & /@ res)} // Flatten // DeleteDuplicates;
res = {res, (Transpose@# & /@ # & /@ res)} // Flatten // DeleteDuplicates;
pref ( res /. M[x__] :> (M[x] /. List -> Sequence) /. List -> Plus) // Expand
]

So that we properly get 72 distinct objects in the most general case, collectively furnishing an invariant set:

invar[M[a, b, c, d, e, f, g, h, q]] /. subM

However, the function invar[m_] is too hacky for my taste. Especially when n grows bigger, I suspect it will become very slow. That is why I'd prefer to have a more elegant solution.

The action on the four corners of the matrix defines the group, so we are talking about DihedralGroup[4], which has 8 elements.

We can construct those 8 matrices combining Transpose and Reverse:

dih4[mat_] := With[{list = NestList[Transpose[Reverse[#]] &, mat, 3]}, Join[list, Transpose /@ list]]

• Actually, I believe the group is $S_2\rtimes D_{2n}$. See my answer for details. – Kagaratsch Nov 12 '17 at 17:48
• It just so happens that the groups $D_{2n}$ and $S_n$ coincide for $n=3$, which means that the swapping that you refer to can always be expressed in terms of cyclic rotation and reflection when only three elements are present. And indeed $n!$ and $2n$ coincide for $n=3$. For $n>3$ no swapping will appear. – Kagaratsch Nov 13 '17 at 21:36