SoCoder -> Article Home -> Advanced Techniques
Created : 20 September 2013
Edited : 20 September 2013
Maths 101 - Episode 3: MatricesWhat are matrices, and what can we do to them?
Now we're going to move on to something a little bit more involved: matrices. This tutorial will cover what matrices are, some of their properties, and some mathematical operations that can be performed with them. Their main use, from a game programming standpoint, is in linear transformations, but we'll save that for next time, or this articles will be unbearably long.
An understanding of vectors is not compulsory, but you might find it useful to understand these first.
What is a matrix?Like a vector, a matrix (plural: matrices) is a mathematical object. However, while a vector has a nice and easy to visualise geometric representation, matrices are somewhat more abstract.
A matrix is a rectangular grid of numbers, consisting of some number of rows and some number of columns. You could think of it like a 2D array. When specifying the size of a matrix, we usually say an 'm by n' matrix has m rows and n columns. A square matrix has the same number of rows as columns, which gives it a few extra properties.
To write out a matrix, we usually put the grid of numbers in large brackets. However, because I'm typing this, I'll be writing mine like this:
That would be a 3x3 matrix, for example.
Matrix AdditionSo, let's look at what we can do with matrices. The simplest operation is matrix addition (and, by extension, matrix subtraction, which works in exactly the same way). You can only add two matrices that are the same size, so you can't for instance add a 2x2 matrix to a 2x5 one.
If you have two mxn matrices, their sum is simply an mxn matrix, where each element is the sum of the elements in the same position.
Scalar MultiplicationAs with vectors, we can multiply any matrix by a scalar quantity. Just multiply each element of the matrix by the scalar - easy!
Matrix Multiplication - The RulesThis is the big one. Matrix multiplication is perhaps the most important and most used matrix operation, but it does not have a lot of the properties we take for granted when multiplying scalars, as we shall see shortly.
Before I show you how to actually perform a matrix multiplication, let's go over the important properties. To multiply two matrices A and B (which we will write AB), matrix A must be mxn, and matrix B must be nxp; i.e. the number of columns in the first matrix must equal the number of rows in the second matrix. If they don't, you can't multiply them together.
This leads us to our first important rule of matrix multiplication: matrix multiplication is NOT commutative. 'Commutative' means that the answer is the same regardless of the order you put the numbers in. So addition and multiplication of scalars is commutative:
2 + 5 = 7 = 5 + 2
2 * 5 = 10 = 5 * 2
This is clearly false for matrices, since if A is 2x3 and B is 3x3, we can calculate AB, but we cannot calculate BA. Even if we can calculate both (say if both were 2x2 matrices), that is no guarantee they will be the same, and in fact in the general case they won't be.
The result of multiplying an mxn matrix by an nxp one is a new matrix of size mxp. But what if we want to chain multiplications together - say we have A (mxn), B (nxp) and C (pxQ)? Fortunately, just like addition and multiplication of scalars, matrix multiplication IS associative. 'Associative' means that, while the order the values are written in cannot change, we can do the individual parts of the calculation however we want. For instance:
1 + 2 + 3 = (1 + 2) + 3 = 1 + (2 + 3) = 6
4 * 5 * 6 = (4 * 5) * 6 = 4 * (5 * 6) = 120
So, if we want to calculate the matrix product ABC, we can calculate it as (AB)C or A(BC) and still get the same result, which in this case will be an mxq matrix.
Matrix Multiplication - The MethodSo, those are properties of matrix multiplication, but how do we actually do it? Assuming we have our mxn matrix A and our nxp matrix B, then to calculate AB:
1) For each element (i,j) in the result matrix (remember, i will be between 1 and m and j will be between 1 and p), consider row i from matrix A and column j from matrix B. Note that these will be the same length - length n, in fact - because of the restriction we placed on what sizes of matrices can be multiplied together.
2) Take the first elements from the row and the column in question, and multiply them together. Then take the second elements from the row and column, and multiply these together. Repeat this for all n elements, then add all of these products together.
3) This scalar quantity is the value of element (i,j) of the result matrix. Now repeat this for all the other elements!
Don't worry if you found that confusing! Matrix multiplication takes practice to really 'get'. As an exercise, try multiplying the two above matrices in the other order (we can do this because A is 2x3 and B is 3x2, so both AB and BA are possible in this case) and notice how the result matrices are completely different, both in size and values contained.
We'll see what we can do with matrix multiplication in the next tutorial. In the meantime, let's look at a few properties of square matrices.
The Zero and Identity MatricesThere are two special square matrices we should know. Notice, of course, that these matrices are different depending on the size of matrix we are dealing with.
The 'zero' matrix is a square matrix consisting only of zeroes! For instance, 2x2 and 4x4 zero matrices are:
The zero matrix (obviously) has no effect when added to another matrix, just like adding zero to a scalar quantity.
The 'identity' matrix is an important square matrix - it is the only matrix that does not have any effect when used for matrix multiplication, just like multiplying any scalar by 1. The identity matrix is defined as a square matrix where all elements are 0 except for those on the leading diagonal, which are 1. The 'leading diagonal' is the diagonal line of elements from the top left of the matrix to the bottom right. So the 2x2 and 4x4 identity matrices would be:
The DeterminantAll square matrices have a special property called the 'determinant'. This is a scalar quantity that encodes certain properties of the matrix. The method for calculating it gets tiresome for 3x3 matrices and larger, so we will only cover the 1x1 and 2x2 cases, since the 1x1 case is trivial and the 2x2 one is the only one we will ever need.
Since a 1x1 matrix is really just a single number, that number is the determinant of a 1x1 matrix.
For a 2x2 matrix, the determinant is calculated by:
We shall see one use of the determinant in a moment, and another in the next tutorial.
Inverse MatricesThe final thing we shall cover in this tutorial is the concept of inverse matrices. Only square matrices can be inverted, however not all matrices have an inverse!
The inverse of a matrix, if it exists, is defined as the unique matrix that, when multiplied by the original matrix, gives the identity matrix.
Now, calculating the inverse for any square matrix in a rigorous mathematical way is a bit beyond the scope of this tutorial, and the scope of what we will be using all this matrix stuff for in the next tutorial, so I'm just going to show you the shortcut way of inverting a 2x2 matrix.
Say we have a 2x2 matrix A, such that:
Then its inverse is calculated by:
Note that, since we are dividing by det(A), if the determinant of A is equal to zero, then we cannot invert the matrix! This is in fact the case for all square matrices - a square matrix with zero determinant has no inverse.
Phew, that was a lot of stuff. Don't worry if you didn't quite follow it all - we'll mostly be using this as reference when we do the more interesting stuff next time, but it was important to get this out of the way first.
What are we doing with matrices next time, you ask? Why, linear transformations, of course!
|Sunday, 22 September 2013, 12:42|
It's been a long time since I did matrices, and I never did see their use, but I distinctly recall there being two kinds of matrix multiplication.
I've no doubt they have proper, lengthy names, but I recall them being referred to as "dot" multiplication and "cross" multiplication, in reference to the symbol used to indicate which process.
|Sunday, 22 September 2013, 13:02|
There is only (to my knowledge) one form of matrix multiplication. The 'dot product' and 'cross product' are operations on vectors, not matrices.