Matrix Transformations

This page explains the basics of matrix operations and coordinate transformations that use them (scaling and rotation).

<Note>
This page uses MathJax to display formulas. If your browser settings block script execution, the formulas on this page will not display correctly.

Matrix basics

A matrix is written as elements arranged in a rectangle and enclosed in brackets. A horizontal line of elements is called a row, and a vertical line is called a column. For example, each of the following matrices is called a matrix with 2 rows and 3 columns, or a 2x3 matrix.

\[\begin{array}{ll} A=\pmatrix{ a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}} & B=\pmatrix{ 1 & 6 & -2\\ -3 & 0 & 4} \end{array}\]


A matrix whose number of rows and number of columns are both \(\ n \ \), that is, an \(\ n\times n \ \) matrix, is called a square matrix of order \(\ n\ \). There are also the following special matrices.

Definition (zero matrix)
A matrix all of whose elements are 0 is called the zero matrix and is denoted \(\ O\ \).

Definition (identity matrix)
Among square matrices of order \(n\ \), the one whose entries in row \(\ m\ \), column \(\ m\ \) are all 1 and whose other elements are all 0 is called the identity matrix of order \(\ n\ \) and is denoted \(\ E_n\ \).

Matrices are also related to vector operations, and row vectors and column vectors are defined as follows.

Definition (row vector, column vector)
A \(\ 1\times n \ \) matrix is called a row vector.
An \(\ n\times 1 \ \) matrix is called a column vector.

Furthermore, for any matrix, we can consider what are called block matrices, as follows.

Definition (block matrix)
When a matrix of any size is regarded as a matrix whose elements are smaller matrices, each of those is called a block of the original matrix.

Matrix operations

Just as the four arithmetic operations can be considered for real numbers, sums, differences and products can be considered for matrices.

Definition
For matrices \(\ A,\ B\ \) of the same shape, their sum and difference are defined as follows.

\[\begin{gathered} A\pm B=\pmatrix{ a_{11}\pm b_{11} & a_{12} \pm b_{12} & \cdots & a_{1n}\pm b_{1n}\\ a_{21}\pm b_{21} & a_{22} \pm b_{22} & \cdots & a_{2n}\pm b_{2n}\\ \vdots & \vdots & \ddots & \vdots\\ a_{m1}\pm b_{m1} & a_{m2} \pm b_{m2} & \cdots & a_{mn}\pm b_{mn}\\ }\end{gathered}\]

The sum and difference are very simple, since they just add or subtract the entries in the same positions. Note, however, that the sum and difference are not defined for matrices of different sizes.
Scalar multiplication (multiplication by a constant) of a matrix is defined as follows. This is also very easy to understand, since it just multiplies every entry by the constant.

Definition
For any matrix \(\ A\ \), its scalar multiple is defined as follows.

\[\begin{gathered} kA=\pmatrix{ ka_{11} & ka_{12} & \cdots & ka_{1n}\\ ka_{21} & ka_{22} & \cdots & ka_{2n}\\ \vdots & \vdots & \ddots & \vdots\\ ka_{m1} & ka_{m2} & \cdots & ka_{mn}\\ }\end{gathered}\]

On the other hand, the product of matrices is considerably more complicated than the sum and difference.

Definition
For an \(m \times n\ \) matrix \(\ A\ \) and an \(\ n \times l\ \) matrix \(\ B\ \), their product is defined as follows.

\[\begin{gathered} AB=\pmatrix{ c_{11} & c_{12} & \cdots & c_{1l}\\ c_{21} & c_{22} & \cdots & c_{2l}\\ \vdots & \vdots & \ddots & \vdots\\ c_{m1} & c_{m2} & \cdots & c_{ml}\\ },\ c_{ij}=\sum_{k=1}^n{a_{ik}b_{kj}} \end{gathered}\]

The product AB of two matrices is defined only when the number of columns of A equals the number of rows of B. When computing a product, you get the same result if you divide each matrix into suitable blocks and compute the product as a matrix of blocks.
Matrix operations have the following properties.

Theorem
For any matrices chosen so that each operation is defined, the following properties hold.

\[\begin{array}{ll} \mbox{- Addition is always commutative:} & A+B=B +A\\ \mbox{- Addition is always associative:} & (A+B)+C=A+(B+C)\\ \mbox{- Multiplication is in general non-commutative:} & AB \not= BA\\ \mbox{- Multiplication is always associative:} & (AB)C = A(BC)\\ \mbox{- Multiplication always distributes over addition:} & (A+B)C=AC+BC,\ A(B+C)=AB+AC\\ \mbox{- Multiplication by the identity matrix always commutes:} & AE=EA=A\\ \mbox{- Multiplication by the zero matrix always commutes:} & AO=OA=O \end{array}\]

Determinants and inverse matrices

We have explained the sum, difference and product of matrices, which raises the question of whether there is a quotient. There is nothing called a matrix quotient, but something similar can be defined, for square matrices only.

To define this “quotient-like” thing for matrices, we need to find a matrix called the inverse matrix, and to find the inverse matrix we need to define something called the determinant. However, the general definition of the determinant is very difficult, so the explanation below focuses on square matrices of order 2.

Definition (determinant)
In general, for a square matrix \(\ A\ \) of order \(\ n\ \), its determinant \(\ \det(A)\ \) is defined as follows.

\[ \det(A) = \sum_{\sigma \in \mathfrak{S}_n}\operatorname{sgn}(\sigma) \prod_{i = 1}^n a_{i \sigma(i)} \]

In particular, for a square matrix of order 2, \(\ A=\pmatrix{a & b\\ c & d}\ \), \(\ \det(A)=ad-bc\ \).

In general, the inverse of a square matrix is defined as follows.

Definition (regular matrix, inverse matrix)
In general, for a square matrix \(\ A\ \) of order \(\ n\ \), if there exists a matrix \(\ B\ \) satisfying \( \ AB=BA=E_n\ \), \(\ A\ \) is said to be regular (invertible).
For a regular matrix \(\ A\ \), this matrix \(\ B\ \) is uniquely determined and is called the inverse matrix of \(\ A\ \).

Using the determinant, the inverse \(\ A^{-1}\ \) of the square matrix of order 2 \(\ A=\pmatrix{a & b\\ c & d}\ \) is written as follows. The determinant is also written \(\ |A|\ \). General inverse matrices can also be defined as long as the determinant is not 0, but their expression is very complicated, so it is not covered here.

Theorem
The inverse of the square matrix of order 2 \(\ A=\pmatrix{a & b \\ c & d}\ \) is defined as long as \(\ |A|\not=0\ \), and is expressed as follows.

\[\begin{gathered} A^{-1}=\cfrac{1}{|A|}\pmatrix{d & -b\\ -c & a} \end{gathered}\]

Inverse matrices and determinants have the following properties.

Theorem
For any square matrices \(\ A,B\ \) of the same order and the identity matrix \(\ E\ \), the following equations always hold.

\[\begin{gathered} |E|=1\\ |AB|=|A||B|\\ |A^{-1}|=\cfrac{1}{|A|}\\ AA^{-1}=A^{-1}A=E\\ \end{gathered}\]

Eigenvalues and eigenvectors

Matrices are related to operations called linear transformations, and understanding these requires the concepts of eigenvalues and eigenvectors.

Definition (eigenvalue, eigenvector)
For a linear transformation \(\ A\ \), a nonzero vector \(\ \Bbb x\ \) satisfying \(\ A\Bbb x=\lambda\Bbb x\ \) is called an eigenvector of this linear transformation, and the constant \(\ \lambda\ \) is called an eigenvalue.

Words alone are not enough, so let’s actually plug in some values and calculate.

Exercise
Find the eigenvalues and eigenvectors of the matrix \(\ A=\pmatrix{1 & -1\\ 2 & 4}\ \).

Multiplying both sides of \(A\Bbb x=\lambda\Bbb x\ \) by the identity matrix does not change its meaning, so
the equation \((A-\lambda E)\Bbb x=O\ \) is derived.
For this equation to have a nonzero solution, \(\ |A-\lambda E|=0\ \) must hold. Thus,
\( |A-\lambda E|=\det\left( \pmatrix{1 & -1\\ 2 & 4}-\lambda\pmatrix{1 & 0\\ 0 & 1}\right)=(\lambda-3)(\lambda-2)=0\ \)
Therefore, the eigenvalues of this matrix \(\ A\ \) are \(\ \lambda=2,3\ \).

When \(\lambda=2\ \), the eigenvector \(\ \Bbb x=\pmatrix{x\\y}\ \) is,
from \((A-2E)\Bbb x=\pmatrix{-1 & -1\\ 2 & 2}\pmatrix{x\\ y}=\pmatrix{-x-y\\ 2x+2y}=O\ \),
\(\ \Bbb x=\pmatrix{k\\ -k}\ \).
When \(\lambda=3\ \), the eigenvector \(\ \Bbb x=\pmatrix{x\\y}\ \) is,
from \((A-3E)\Bbb x=\pmatrix{-2 & -1\\ 2 & 1}\pmatrix{x\\ y}=\pmatrix{-2x-y\\ 2x+y}=O\ \),
\(\ \Bbb x=\pmatrix{k\\ -2k}\ \).

