Introduction to Matrices
We have talked about transformations, projections, spaces and many other cool things belonging to 3D fundamentals. But one of the most important things in this 3D world is to know your way around matrices, we’ll learn why they are used and how we can use them properly to optimize our final code.
What the heck is a matrix?
According to Wikipedia The Free Encyclopedia, a Matrix is defined as follows:
In mathematics, a matrix (plural matrices, or less commonly matrixes) is
a rectangular array of numbers, symbols, or expressions. The individual
items in a matrix are called its elements or entries.
To make things simpler, let’s take a look at the matrix shown below. Just like Wiki said, a Matrix is simply a rectangular disposition of elements, similar to a table. All matrices have size, and it is defined as MxN where M is the number of rows, and N is the number of columns. Hence the matrix on the sample is a 3 by 2 matrix (3 rows, and 2 columns), which can also be written just 3×2. – Most times matrices are written inside square brakets as shown on the figure, but you can also use curly braces and even huge parenthesis.
If the values of M and N are the same (such as 3×3 or 4×4) the matrix is said to be a squared matrix because the number of columns and rows are the same, effectively resembling a perfect square figure. Squared matrices have special properties that will be discussed on following sections.
Each element of a matrix can be any mathematical object (such as a number, an equation, and even another matrix!) each of these elements on the matrix have their own location, and this location is defined by coordinates of the form (i, j) where i is the row number and j is the column number. To follow the example matrix shown above the location of the numbers: 1 is (1, 1), 5 is (2,2), 6 is (3,2), 2 is (2, 1), etc. This is very important to remember and to understand.
Since matrices are mathematical objects, we are able to perform several operations with them just like we do with numbers. With numbers we can add, subtract, multiply, divide and many other operations. With matrices we can also perform operations such as: Addition and Subtraction, Scaling, Transpose, and Multiplication. There are several other operations that can be performed over matrices, but to avoid lengthening too much this lesson we will cover only the basic operations mentioned above.
1.1 Matrix Addition
Addition and subtraction are essentially the same operation, the only difference is simply convenience for us humans, since it is faster for us to evaluate 1 – 2 (written as a subtraction) than 1 + (-2) (written as an addition). To perform matrix addition we need two matrices of the same size. For example, let’s evaluate the following matrix operation:
To evaluate this expression correctly we need to make sure that all matrices are of the same size. The first matrix has size 3×2, and the second is also 3×2 so we can now proceed without issues.
Given two matrices A and B, both of size MxN, the addition of those matrices will produce a matrix C also of size MxN, each element C(i,j) is defined to be: C(i,j) = A(i,j) + B(i,j)
In more easier terms the previous statement means that each element of the resulting matrix C will be the sum of the corresponding element on A and B. So, the result is:
The same principle applies for subtraction. As follows:
1.2 Matrix Scaling
Scaling a matrix is also known as multiplying by an scalar factor, an “scalar” is simply a number. Scaling is a binary operation involving one matrix and one scalar factor. The factor is usually written on the left side of the matrix as a coefficient.
Given a matrix A of size MxN and an scalar factor k, the scalar multiplications of both terms will produce a matrix also of size MxN, where each element of C = k*A is will be k*A(i,j). – In other words, each element of the matrix A will be multiplied by the factor k, and that will be the corresponding element of the resulting matrix C.
1.3 Matrix Transposition
Transposition operation is used to reverse the size of the matrix (including all elements inside it). This is a unary operation in which only one matrix is required. The transposition operation is written using an uppercase T as a super index on the matrix as in: AT
Given a matrix A of size MxN, assuming B is the transpose of A written as B = AT then each element of B is defined as B(i,j) = A(j,i) and the resulting matrix B will have size NxM.
1.4 Matrix Multiplication
Multiplication of matrices is most of times what makes people really angry in just a few minutes, specially when multiplying on paper. It is a lot more difficult than the rest of the matrix operations. So, let’s start already! – Matrix multiplication is a binary operation that requires forcefully two squared matrices of the same size.
Before going further, there is need to define the dot product of two vectors. Imagine we have two vectors A and B for instance A = <1, 2, 3> and B = <4, 5, 6>, the dot product of A and B defined as C = A•B is defined to be the sum of the product of each corresponding element on the vectors, so, C = 1*4 + 2*5 + 3*6, therefore C = 32.
Given two matrices A and B both of size NxN, if C = A*B (matrix multiplication) then the resulting matrix will also be of the NxN, and each element of C is defined to be
C(i,j) = A(row-i) • B(col-j) , where “•” is the dot product operation.
Let’s view the following example, here we are multiplying matrix A (bottom left) with matrix B (top right), the result is matrix C (bottom right).
The definition basically says that for every ROW i in A, we have to take one COLUMN j from B and perform dot product, the result is the element (i, j) of the resulting matrix.
In the example ROW i=1 of A is <1, 4, 7> and COLUMN j=1 of B is <1, 2, 3>, the dot product operation explained is 1*1 + 4*2 + 7*3 which results in 30. This final result is the element (1, 1) of the result matrix.
Matrices can be a bit complicated at first, but once you get the grasp of them you will see how useful they actually are. Matrices will save us a lot of time, and they have a lot of uses, some of those uses apply to 3D algorithms as well and will be detailed on our next lesson.
Make sure you understand all matrix operations, specially multiplication. Doing that will be give you great advantage on the next lesson “Affine Transformations”.
Matrices are scary at first, but they are your friends. 🙂