In the previous segments, we have studied various aspects of vectors. In this segment, we will talk about matrices, which are also very important. Most transformations involve a matrix multiplying a vector.
Matrices can be used to transform points. The next few lectures will talk about their use for translation, rotation, shears, scales. In fact, all transformations and the graphics pipeline are handled by matrices. First question, what is a matrix. Its just an array of numbers. It has m rows, in this case 3 rows, and n columns, in this case 2 columns.
Operations you can do on a matrix. Addition and multiplication by a scalar are simple. They're just applied to each element of the matrix.
So if you add one matrix to another matrix you just do element by element addition. If you want to multiply a matrix by scalar you just scale up all the elements by a constant, by the scalar.
The most interesting thing is matrix-vector and matrix-matrix multiplication. Note that a vector is a special case of a matrix. In our case, we have been using column vectors, so they have n rows and 1 column.
For matrix-matrix multiplication, the rules are that the number of columns in the first matrix, in this case the first matrix has 2 columns, must equal to number of rows, in this case 2 rows, in the second matrix. So if you multiply matrix m by n times another matrix n by k, the result will be m times k.
Another way of thinking about this is that the element (i,j) in the product is a dot product of row i in the first matrix, and column j of the second matrix. So the condition that the number of columns in the first is equal to the number of rows in the second is simply a condition that we must be able to take dot products, the vectors must be of the same length.
So here, I've shown how the matrix multiplication works. The highlight is on the first row of the first matrix and the result. Note that in element (i,j), so this is the element in the top left, and starting to number my rows and columns from 1, I can say that this is element (1,1).
So it will be a dot product of row 1 and so I can number these rows as 1, 2, 3, and I can similarly number the columns as 1, 2, 3, 4. So it will be a dot product of row 1 and column 1 in order to highlight the different columns here I have used the color red.
Ans so 1 times 3 is 3, 3 times 2 is 6, you add these together in the dot product. It's 9, and that's the value. Notice the color of highlighting so, red is used to highlight the first column here, first column here and so you get the dot product of [1, 3] and [3, 2] for this number. Similarly, for this number here its dot product of [1, 3] and [6, 7] so, 1 times 6 is 6, 3 times 7 is 21, you get 27.
Now the next one, 1 times 9 is 9, 3 times 8 is 24, you get 33. 1 times 4 is 3 times 3 is 9, you get 13.
And similarly, we can apply this to the next row. And so we take the dot product with each column to give each element of this matrix. For example, for the first one, 5 times 3 is 15, 2 times 2 is 4, 19. 5 times 6 is 30, 2 times 7 is 14, 44. 5 times 9 is 45, 2 times 8 is 16, 61. 5 times 4 is 20, 2 times 3 is 6, 26.
And the final example here, the first one is 0, so we don't even need to consider it. 4 times 2 is 8, 4 times 7 is 28, 4 times 8 is 32, 4 times 3 is 12. And this is the way matrix multiplication works.
Matrix multiplication is not commutative. If I reverse the order of the matrices here, it's not even a legal operation. The number of columns in the first matrix, in this case, is 4. The number of rows in the second, in this case, is 3, and it's not commutative. Even if the matrices actually have dimensions that they can be multiplied, it's still not commutative. However, matrix multiplication is associative and distributive. So a * (b+c) is equal to ab + ac. (a + b) * c is equal to ac + bc.
Matrix-vector multiplication is key for transforming the points and this is the main topic we will be treating in the next few lectures. We can treat the vector as a column matrix, m rows times 1 column. And just as a simple example, I've shown you 2D reflection about the Y axis, so let's talk about reflection for a minute.
So I have my screen, this is the X axis, this is the Y axis, and I have a point here, which has some coordinates (x,y). I wish to reflect it about the Y axis to get a point here. And this will have coordinates, (-x,y).
That's what reflection about the y axis is. If you think about this matrix equation, the new x coordinate is -1 times x plus 0 times y, which is -x, and 0 times x plus 1 times y, which is y. So reflection is given by this equation, [-1 0, 0 1], whereas the identity would just have this as plus 1. So this is a simple example of how matrix vector multiplication can be used to transform points corresponding to a particular operation.
The other interesting thing we consider is the transpose of a matrix. And that simply replaces the rows with columns, so you can think of [1 2] being the first row becomes the first column, [3 4] second column, [5 6] third column. We can also transpose a vector in which the column vector becomes a row vector and vice versa.
One interesting property on the matrix transpose, it also applies to matrix inverses, is that, if you want to transpose the product of 2 matrices, A and B, A times B transpose, you just transpose the individual matrices, so you transpose B and you transpose A, but you have to flip the order. So you have to reverse the order in which the transposes are applied. So it will be B transpose times A transpose. That's a very useful property to know.
The identity matrix is useful to define, and so the dimensions of the matrix have noted here 3 by 3. It just has ones along its main diagonal, zeroes elsewhere. And the properties of the identity matrix is the matrix times its inverse is equal to inverse times matrix, is the identity. How do you, do the inverse of A times B, it's A inverse B inverse, but you have to change the order. So if I consider, B inverse times A inverse, times A, times B.
You can see that this becomes the identity, and then B inverse times B, also becomes the identity. So changing the order is in fact, the right thing to do. If I don't change the order, the result is not the identity.
Finally let's relate matrices back to the dot product and the cross product. And we want these vector multiplication operations, a dot b and a cross b to be represented in terms of matrix-vector multiplication.
So a dot b just multiplies the same components of vector a with vector b, and there is a very easy way to represent this in matrices, as a transpose b (a^T * b). So, if the vector is a column vector, the transpose is x_a, y_a, z_a. The number of columns 3 is equal to the number of rows, and the dimensions of the matrix is the number of rows, here 1 and the number of columns, here 1. And so you have x_a * x_b + y_a * y_b + z_a * z_b.
Next, we come to the cross product. We've already seen it when we discussed cross products. You can do it using the dual matrix of the vector a, and so you can represent it as "A star" (A*) times b, where A* is the dual matrix of the vector a.