An eigenvalue represents the scale factor of the linear transformation in the direction of its eigenvector. That is, a linear transformation with eigenvalue 1 preserves the original size in the direction of that eigenvector. An eigenvector indicates a direction that is not changed by this linear transformation (although if the eigenvalue is negative, it is flipped through the origin). Vectors not in the direction of an eigenvector generally change both in size and direction under the linear transformation.

Scaling and rotation

We have already explained that matrices represent linear transformations; here we explain two that are particularly important, scaling and rotation.
Scaling is, literally, a linear transformation that changes only the size of the original figure while preserving its shape. A special case of scaling is the identity transformation, which preserves both shape and size, i.e. in the end a transformation that “does nothing”.

Theorem (identity transformation)
For any \(\ n\ \)-dimensional vector, the \(\ n\ \)-dimensional identity matrix \(\ E_n\ \) represents the identity transformation.

Given this, a scaling that enlarges the size by a factor of \(\ k\ \) is as follows.

Theorem (scaling)
For any \(\ n\ \)-dimensional vector, the scalar multiple \(\ kE_n\ \) of the \(\ n\ \)-dimensional identity matrix represents a scaling.
For any \(\ n\ \)-dimensional vector, the scalar multiple \(\ kA\ \) of a linear transformation \(\ A\ \) represents a scaling of the original linear transformation \(\ A\ \).

It is easy to see that if the factor is negative, the figure is flipped by 180 degrees and its size is multiplied by the absolute value of the factor.
Next, consider rotation about the origin. It can be defined in \(\ n\ \) dimensions, but here we consider only two dimensions, that is, the plane.

Theorem (rotation)
For any plane vector, the linear transformation \(\ \pmatrix{\cos\phi & -\sin\phi\\ \sin\phi & \cos\phi}\ \) represents a rotation by \(\ \phi\ \) counterclockwise about the origin of the coordinate plane.

If you actually take some point on the coordinate plane and calculate, you will see that it is indeed rotated. To scale as well, as usual, just multiply by a scalar.
When several linear transformations are performed in succession, this series of operations is called a composite transformation. Composite transformations have the following properties.

Theorem (composite transformation)
The composite transformation that performs arbitrary transformations \(\ A\ \) and \(\ B\ \) in this order is represented by the matrix \(\ BA\ \).
Because matrix multiplication is non-commutative, composite transformations are likewise non-commutative.
Scaling and rotation commute with each other. That is, scaling and then rotating always represents the same transformation as rotating and then scaling.

Reflection

A transformation that flips a figure about a line (axis) is called a reflection. Reflections about the coordinate axes of the coordinate plane are represented as follows.

Theorem (reflection about an axis)
The transformation \(\ \pmatrix{1 & 0\\ 0 & -1}\ \) represents reflection about the \(\ x\ \)-axis of the coordinate plane.
The transformation \(\ \pmatrix{-1 & 0\\ 0 & 1}\ \) represents reflection about the \(\ y\ \)-axis of the coordinate plane.

To consider reflection about some other arbitrary line instead of a coordinate axis, you can think of it as a composite of rotations and a reflection about an axis.
For example, reflection about the line \(\ y=\sqrt{3}x\ \) (the line through the origin making an angle of \(\ \cfrac{\pi}{3}\ \) with the \(\ x\ \)-axis) can be decomposed into the following three transformations.

Exercise
Find the matrix representing reflection about the line \(\ y=\sqrt{3}x\ \).

The transformation represented by the desired matrix is the same as the composite transformation that rotates by \(\ -\cfrac{\pi}{3}\ \), reflects about the \(\ x\ \)-axis, and rotates by \(\ \cfrac{\pi}{3}\ \). Thus, the desired matrix is

\[\begin{gathered} \pmatrix{\cos\cfrac{\pi}{3} & -\sin\cfrac{\pi}{3}\\ \sin\cfrac{\pi}{3} & \cos\cfrac{\pi}{3}} \pmatrix{1 & 0\\ 0 & -1} \pmatrix{\cos\left(-\cfrac{\pi}{3}\right) & -\sin\left(-\cfrac{\pi}{3}\right)\\ \sin\left(-\cfrac{\pi}{3}\right) & \cos\left(-\cfrac{\pi}{3}\right)}= \cfrac{1}{2}\pmatrix{-1 & \sqrt{3}\\ \sqrt{3} & 1} \end{gathered}\]

