Matrix multiplication is a fundamental matrix operation. Modern game engines are built with the help of matrix multiplication, since it's closely related to geometry. Apart from that, it's also used in machine learning, for example, to discover what features of studied objects are the most important. In every subject, where matrices come into play, matrix multiplication plays an important role.
Multiply two matrices
Before multiplying matrices, you should find out whether you can multiply them or not. It may seem odd if you've never worked with matrices, but after some practice, you'll get used to it. So, to define multiplication:
The number of columns of the st matrix must be equal to the number of rows of the nd matrix.
The result will have the same number of rows as the st matrix, and the same number of columns as the nd matrix.
Let's move on and multiply matrices and To do this, you need to know the dot product of their rows and columns. You've already learned about vector dot product and how to calculate it. Now you're going to deal with the same thing but for matrices. In case of matrices, a dot product is a sum of the products of corresponding elements from a row of the first matrix and a column of the second, for example, the 1st row and the 1st column:
is the dot product of the st row and the st column.
What is the dot product of the st row and the nd column?
It is .
What about the dot product of the nd row and the st column?
In the same way, the dot product of the nd row and the nd column equals . So the result of the multiplication of matrices and will be:
Such things might be hard to remember, so let's look at a real-life example.
Example
A local shop sells three types of useful things:
AR glasses cost each
Hoverboards cost each
USB pet rocks cost each
This table shows how many items were sold in four days:
| Tuesday | Wednesday | Thursday | Friday |
AR glasses | ||||
Hoverboard | ||||
USB pet rock |
The value of sales for Tuesday is calculated this way: AR glasses Tuesday value Hoverboard Tuesday value USB pet rock Tuesday value
So it is, in fact, the dot product of the prices and the number of sold items on Tuesday:
We match the price to the number of sold items, multiply each, then sum up the result.
We do the same for other days:
Wed =
Thu =
Fri =
So what just happened here in terms of matrices?
To multiply matrix by matrix, must be the same, and the result is matrix. In our case we did
Order of multiplication
What about other matrices? Let's multiply the matrices by each other and check it out.
If we swap these matrices and multiply again, what happens then?
Wow! The results are different! Forget about the commutative law in multiplication: it usually does not work for matrices. Even if you're used to , in matrix notation . The order is very important for the multiplication of matrices. There may be some cases when the commutative law works, but they're quite unique. We'll discuss one of them later.
Multiplication by a diagonal or a scalar matrix
Let's consider two special cases of matrix multiplication: multiplication by a diagonal and a scalar matrix.
Multiplication by a diagonal matrix multiplies all rows by corresponding leading entries of the diagonal matrix:
Why is it so? The dot product of the st row of the diagonal matrix and the st column of the right matrix, the only non-zero element is equal to :
The same thing happens with all other dot products calculated during matrix multiplication.
Right multiplication by a diagonal matrix multiplies all columns by corresponding leading entries of the diagonal matrix:
The explanation here is nearly the same as in the previous case. The dot product of the st row of the left matrix and the st column of the diagonal matrix, the only non-zero element is equal to :
Here, the same thing also happens with all other dot products calculated during matrix multiplication.
And what about a scalar matrix? Since it's a diagonal matrix in which all the principal diagonal elements are equal, multiplication by it would multiply all rows or columns by the same value. Therefore, the resulting matrix would be a matrix, all entries of which are equal to the entries of the original matrix multiplied by a scalar, hence the name.
Multiply matrix and vector
A vector is an matrix, where is a natural number, so it is also a case of matrix multiplication. Usually, you use a or -dimensional space, so in most cases equals or .
Let's consider that you use a -dimensional space and have a matrix and a vector . Here, the number of columns of the matrix is equal to the number of rows of the vector , so you can find their product.
In this case, the result is a vector. In general, if you multiply an matrix by an vector, you'll always get an vector.
Can you change the order and multiply a vector by a matrix? Every vector has only one column, so in order to be able to multiply a vector by a matrix, you need a matrix where is a natural number. You can obtain this matrix by transposing an vector! So, you can multiply a vector only by another transposed vector, but not by any matrix. Here's an illustration of such multiplication.
Identity matrix & Commutative law
Do you remember the rule from mathematics ? Is there such a rule for matrices? Sure! It is the Identity matrix denoted as . When we multiply a matrix by the Identity matrix, the commutative law applies:
Check it out!
Matrix multiplication from childhood
Matrix multiplication is a beautiful mathematical process. Remember the multiplication table from school?
In fact, in matrix form, it looks like:
Don't believe it? Now you have enough knowledge to verify that.
Multiplying many matrices
How to multiply more than two matrices? Imagine that you have three matrices , and . In order to find , you have to multiply by and after that, multiply the result by .
Let's consider an example. Let , and . In this case
And the final result is
Conclusion
Let's summarize what we have learned today, all this knowledge is fundamental when working with matrices in further topics:
Dot product represents the addition of corresponding elements from a row of a first matrix with the corresponding element of a column of a second matrix.
For us to be able to multiply matrices, the number of columns of the first matrix has to be equal to the number of rows of the second matrix.
When multiplying two matrices, the resulting matrix will have the same number of rows as the first matrix and the same number of columns as the second one.
Commutative law does not usually apply when multiplying matrices. If you multiply a matrix by matrix , you will not always get the same matrix as from multiplying matrix by matrix .