For me, the star attraction of March Mathness is matrix rotation. It’s a new toy (um, tool) for me that’s exciting on two levels: Firstly, it answers key questions I’ve had about rotation, especially with regard to 4D (let alone 3D or easy peasy 2D). Secondly, I’ve never had a handle on matrix math, and thanks to an extraordinary YouTube channel, now I see it in a whole new light.
Literally (and I do mean “literally” literally), I will never look at a matrix the same way again. Knowing how to look at them changes everything. That they turned out to be exactly what I needed to understand rotation makes the whole thing kinda wondrous.
I’m going to try to provide an overview of what I learned and then point to a great set of YouTube videos if you want to learn, too.
There is no question we invent the language of math. (The question is what — if anything real — does that language describe?)
When it comes to rotation, for which many real physical cases exist, there are related mathematical “dialects” that all accomplish the same thing: rotating a point.
This rotation is a linear process (we can rotate a little or a lot). We can view it as a function that takes some coordinates (a point) and produces new coordinates that are rotated some number of degrees around an axis.
We can formalize this function as:
Where P0 is the point to rotate, and P1 is the result, rotated θ (theta) degrees about the axis described by Φ (phi), which is a vector pointing from the origin.
At least three mathematical dialects let us build the rotate() function.
They’re all related and generally amount to the same amount of actual work. I’ll call them: Trigonometry, Complex, and Matrix.
The point of this post is the last one, so I’ll cover the other two fairly briefly.
Here in the 2D world of screens, it’s easiest to start with 2D rotation. Our axis of rotation is the z axis.
Everything there applies to higher dimensions (there’s just more of it).
The “old-fashioned” almost “brute force” way involves trigonometry. (So does a rotation matrix, but in a much simpler way.)
Essentially, we use a common (but special) trig function, called atan2, to figure out the point’s current angle, α (alpha), relative to the x axis.
We use Pythagoras to calculate the point’s distance (r) from the axis.
The current angle, α, plus the rotation angle, θ, along with the distance (r), allows us to calculate the new x and y.
Here’s the math:
It works just fine, although it requires doing all the calculations for each point to rotate. It works well in cases where you rotate one point of known distance repeatedly, generating a circular motion. Often, only the last two bits of math are needed.
As I mentioned in the Quaternions post, complex numbers are inherently two-dimensional, so they can be interpreted as points on the complex plane.
When we do that, an interesting property emerges: multiplying one complex number by another produces a new number that is rotated with respect to the origin.
Rotation angle, as always, is referenced from the x axis (counter-clockwise).
If point A has an angle of 30°, and point B has an angle of 10°, then A×B results in a point with an angle of 40°.
The caveat is that at least one point needs to have a distance (from the origin) of one, otherwise the distance of the new point will be the product of A and B. It means the point we use to rotate needs a distance of one (it needs to be a unit vector).
Here’s the math:
Two nice things here: We can deal in more holistic points (as complex numbers) rather than x,y pairs.
Also, because it depends only on the angle, the rotation point, PR, can be calculated early and used for all points. No need to recalculate it.
The complex numbers are great for 2D, but we need quaternions to do the same thing in 3D. (I don’t know if octonions enable 4D rotation the way quaternions do for 3D. I’m not entirely sure “rotation” makes sense in 4D.)
Still, I’d bet there is an N-dimensional generalization that uses complex numbers. I’d also bet it gets pretty complicated.
Matrix rotation generalizes much more nicely.
The downside is that matrices can look a little horrific at first, and they are often taught in a way that’s equally horrific.
Most of us were introduced to matrices in high school or college math, taught the rote ways of doing math on them, subjected to some exercises using that math, and that may have been all.
If things went deeper, it probably involved how a matrix can alter a vector. My college linear algebra book gets seriously into proofs about those transformations, but never really opens the door to what is really going on.
Which is pretty cool, very geometrical, and not that challenging to grasp. (Granted we’re only looking at a small part of matrix mathematics. Shallow water that won’t drown us!)
Let’s start with what a matrix looks like (and, more importantly, what it means). Here is a simple 2×2 matrix:
This says we’re associating the name, I2, to a 2×2 matrix with the four values shown (in the row-column order shown).
Note the name doesn’t mean “I-squared.” The little “2” is part of the name. It tells us that I2 is a two-dimensional object. I named it “I” because the values shown make this a 2D identity matrix.
In other words, if we multiply some other matrix by I2, the result is a copy of the other matrix. This is like multiplying a number by 1; the result is a copy of the number. (I mentioned identity elements recently in a Noether post.)
To open the door on what a matrix means, let me show you another 2×2 matrix (which I’ll call T2, a 2D transform):
When we look at a 2×2 matrix, what we should see is two vertical columns, both of which are 2D vectors (with an x and y). We’ll call the one on the left (in red) “i-hat” and the one on the right (in blue) “j-hat” (the “hat” for the circumflex they both wear).
So a 2×2 matrix matrix has two vectors, i-hat and j-hat, which we think of as initially associated with the x and y axes (respectively). Consider the identity matrix, I2, in this new light:
We imagine i-hat is initially set to (x=1, y=0), so it is a unit vector that lies along the x axis. It is, in fact, the x basis vector for the 2D space. As such, we could also call it x-hat.
Likewise, we imagine j-hat is initially set to (x=0, y=1), so it is also a unit vector, but it lies along the y axis. It is the y basis vector for the space. We could call it y-hat.
Note that we call them ‘i‘ and ‘j‘ (rather than ‘x’ and ‘y’) because a transform can move them off the x or y axis.
Essentially, we start by imagining a transformation matrix initially is the identity matrix!
I say we “imagine” the initial setting because we look at the actual values of the matrix as representing a transformation.
The values of the matrix tell us where i-hat and j-hat end up because the transformation.
This is why the identity matrix doesn’t change anything. The imagined initial values and the actual values are the same, so i-hat and j-hat don’t change.
Crucially here, any 2D point can be thought of in terms of the basis vectors, x-hat and y-hat:
That is, any point is some number, a, times x-hat and some other number, b, times y-hat. (This is what we really mean any time we work with x and y. We just don’t usually break it down in terms of basic vectors.)
The magic of transformation is that, when we transform i-hat and j-hat, any transformed point has the same relationship to them as it did to x-hat and y-hat before the transform.
By knowing where i-hat and j-hat end up, we know where any point we want to transform goes.
Now let’s consider how we can rotate with a matrix transformation:
The diagram above illustrates several parts of the process:
Firstly, x-hat (dark blue horizontal) and y-hat (dark green vertical) are the original basis vectors for the (original) 2D space. The lines are a visual representation of the I2 identity matrix.
Secondly, i-hat (slanted light blue) and j-hat (slanted light green) are the basis vectors for the transformed space. They are a visual representation of a rotation matrix (described below).
Thirdly, the original point, P0 (black), and transformed point, P1 (red), which is rotated by θ (theta) degrees.
As mentioned above, P0, has an x and y that relate to x-hat and y-hat through constants (a and b, which on the diagram are 0.9 and 0.8, respectively):
This is important, because the rotated point, P1, has the same relationship with i-hat and j-hat:
As you can see in the diagram above.
This is the foundation of all linear matrix transforms!
The matrix values tell us where i-hat and j-hat go. To transform a point, we just consider its relationship to x-hat and y-hat (its x and y values) and then apply those values to i-hat and j-hat.
(Another way to view it is that a matrix describes new basis vectors that transform a space.)
At this point there are, perhaps, at least two obvious questions:
- How do we make a rotation matrix?
- How do we use a rotation matrix?
There is also the question of how all this applies to four-dimensional tesseractae (which it so does).
In the next installment I’ll try to answer the first two questions as well as (at least) setting the stage for extending this to three and four dimensions. (As you might guess, we just add more rows and columns.)
As a foretaste, here’s the rotation matrix for the diagram above:
See how the two columns have the x (top) and y (bottom) values for the i-hat and j-hat vectors on the diagram?
Stay tuned, my friends!