In general, the transformation that reflects a figure about the line \(\ y=(\tan\phi)x\ \) is represented as follows. Try verifying it in the same way as the exercise above.

Theorem (reflection about a line through the origin)
The transformation \(\ \pmatrix{\cos2\phi & \sin2\phi\\ \sin2\phi & -\cos2\phi}\ \) represents reflection about the line \(\ y=(\tan\phi)x\ \).

Affine transformations

So far we have explained scaling and rotating figures, but one basic operation is still missing. Needless to say, it is translation.
In fact, there is no 2x2 matrix that represents a transformation translating a two-dimensional figure (while preserving its shape). Therefore, mathematically, it is usual to perform linear transformations with matrices and translations with vector operations. However, using a 3x3 matrix, you can describe a transformation that translates a two-dimensional figure. It may seem a little strange, but by using a matrix one dimension higher than the figure you want to manipulate, you can perform translation in addition to all the linear transformation operations explained so far. In fact, 3D games similarly use four-dimensional matrices, one dimension higher, to transform three-dimensional figures (even three-dimensional matrices are somewhat cumbersome to compute, so four-dimensional matrices are not covered here).
To apply an affine transformation to a two-dimensional figure, the following affine matrix is used.

Affine matrix
\(\ \pmatrix{a & b & c\\ d & e & f\\ 0 & 0 & 1}\ \) is called an affine matrix.

The third row is always of the form 0 0 1. This row has no essential meaning; it exists only so that the matrix operations are properly defined.
Only the first and second rows have meaning for the actual transformation.

Now we will use this affine matrix to transform figures, but there is something we must do first. Since we are working in two dimensions, coordinates consist of only two parameters, the X and Y coordinates. However, the product of a 3x3 matrix and a 2-element column vector cannot be computed, so the column vector corresponding to the coordinates being manipulated must also be a 3-element column vector. So, when performing an affine transformation, we put 1 in the third row. This makes the operation well defined without having any adverse effect on the result. In other words, the formula for an affine transformation of a two-dimensional figure is as follows. To repeat, the 0 0 1 sequence in the third row of the affine matrix and the value 1 in the third row of the column vector have no meaning. They exist only so that the calculation works out.

Affine transformation
\(\ \pmatrix{a & b & c\\ d & e & f\\ 0 & 0 & 1}\pmatrix{x\\ y\\ 1} = \pmatrix{x'\\ y'\\ 1}\ \)

First, let’s express the linear transformations seen so far as affine matrices.

Theorem (affine transformation representing scaling)
The affine transformation \(\ \pmatrix{a & 0 & 0\\ 0 & b & 0\\ 0 & 0 & 1}\) represents a transformation that scales by a factor of \(\ a\ \) along the \(\ x\ \)-axis and \(\ b\ \) along the \(\ y\ \)-axis.

The first is scaling. Two sections back we showed scaling with the same factor for X and Y, but the form is essentially the same even if the factor differs in each direction. Specifying a negative factor flips the figure left-right or up-down, so reflection about a coordinate axis can be said to be a special case of scaling. Of course, if \(\ a=b=1\ \) it is the identity transformation.
Next is rotation.

Theorem (affine transformation representing rotation)
The affine transformation \(\ \pmatrix{\cos\phi & -\sin\phi & 0\\ \sin\phi & \cos\phi & 0\\ 0 & 0 & 1}\ \) represents a rotation by \(\ \phi\ \) counterclockwise about the origin of the coordinate plane.

Let’s also remember the affine transformation representing an arbitrary linear transformation. As you may have noticed from these two examples, you simply put the 2x2 matrix as is in the upper-left corner.

Theorem (affine transformation representing a linear transformation)
The affine transformation \(\ \pmatrix{a & b & 0\\ c & d & 0\\ 0 & 0 & 1}\ \) represents a transformation equivalent to the linear transformation \(\ \pmatrix{a & b\\ c & d}\ \).

Now let’s find the transformation representing translation. Translating means moving a fixed distance along each coordinate axis, so we want to obtain the following result. (The Greek letter \(\ \Delta\ \) is often used with meanings such as “small variation”, “difference” or “change”.)

\(\ \pmatrix{a & b & c\\ d & e & f\\ 0 & 0 & 1}\pmatrix{x\\ y\\ 1} = \pmatrix{x + \Delta x\\ y + \Delta y\\ 1}\ \)

