Research - Matrices
On my journey to learn and become a better developer, I find myself researching matrices. Having never been exposed to matrices I will be approaching this learning as a complete beginner on the topic.
I will hopefully by the end of this blog have a better understanding about: What they are?, How they are used?, Basic operations and more!
Table of Contents
Reason For Researching:
My reasons for researching matrices is due to several points. Other than the obvious points of “Wanting to be a better developer” or “Expand my knowledge”; My real answer is due to their large role being played on my daily life whilst I am blind to them.
What I mean by this is that I mainly spend my working days developing using Unity3D. Within Unity, it is common place for myself to affect an object in some way. Whether I am changing an objects transform via rotation/position I am using Vectors or Quaternions as part of the transform class.
However, several functions within unity use Matrices (Matrix4X4) such as Transform, Camera, Material and more. With this I want to develop an understanding of them
Research, Findings & Development:
What Are Matrices:
Put simply: A Matrix is an array of numbers. They are widely used throughout various sectors & environments. In geometry, matrices are used for representing geometric transformations such as rotations, positions etc.
Matrices are a convenient and compact way to represent large sets of numbers.
Matrix Uses:
Matrices, as mentioned above, are widely used. One such example is in 3D computer graphics. 4x4 transformation matrices are common place. These n+1 dimensional transformation matrices can be called affine transformation matrices, projective transformation matrices or non-linear transformation matrices (dependent on application)
The size of a matrix is defined by the number of rows and columns it contains. There is no limit to the numbers of rows and columns a matrix (in the usual sense) can have.
Matrix Operations:
Matrix Breakdown:
Before I dive into calculating certain operations of a given matrix, first I must break down and understand a matrix. By nature, they are very simple to understand.
As seen from the matrix above, they are very simple to understand. Matrices are written with row first, column second. This means the dimensions of the matrix above are 2x3
The bottom line of A2,3 means in matrix of A find the entry at 23. After going two rows along and 3 columns down the answer is 5.
Below are some more examples of matrix breakdowns. I have written the dimensions for all the following matrices:
Addition:
Now I understand the basic structure of a matrix I can move onto its operations. With addition, It’s vital to point out that the TWO MATRICES MUST be the same size. The rows and columns must match in size else they cannot be added together.
To start adding you must first add the numbers at there matching position. Following the example above you would do 3+6 & 7+2 etc. After adding each value, the output will be the sum of both matrices.
Subtraction:
To subtract matrices, we can simply subtract each value at the corresponding location. It’s the exact same process as adding, except we are subtracting the values
We have to subtract each value at the matching location. Using the example above A1,1 - B1,1 is equivalent to 3-6. This will be position 1,1 of the subtracted matrix output.
Multiplication:
Multiplying two matrices together requires the following condition to bear true:
Columns in the first matrix HAVE to be the SAME value as rows in the second matrixExpanding on this. If we explore the next example it will be made easier to understand
As shown above the columns of matrix A are the same as the rows on matrix B. If this condition does not hold true, then the matrices CANNOT be multiplied together.
the other two values ( A row and B column) provide helpful information. They inform us that the output of the multiplication will be a matrix with 1 row and two columns.
To multiply two matrices we have to start by multiplying the first value at A1,1 with the value at B1,1. We then multiply A1,2 by B2,1. we repeat this until we have multiplied all the values in matrix A with every value in Matrix B.
As seen from the example above the product of AxB is 60 44
Referencing my earlier point of “If this condition does not hold true than the matrices CANNOT be multiplied together.” we can see this is the case when we try to do BxA. As B has 2 columns and A has 1 row, they do not match and thus the multiplication cannot take place.
Scalars
Multiplying By Scalars
Scalar multiplication is also very simple. You take a regular number and multiply it on every entry on the matrix.
As seen from above the matrix A has each value multiplied by 4. The output 4A is shown above.
Addition With Scalars
Adding two matrices that are multiplied by scalars is also simple. That being said the process is a bit longer that just multiplying a matrix by a scalar.
If we consider the following two matrices:
A sample question could follow something along the lines of:
If we break down what is being asked from the question above; 3A simply means multiply matrix A by 3. Add this result to matrix B multiplied by 7. The process will look as follows:
The image above shows that I simply needed to multiply the matrix by the provided scalar. I am now left with two matrices that can be added together.
Subtraction With Scalars
I will not go into as much detail about subtraction as it’s the exact same process as addition. I will just show an image of my workings for subtraction with scalars.
Inverse
Just like a number has a reciprocal ( 8 is 1/8) a matrix has an inverse. The inverse of a matrix is A ^-1. When we multiply a matrix by its inverse we get the identity matrix. ( AxA ^-1 = I)
An Identity matrix has the following properties:
- It is “square”
- It has 1s on the diagonal and 0s everywhere else
- Its symbol is the capital letter I
- Identity matrices can be 2x2, 3x3, 4x4 …
There is not always an inverse of a matrix and thus the following equation defines the inverse and when it can be applied: The inverse of a matrix A is A ^-1 when:
AA ^-1 = A ^-1 A= I
The use of Inverse is significant. Matrices do not have any concept of division. Due to this, multiplying by an inverse achieves the same thing as division.
Inverse Of 2x2
To calculate the inverse of a 2x2 matrix, we start by writing out the formula for calculating an inverse of a 2x2 matrix.
The image above shows the formula we need. The ad-bc part of the equation is called the determinant and will be explored further soon.
To calculate the inverse we need to swap the positions of a & d, put negatives in front of b & c and then divide everything by the determinant.
Given an example data set of the following:
We start by calculating the determinant. This comes to 1/10 once we multiply add and subtract from bc.
Once that has been calculated we simply multiply this by each value in the matrix. This gives us the answer to “what is the inverse if this 2x2 matrix”.
Whilst above is the answer, it is only 50% of the solution. The other half is proving that this is the correct answer. The inverse is only true if this formula holds:
AA ^-1 = I
We use this calculated inverse & apply it to the formula above.
After multiplying and adding the values together the result is the following:
After calculating the values the output is:
Yay! the result is the identity matrix. This proves that the A ^-1 we calculated earlier is indeed the inverse.
Determinant Of A Matrix:
As shown in the previous topic of calculating an inverse for a 2x2 matrix the determinant was shown. The determinant is a special number that can be calculated from a matrix.
For a matrix to have a determinant it must be square. Calculating the determinant for a 2x2 matrix is very simple. Take the following example:
This is how you calculate the determinant for a 2x2 matrix. It is used to help find the inverse of a matrix.
2x2 Matrix Formula:
If we take a matrix of A.
We can use the following formula to work out its determinant.3x3 Matrix Formula:
Whilst calculating the determinant for a 3x3 matrix is slightly more complicated there is a pattern to it.
Above shows what a typical 3x3 matrix looks like. If we use this for calculating the determinant we can use the following formula:
Within the formula, the vertical bars represent the word “determinant”. This makes understanding how to find the determinant of a 3x3 matrix a lot simpler. Additionally, I have drawn a visual representation below:
The image above showcases the way to calculate the 3x3 matrix. It’s similar to that of the 2x2 matrix, except there are a few more steps.
- We start by multiplying a by the determinant of the 2xx2 matrix that is NOT in as row.
- The same is done for b & c. We calculate the 2x2 matrix where b & c are not in the rows
- We then sum up all the results making sure to put a minus symbol in front of the b
This method is the Laplace expansion and is super simple to understand and apply to calculate the determinant of a given matrix
Applying the above learned knowledge to a matrix of B we get the following:
With the matrix above with the values we can apply the formula learned. With this we can calculate the determinant.
The image showcases the determinant of the given matrix B.
4x4 Matrix Formula:
4x4 doesn’t need much explanation. As we are using Laplace’s expansion it’s the same as a 3x3 with a few more steps. The pattern is the following:
- Plus a times the determinant of the matrix not in a’s row or column
- Minus b times the determinant of the matrix that is not in b’s row or column
- Plus c times the determinant of the matrix that is not in c’s row or column
- Minus d times the determinant of the matrix that is not in d’s row or column
As mentioned already, this is very similar to that of a 3x3 matrix. The pattern above is +-+-
The formula to remember is as follows:
Whilst I won’t work out a 4x4 matrix I wil show the initial steps. If we take the following matrix C
We have to take the 4x4 matrix and using the formula split it up in 4 3x3 matrices:
We would then proceed to use the 3x3 formula on each of the given 3x3 matrices.
Inverse Of A Matrix (3x3;4x4):
There are a set of standardised steps in order to calculate the inverse of a matrix. We use the following rules in order:
- Calculate the Matrix of Minors,
- Turn that into the Matrix of Cofactors,
- Then Adjugate,
- Multiply that by 1/Determinant
These rules seem complex/hard to follow but with the example below, it’s easy.
Let’s find the Inverse of A (image below):
We start by calculating the Matrix of Minors. To do this we take each value in the matrix, ignore its column and row and calculate the determinant for the remaining values. Example of this is below. I have calculated the matrix of minors for the first two values in Matrix A
The finished result of all the matrix of minors is the following:
Now we have the matrix of minors we move onto the next step. This is turning the matrix of minors into the Matrix of Cofactors
This is very simple to achieve. We simply follow the pattern of “ +-+-” for the next row we start do “ -+-+” and then switch again back to “ +-+-” We repeat this checkered affect replacing each value with a “ +” or “ -” sign.
Above is the image of the Adjugate process. We transpose all elements of the Matrix of Cofactors. We swap there positions diagonally.
Finally, we have to find the determinant of the original matrix. Whilst I can use the original matrix and calculate the determinant like learned previously, it’s simpler to just multiply the elements of the top row from the original matrix with the top row from the matrix of cofactors. This means we multiply the following:
Elements of Top row: 3, 0, 2 Cofactors for top row: 2, -2, 2
Below is value of the determinant
Below is the final image. This is where we calculate the inverse of the matrix. This is simple using the formula purposed previously.
Understanding & Notes:
My final thoughts and takeaways from my learnings are described below:
- I know what matrices are and there uses
- I can use Scalars to multiply matrices by
- I can apply basic operations when working with two matrices
- I can now successfully apply basic operations for matrices of 3x3, 3x3, 4x4
- I can find the inverse of matrices and why its important
- &MORE