Rotating a matrix

March 2022 — math

This is a small problem with an interesting link to geometry.

Problem statement
Design a function that takes a matrix and rotates it clockwise by 90 degrees.

This shows a clockwise rotation on a test matrix:

$$ \left( \begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{matrix} \right) \xrightarrow{cw} \left( \begin{matrix} 7 & 4 & 1 \\ 8 & 5 & 2 \\ 9 & 6 & 3 \end{matrix} \right) $$  

How would we do this?

One way is to take all the rows, and write them out as columns in the appropriate slots. This is easy to see if we set the first row in bold and see where it ends up in the rotated matrix:

$$ \left( \begin{matrix} \bf{1} & \bf{2} & \bf{3} \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{matrix} \right) \xrightarrow{cw} \left( \begin{matrix} 7 & 4 & \bf{1} \\ 8 & 5 & \bf{2} \\ 9 & 6 & \bf{3} \end{matrix} \right) $$

That works, but there’s a more interesting solution. Let’s look at the effect of a transpose operation on our test matrix:

$$ \left( \begin{matrix} \bf{1} & \bf{2} & \bf{3} \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{matrix} \right) \xrightarrow{\top} \left( \begin{matrix} \bf{1} & 4 & 7 \\ \bf{2} & 5 & 8 \\ \bf{3} & 6 & 9 \end{matrix} \right) $$

This is almost what we want, but the columns are in exactly the opposite order. So, if we first transpose, and then reverse the columns, we get our rotated matrix:

$$ \left( \begin{matrix} \bf{1} & \bf{2} & \bf{3} \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{matrix} \right) \xrightarrow{\top} \left( \begin{matrix} \bf{1} & 4 & 7 \\ \bf{2} & 5 & 8 \\ \bf{3} & 6 & 9 \end{matrix} \right) $$

$$ \left( \begin{matrix} \bf{1} & 4 & 7 \\ \bf{2} & 5 & 8 \\ \bf{3} & 6 & 9 \end{matrix} \right) \xrightarrow{\text{flipLR}} \left( \begin{matrix} 7 & 4 & \bf{1} \\ 8 & 5 & \bf{2} \\ 9 & 6 & \bf{3} \end{matrix} \right) $$

Amazing! But why does this work?

There’s a deep reason: A composition of two reflections across intersecting reflection lines produces a rotation of twice the angle between them, in the direction of the first to the second reflection.

That’s a mouthful. Let’s draw some pictures and see it in action. We’ll squint at the matrix and visualize it as a square on the plane:

Transposing is a reflection through the red line. The blue point reflects to the red point.

Flipping left and right is a reflection through the green vertical center line. The red point reflects to the green point.

Combining the transpose reflection and the flip reflection produces a 90 degree rotation, which is twice the angle (45 degrees) between the reflecting lines. The blue point first reflects to the red point, and that reflects to the green point. The total rotation takes the blue corner to the green corner.

This works for any rotation in the plane, and our matrix rotation is a special case where only quarter turns make valid rotations.

How would you make the square turn counterclockwise?

Credit: I found the original problem in Algebra-Driven Design, which arrives at the same solution but does not explain why it works.