Let’s compute the left-hand side. We have dealt almost exclusively with 2x2 matrices, but the product is computed mechanically in the same way as for 2x2.

\(\ \pmatrix{a & b & c\\ d & e & f\\ 0 & 0 & 1}\pmatrix{x\\ y\\ 1} = \pmatrix{ax + by + c\\ dx + ey +f\\ 1}\ \)

Let’s determine the value of each coefficient by comparing with the result we should end up with.

\(\ \pmatrix{ax + by + c\\ dx + ey +f\\ 1} = \pmatrix{x + \Delta x\\ y + \Delta y\\ 1}\ \)

This should hold, so the matrix representing translation is as follows.

Theorem (affine transformation representing translation)
The affine transformation \(\ \pmatrix{1 & 0 & \Delta x\\ 0 & 1 & \Delta y\\ 0 & 0 & 1}\ \) represents a translation that moves by \(\ \Delta x\ \) along the \(\ x\ \)-axis and \(\ \Delta y\ \) along the \(\ y\ \)-axis.

For the first time, a nonzero value has appeared in the third column. And you can see that the values in this third column are precisely the distances by which the figure is moved.
This should also make clear why the third row of the column vector is 1. If it were any value other than 1, the translation would not work correctly.
Also, composite transformations can be considered for affine transformations in exactly the same way as for linear transformations. And as long as the affine matrix is regular, its inverse is defined, so the inverse transformation can be found by computing the inverse of the affine matrix. The inverse of an affine matrix is as follows.

Theorem (determinant and inverse of an affine matrix)
The determinant \(\ \Delta\ \) of the affine matrix \(\ \pmatrix{a & b & c\\ d & e & f\\ 0 & 0 & 1}\) is given by \(\ \Delta = ae - bd\ \).
The inverse of the affine matrix is defined when \(\ \Delta \ne 0\ \), and is expressed as \(\ \cfrac{1}{\Delta}\pmatrix{e & -b & bf - ce\\ -d & a & cd - fa\\ 0 & 0 & \Delta}\).

As you can see from the formula, the determinant of an affine matrix is the same as the determinant of the 2x2 matrix representing the linear transformation part. You can also see that in the inverse, the linear transformation part is the inverse of the linear transformation. The part related to translation is a little harder to see, but it represents a translation in the opposite direction, that is, a translation that returns from the destination coordinates to the original position. The third row is still 0 0 1.
Also, since the determinant of the linear transformation and that of the affine transformation coincide, the condition for an affine transformation to have an inverse can be stated as the linear transformation part having an inverse transformation.
Let’s look at one applied example. We explained rotation about the origin earlier, but how can rotation about an arbitrary point be represented? Using affine matrices, it can be written concisely.

Exercise
Find the matrix representing the transformation that rotates by \(\ \phi\ \) counterclockwise about an arbitrary point \(\ (a, b)\ \) on the coordinate plane.

The transformation represented by the desired matrix is the same as the composite transformation that translates by \(\ (-a, -b)\ \), rotates by \(\ \phi\ \), and then translates by \(\ (a, b)\ \). Thus, the desired matrix is

\[\begin{gathered} \pmatrix{ 1 & 0 & a\\ 0 & 1 & b\\ 0 & 0 & 1 } \pmatrix{ \cos \phi & -\sin\phi & 0 \\ \sin\phi & \cos\phi & 0\\ 0 & 0 & 1} \pmatrix{ 1 & 0 & -a\\ 0 & 1 & -b\\ 0 & 0 & 1}= \pmatrix{ \cos\phi & -\sin\phi & -a\cos\phi +b\sin\phi +a\\ \sin\phi & \cos\phi & -a\sin\phi -b\cos\phi +b \\ 0 & 0 & 1} \end{gathered}\]

The above are the basics of transforming figures using matrices. Operations that transform figures can also be considered using complex numbers (the Gaussian plane), but that is not covered here. To explain just the gist briefly: the y-axis of the coordinate plane is mapped to the pure imaginary numbers, and the complex number \(\ z=x+iy\ \) is mapped to the coordinates \(\ (x, y)\ \). With this approach, multiplying by a real number means scaling, and multiplying by an imaginary number means a linear transformation including rotation, flipping and so on.

The SNES’s Mode 7 scales and rotates a BG using the 2x2 matrix and translation explained here. Each element of the matrix is set as an 8.8 fixed-point number. For the actual setup, see the M7A-M7D registers and the sample code in Mode 7.