## Flipping over Harry Potter

Scientists, engineers and mathematicians often model events in the world with linear systems. Given the bumps, curves and sudden unexpected happenings of life, it can surprise me that such models produce meaningful results. Granted, small time steps may be required as such linearity may sufficiently hold only within a small local neighborhood, but still, life, at least mine, can feel quite far from linear!

I believe this will be my last blog post that uses linear algebra to manipulate a Harry Potter image. We’ll imagine we are in a classroom of Hogwarts learning to cast the levicorpus spell, which flips a person on the spot. Why flip Harry? Maybe Malfoy intentionally misdirected his spell from the feather on his desk to his young adversary or Ron’s wand malfunctioned or maybe….Whatever the case, Harry will flip out over the magic of linear algebra, a Muggles way of performing such a feat.

An image can be viewed as a matrix of colors. The image above that we will manipulate is 188 by 301 pixels. Each entry of the matrix stores a color of the corresponding pixel. This assumes that color is stored as a single value. Often, a color pixel is stored as a triplet indicating the amount of red, blue and green that combine to produce the color. When stored in this way, we have an n by m by 3 matrix, where the resulting image is n by m pixels. The linear algebra discussed below holds for such matrices as we could treat an n by m by 3 matrix as three n by m matrices.

To begin, let’s flip Harry in what would be a truly magical and possibly somewhat painful way that is reminiscent of a PowerPoint slide transition. To begin, we’ll swap the image’s first and last rows. Then, the second and second to last rows are interchanged. We continue to flip the image by swapping rows i and n-i+1 where n is the number of rows in the image. Below, we see how such operations would be performed on a 5 by 4 image.

After each swap, we redraw the image. The result is the levicorpus spell performed on Harry’s image as seen below.

What a painful hex! Does it look linear? It is from the standpoint of matrix multiplication. Let H equal the matrix of pixel colors for Harry’s image. Interchanging two rows is commonly used in linear algebra. To perform this as a linear operation, we take the identity matrix, which is a matrix containing zeros for all off diagonal elements and ones along the diagonal. If we want to swap rows i and j in matrix H, we form the matrix P that equals the identity matrix except we swapped rows i and j. We see such an example below if we wanted to swap the first and last rows.

The image with the rows swapped equals PH. Magic? Maybe not…but a PowerPoint transition approach to levicorpus, indeed.

Spinning Harry might be a more expected method of flipping. This can also be accomplished with matrix multiplication. This time, we form a rotation matrix,
$R = left( begin{array}{cc} costheta & - sintheta \ sintheta & costheta end{array}right)$
which will rotate a vector v counterclockwise by the angle $theta$ about the origin. However, what are the coordinates of the original pixels? If they are integral then the rotated coordinates may not also be integral. (In fact, even if they are not taken to be integral, similar issues arise.) One way to deal with this is to use interpolation to compute the pixel color of each pixel at an integral coordinate. This type of approach was used to produce the rotation below.

Enjoy using linear algebra to cast a Muggle’s levicorpus spell.