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!

Image of matrix transformations on a 3d axis Image of matrix transformations on a 3d axis

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

Unity matrix example Unity matrix example

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.

Image showing 2x4 matrix Image showing 2x4 matrix

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.

Image showing breakdown of a 2x3 matrix Image showing breakdown of a 2x3 matrix

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:

Image showing breakdown of a multitude of matrices Image showing breakdown of a multitude of 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.

Image showing addition of two matrices Image showing addition of two matrices

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

Image showing subtraction of two matrices Image showing subtraction of two matrices

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 matrix

Expanding on this. If we explore the next example it will be made easier to understand

Image showing A and B matrices dimensions Image showing A and B matrices dimensions

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.

Image showing A and B matrices dimensions Image showing A and B matrices dimensions

As seen from the example above the product of AxB is 60 44

Image showing product of A and B Image showing product of A and B

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.

Image showing AxB works and BxA doesnt Image showing AxB works and BxA doesnt

Scalars

Multiplying By Scalars

Scalar multiplication is also very simple. You take a regular number and multiply it on every entry on the matrix.

Image showing matrix being multiplies by a scalar Image showing matrix being multiplies by a scalar

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:

Image showing two random matrices Image showing two random matrices

A sample question could follow something along the lines of:

Image showing a question of addition with scalar matrices Image showing a question of addition with scalar matrices

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:

Image showing a both matrices multiplied by there scalars Image showing a both matrices multiplied by there scalars

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.

Image showing the final result of the equation Image showing the final result of the equation

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.

Image showing result of two scalar matrices being subtracted from each other Image showing result of two scalar matrices being subtracted from each other

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.

Formula to calculate the inverse of a 2x2 matrix Formula to calculate the 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:

2x2 matrix to calculate inverse with 2x2 matrix to calculate inverse with

We start by calculating the determinant. This comes to 1/10 once we multiply add and subtract from bc.

Image showing determinant being calculated Image showing determinant being calculated

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”.

Answer of the 2x2 matrix Answer of the 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.

Image showing both the A matrix and the inverse of A being multiplied together Image showing both the A matrix and the inverse of A being multiplied together

After multiplying and adding the values together the result is the following:

Image of both matrices multiplied and added together Image of both matrices multiplied and added together

After calculating the values the output is:

Image proving that the matrix of A^-1 is the inverse Image proving that the matrix of A^-1 is the inverse

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:

Image showing how to calculate determinant for 2x2 matrix Image showing how to calculate determinant for 2x2 matrix

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.

Formula to calculate determinant of a 2x2 matrix Formula to calculate determinant of a 2x2 matrix
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.

Image of a 3x3 matrix Image of a 3x3 matrix

Above shows what a typical 3x3 matrix looks like. If we use this for calculating the determinant we can use the following formula:

Image showing formula for calculating determinant of 3x3 matrix Image showing formula for calculating determinant of 3x3 matrix

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:

Image of the formula to calculate determinant visualised Image of the formula to calculate determinant visualised

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:

Image of a sample matrix B Image of a sample matrix B

With the matrix above with the values we can apply the formula learned. With this we can calculate the determinant.

Image of the formula applied to matrix B Image of the formula applied to matrix B

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:

Image of the formula for 4x4 matrix Image of the formula for 4x4 matrix

Whilst I won’t work out a 4x4 matrix I wil show the initial steps. If we take the following matrix C

Image showing sample data for matrix C Image showing sample data for matrix C

We have to take the 4x4 matrix and using the formula split it up in 4 3x3 matrices:

Image showing 4x4 matrix being slit into 4 3x3 matrices Image showing 4x4 matrix being slit into 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):

Image showing 3x3 matrix to find the inverse Image showing 3x3 matrix to find the inverse

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

Image of the matrix of minors for first two values Image of the matrix of minors for first two values

The finished result of all the matrix of minors is the following:

Image of the matrix of minors for matrix A Image of the matrix of minors for matrix A

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

Image of the matrix of cofactors Image of 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.

Image of the Adjugate Image of the Adjugate

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

Determinant of matrix Determinant of matrix

Below is the final image. This is where we calculate the inverse of the matrix. This is simple using the formula purposed previously.

Calculated inverse of matrix A Calculated inverse of matrix A

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

Resources Used: