Linear Algebra 1: Introduction and matrix multiplication
(This tutorial post is not finished, I will update this post, and you will always be able to find it from the tutorials page above)
Linear Algebra is usually learnt at undergraduate university classes, so don't be surprised if you didn't learn this at high school. Anyway, just because it's learnt at a university level doesn't mean it's super difficult. And the funny thing is that you will not need any high level high school maths such as differential equations and whatnot to understand it. (If you wish to learn more linear algebra, I suggest you watch these video lectures from MIT with Gilbert Strang. Or, read this text, aimed at game developers, Learning Modern 3D Graphics Programming, by Jason L. McKesson)
Linear Algebra can be used for a great many things, but what we need it for are just a few quite simple things.
First, we're going to need to know what Matrices and Vectors are, and then we need to know how to multiply them. This is because we use matrix multiplication when we calculate where to print our polygons on the screen.
Vectors and Matrices?
I will begin by explaining what a vector is. A vector is a value, that has numbers in more than one dimension. This opposed to the simple numbers (called scalars) that most of us are used to, that explain a certain amount in a certain dimension. For example: 4.53 meters, 10 apples, or 3 triangles.
A vector contains information about several dimensions, in high school or primary school you are usually introduced to a coordinate system, where we describe a position as 2 numbers, for example (3, 5). This position is a vector with two dimensions, we might call them x and y.
Vectors can have any number of dimensions, if we want to explain a position in 3D space we will need 3 dimensions, if we want we can call them x, y, z.
Let's define a point as p = ( 1, 2, 5). To know what position this vector is explaining, we need to have a coordinate system.
A coordinate system defines the directions of these different dimensions. And if these directions are all perpendicular to each other (the angle between them being 90degrees) this is a Cartesian coordinate system. (there are left and right hand systems, but lets ignore this.)
Up to this point is explained in the DirectX SDK aswell, but what about Matrices?
A matrix hold many vectors, or many different values for many different dimensions.
1 2 3 5 0
A = 1 0 0 1 2
3 0 0 2 0
This matrix holds 5 different values for each dimension x, y, z. That's all a Matrix is, just a collection of values. Note that the first row is not "worth" 12350 or anything, it's just 5 different values that have nothing to to with each other.
Now the only thing we need to learn is how to use matrices and vectors, and to do this, we need to learn the mathematical rules that apply to them. For example, what is A + p? maybe p*A? or what about A*A or p+p?
Addition and Subtraction
Vectors can be added to and subtracted from vectors with the same amount of dimensions, and the same goes for matrices, but we can't add a vector to a matrix. It's quite straight forward and works kind of like what you'd expect.
if p = (0, -1, 5), q = (3, 2, 7)
then p + q = (0+3, -1+2, 5+7) = (3, 1, 12)
and p - q = (0-3, -1-2, 5-7) = (-3, -3, -2)
2 3 -1 2
A = 2 4 and B = 1 0
-2 0 5 4
A + B = 3 4
Multiplication is a bit different. There are two ways to multiply vectors, there is dot product and the cross product, but I'll ignore them for now because I don't think we need them.
Which takes us to the important part of this tutorial, multiplying matrices with other matrices and or vectors. Let's start with the multiplication of matrices. (Also see wikipedia)
Matrices have m rows and n columns, and can only be multiplied if the left(the order matters) matrix columns n is equal to the right matrix rows m, and this results in a left matrix m by right matrix n matrix. For example, a 3 x 4 matrix can be multiplied with a 4 x 2 matrix, and results in a 3 x 2 matrix.
The individual values in the obtained matrix is calculated as follows.
The value at row m, col n, is given by the values in the left matrix row m multiplied with the values in the right matrix col n, one by one, and then added together. I'll show a simple example:
2 x 3 * 3 x 4 = 2 x 4
1 2 3 1 2 3 4 a b c d 38 44 50 56
4 5 6 * 5 6 7 8 = e f g h = 83 98 113 128
9 10 11 12
The individual values are calculated like this:
a = 1*1 + 2*5 + 3*9 = 38
b = 1*2 + 2*6 + 3*10 = 44
c = 1*3 + 2*7 + 3*11 = 50
d = 1*4 + 2*8 + 3*12 = 56
e = 4*1 + 5*5 + 6*9 = 83
f = 4*2 + 5*6 + 6*10 = 98
g = 4*3 + 5*7 + 6*11 = 113
h = 4*4 + 5*8 + 6*12 = 128
Try it yourself, and see if you can calculate this:
1 0 0
A = 1 2 0
0 0 3
b = -1
A*b = c
the answer is:
1*5 + 0*-1 + 0*3 5
c = 1*5 + 2*-1 + 0*3 = 3
0*5 + 0*-1 + 3*3 9
And that, is how you multiply a matrix with a vector, simple, isn't it?
Now, you might have guessed this by now, but a matrix looking like this:
1 0 0
A = 0 1 0
0 0 1
would not change b at all. This is called an identity matrix. I will explain more of their usage further down.
Now we've come to what we really wanted to learn, and do. We wanted to use a matrix to transform vectors. Or to put it in more direct terms, we wanted to move, rotate and scale 3d models(collections of vectors, or points) in space. And we can to this, just by making a suitable matrix A to multiply with all our vectors b.
We could move all our points the same amount in space by adding an offset vector to all of our points. But this would take time(I'll explain why later). Instead, we introduce a 4th dimension! This might sound like everything will get super advanced, I mean, thinking in more than 3 dimensions are beyond most of us, but it's actually quite easy, so don't be alarmed. Even if we have another dimension, we will keep all our points at the same place in that dimension. So when doing calculations, our data points will look like this: p = (x, y, z, 1). They'll have a coordinate in all our regular x, y, z dimensions, but also everything will be on "1" in the forth dimension.
Instead of trying to explain why, I'll just show you. We have a point at (3, 4, 7) and we want to move it -5 in all dimensions
p = 4
1 0 0 -5
A = 0 1 0 -5
0 0 1 -5
0 0 0 1
1*3 + 0*4 + 0*7 +1*-5 3-5 -2
This results in A*p = 0*3 + 1*4 + 0*7 +1*-5 = 4-5 = -1
0*3 + 0*4 + 1*7 + 1*-5 7-5 2
0*3 + 0*4 + 0*7 + 1*1 1 1
Wasn't that easy? we moved our data point exactly as much as we wanted by multiplying it with a nice and simple matrix.
Now all we need is a scaling and a rotation matrix. I will add these soon.