In my tutorial that discussed the derivation of rotation matrices I gave a "quick and dirty" introduction to matrices. After reading it just now I realize how bad it was. This time I am going to make right what that tutorial did wrong. And since this is a tutorial about only matrices there are no prerequisites (yea!). This will also be a much shorter tutorial compared to my other ones and hopefully pretty in depth. Pair this up with my vector tutorial and you have a somewhat good introduction to Linear Algebra (or at least a Alg.II/Pre-Calc./Calculus class). First we discuss some fundamentals that I hope you learned in an Algebra class, but I think is worth going over again, and that's properties of operations. Some of these are really obvious and others are even more obvious, but we need terms to stick to them so I can just call out a word and you already what it means...and there are going to be a lot of terms in this part. Note: these have nothing to do with matrices as of yet. Properties of addition first: Associative Property of Addition
(a + b) + c = a + (b + c)
Commutative Property of Addition
a + b = b + a
Additive Identity
The additive identity, I, is a number which when added to
any other number, a, will not change a. For addition the
identity is zero because: a + I = a and a + 0 = a
Now I bet you are seeing just how obvious and fundamental that is. But, in any case you need to know it. Next is multiplication: Associative Property of Multiplication
(a * b) * c = a * (b * c)
Commutative Property of Multiplication
a * b = b * a
Distributive Property of Multiplication
a * (b + c) = (a * b) + (a * c)
Multiplicative Identity
The multiplicative identity, I, is a number which when
multiplied by any other number, a, will not change a.
For multiplication the identity is one because: a * I = a
and a * 1 = a
Once again pretty simple. But, just one more: Inverse Operation
An operation that "reverses" another operation. Addition
and subtraction are inverses of each other, as are multiplication
and division.
So you've learned a few properties. So what? Well it's important
to know these properties when we start examining the properties of
matrices. Now for the actual matrix stuff. | M11 M12 M13 ... |
M = | M21 M22 M23 ... |
| M31 M32 M33 ... |
| ... ... ... ... |
You can now easily refer to any element by simply saying "M11" instead of "the element in the first row and the first column." And when writing things down the above matrix is usually denoted as [M]. The above matrix does not have any set dimensions though because I wanted to show you what one looked like and how you can refer to its elements. If we changed [M] into, say, a 3 x 4 matrix it would look something like this: | 2 4 1 2 |
M = | 1 6 8 1 |
Those numbers I put there are just random also so do not worry about
that. Now, since you understand how to refer to certain elements by their
place in the matrix you should be able to answer a few things. Like what
is the value in element M23? Go ahead and figure it would real
quick. Just so you can check yourself the answer is 8. | a11 a12 | | b11 b12 |
A = | a21 a22 | B = | b21 b22 |
| a31 a32 | | b31 b32 |
| a11 + b11 a12 + b12 |
[A] + [B] = | a21 + b21 a22 + b22 |
| a31 + b31 a32 + b32 |
Everything should be pretty clear so far. I am merely adding corresponding elements in both the matrices. I find A11 in [A] and B11 in [B] and add the two and then that's the first element in the sum matrix. Some properties which of matrix addition, which you should see, is that it is commutative and associative. Here is an example with actual numbers: | 2 5 | | 7 3 |
A = | 9 4 | B = | 1 3 |
| 2 5 | | 2 7 |
| 2 + 7 5 + 3 | | 9 8 |
[A] + [B] = | 9 + 1 4 + 3 | = | 10 7 |
| 2 + 2 5 + 7 | | 4 12 |
Hopefully that will clear up any confusion you might have. The next
arithmetic operation would be subtraction. But, this time I'm not going to
give any examples because it is basically the same as addition. When
subtracting two matrices you subtract their corresponding elements. Just
use all the above examples except replace the "+'s" with "-'s". | M11 M12 M13 |
[M] = | M21 M22 M23 | x = scalar
| M31 M32 M33 |
Multiplying a matrix by a scalar is pretty easy right? Here is an example with numbers: | 3 5 |
[M] = | 9 1 |
| 1 4 |
| 9x 15x |
3x * [M] = | 27x 3x |
| 3x 12x |
And that's all there is to it. Now here comes something a little
harder: multiplying two matrices. Just like when adding and subtracting
matrices, for multiplying you must first check if the two matrices meet
certain requirements. In the case for addition and subtraction the two
matrices had to have the same dimensions. This time, however, if you were
multiplying [A] by [B] then [A] must have the same number of
columns as [B] has rows. In general, [A] must have the dimension m
x p and [B] must have the dimensions p x n. All three
variables (m, n, and p) can be any number. | b11 |
[A] = | a11 a12 a13 a14 | [B] = | b21 |
| b31 |
| b41 |
[A] * [B] = [P]
The product of [A] and [B] is another matrix, [P]. Matrix [A] has the
dimensions of 1 x 4 and [B] that of 4 x 1. Are they capable of being
multiplied? Yes, because [A]'s number of columns equals the number of
rows in [B]. When you finally get around to the actual multiplication the
product, [P], will have the dimensions of the number of rows in [A]
by the number of columns in [B]. In this case it will be a 1 x 1
matrix which is just a number. No need for the brackets in this
case. [B] = | b11 b21 b31 b41 |
[A] = | a11 a12 a13 a14 |
Something important to note here is that I kept the element names for [B] the same even though I changed the dimensions of the matrix. Do not let that confuse you. Ok, so this is what you are going to do next. All the elements that line up vertically you are going to multiply them together. Then once you have all of those values you are going to add them. So we can change the above to this: [B] = | b11 b21 b31 b41 |
[A] = | a11 a12 a13 a14 |
---------------------------------------------------
[P] = (b11*a11) + (b21*b12) + (b31*a13) + (b41*a14)
So that is another way of doing things. Now the reason I showed you this is because all situations where you are multiplying two matrices you can break it down into a bunch of instances like the above, sometimes making it easier to evaluate the problem. Before we move on let's have a sample problem. Try to figure this out on your own: | 5 |
[A] = | 2 5 7 | [B] = | 6 |
| 2 |
[A] * [B] = ?
Try to figure it out before you read anymore. If you have already figured it out then...I...got...54 as an answer. Now you know everything there is to know about multiplying a row by a column. However, when you get into matrices with larger dimensions there is still one more thing to learn. Like always, let's look at a generalized example real quick: | a11 a12 a13 | | b11 b12 b13 |
[A] = | a21 a22 a23 | [B] = | b21 b22 b23 |
| b31 b32 b33 |
[A] * [B] = [P]
A brief description of what we have going on here. Two matrices. [A]
with dimensions of 2 x 3 and [B] with dimensions of 3 x 3. They are
capable of being multiplied as you should be able to tell by now. The
product matrix will have the dimensions of 2 x 3. Now let's try to find
the elements of the product matrix. We are going start out by finding
p11 (the first element in the product matrix). To find that
element you are going to use the same method we discuss above with the
first row in [A] and the first column [B]. After that you solve for
p21 (directly below p11). To do this use the same
column from [B], but this time multiply it with the second row of [A].
Since your product matrix only has two rows you would stop there and go on
to the next column. Now you do the same thing as before except use the
second column in [B] and multiply it by the first and second row of
[A]. | 4 1 6 | | 4 6 1 |
[A] = | 3 6 9 | [B] = | 5 8 2 |
| 2 5 8 |
[A] * [B] = [P]
Ok, let's just take it one step at a time. The first thing you should
always ask yourself is whether or not the matrices can be multiplied by
each other. In this case, of course, they can. Next, you want to find out
what the dimensions are going to be for the product matrix. The above
matrices will produce a 2 x 3 matrix when multiplied. | 4 1 6 | | 4 6 1 |
[P] = | 3 6 9 | * | 5 8 2 |
| 2 5 8 |
| (4*4 + 5*1 + 2*6) (6*4 + 8*1 + 5*6) (1*4 + 2*1 + 8*6) |
= | (4*3 + 5*6 + 2*9) (6*3 + 8*6 + 5*9) (1*3 + 2*6 + 8*9) |
| 33 62 54 |
= | 60 111 87 | < -- final answer
And that is the final answer. Did you get it right? Let's hope so
because this is the best I can explain it. This also wraps up arithmetic
with matrices. If you thought this was hard then you are going to hate
what we are going to do near the end: inverses and determinants. But to
get you ready for the rest of this stuff I made a little review for you. I
made three Flash movies which will give you some more practice with the
arithmetic. Go here
to practice your matrix addition. Go here
to practice matrix subtraction. And finally, go here
to practice matrix multiplication. Those file will create matrices with
random dimensions and random elements. Try to solve a few. If you cannot
get it there is a button to solve the problem. It's is wierd. I had a
really hard time making those. The multiplication file was one of
the, if not the, hardest things I have ever done. | 0 0 0 ... |
[I] = | 0 0 0 ... |
| 0 0 0 ... |
| ... ... ... ... |
Now remember that the whole foundation of an additive identity is based solely on the fact that this will always hold true: [A] = any arbitrary matrix
[I] = additive identity matrix (with same dimensions as [A])
[A] + [I] = [A] and [I] + [A] = [A]
That should be easy enough to remember. The multiplicative identity is a little harder to come up with. The multiplicative identity matrix has square dimension. It will always have dimensions of n by n. All of the the identity's elements are zero except for the main diagonal of elements going from top left to bottom right...those elements are one. It looks like this: | 1 0 0 0 ... |
| 0 1 0 0 ... |
[I] = | 0 0 1 0 ... |
| 0 0 0 1 ... |
| ... ... ... ... ... |
Whenever you multiply the above matrix with any other matrix the other matrix will not change. Once again this will always hold true: [A] = any arbitrary matrix
[I] = additive identity matrix (with same number of rows as [A]'s column)
[A] * [I] = [A] but not necessarily this, [I] * [A] = [A]
Remember that the two matrices still have to follow the rules of
multiplication for them to have a product. 1.) Switching around two rows
2.) Multiplying a row by a constant
3.) Adding a row times a constant to a different row
Please do not get confused with this stuff because it should be really easy. When I first heard this part right here I was entirely confused (partly because of a not so great teacher). Just think of it as adding/subtracting/dividing/multiplying numbers except within a matrix. But, don't add your own operations...only the ones above. An example of the above row operations would probably help: 1.) Switching around two rows
| 4 6 9 |
| 1 9 7 | = original matrix
| 4 1 2 |
Switch rows two and three
| 4 6 9 |
| 4 1 2 | = matrix with rows switched
| 1 9 7 |
2.) Multiplying a row by a constant
| 2 3 5 |
| 3 3 6 | = original matrix
| 2 7 4 |
Multiply the first row by 3
| 12 18 15 |
| 3 3 6 | = new matrix
| 2 7 4 |
3.) Adding a row times a constant to a different row
| 5 1 7 |
| 2 1 2 | = original matrix
| 8 2 4 |
Summary: we are going to multiply row three by 4 and then
add the entire row to row one without changing
the original row three.
First step: find out what row three times 4 equals
| 32 8 16 | = row three times four
Second step: add the above row to row one of the first matrix
| 37 9 23 |
| 2 1 2 | = matrix after the row operation
| 8 2 4 |
Note: the third row does not change even though
you multiply it by a constant...only the first.
See how simple those can be? We are now going to use this seemingly useless technique to row reduce a matrix. When you are row reducing a matrix you are wanting to change a matrix so that its first m by m elements form the identity matrix. Once you have it in that form the matrix is said to be in reduced row-echelon form (well not *always* but we will never get into anything were it is not). There are specific steps you have to take to row reduce a matrix. I will outline them here: 1.) Use row operations (above) to get a one in element "11"
2.) Fill all of the first column with zeros except for the first row
by adding the first row times a constant to each other row (#3 above).
3.) Use row operations (still above) to get a one in element "22"
4.) Fill all of the second column with zeros except for the second row
by adding the second row times a constant to each other row (#3 above).
5.) As you can see its just regurgitation. Just do the above over and
over until you get the first m by m elements in the matrix to equal
the identity matrix.
Most likely the above does not mean much to you unless you see an example. The following is just some matrix I found on a website. It is going to get quite long so pay close attention. I had actually, at first, made up my own matrix to row reduce but at the end the fractions were so crazy that I decided not use it and I just took a sample problem from a site. I tried to comment my steps thoroughly: Row reduce the following matrix:
| 4 4 3 2 0 -2 |
[M] = | 2 3 0 13 1 18 |
| -2 -2 -1 -3 0 -2 |
- I picked a kind of hard one. If you understand this then you should
have no problem with any of this. The first thing you want to do is
get a one for M11. To do this we are going to use the row operation #2
and multiply row one by 1/4:
Row1 * 1/4
| 1 1 3/4 1/2 0 -1/2 |
= | 2 3 0 13 1 18 |
| -2 -2 -1 -3 0 -2 |
- Note how I kept everything in fractional form. Fractions are far
easier to work with than decimals. If you are not used to working
with them then do so quickly.
- Next thing we want to do is try to get a zero for M21 and M31. Lets
start with M21. To do this we are going to use row operation #3 by
adding the first row multiplied by a constant to the second row. But,
what constant? It needs to be something so that when M11 is
multiplied by it it will equal the negative of M21. In this case the
constant is going to be -2. Note that row one is not going to change
in this step. Here is the matrix after what I described above:
Row2 + (-2 * Row1)
| 1 1 3/4 1/2 0 -1/2 |
= | 0 1 -3/2 12 1 19 |
| -2 -2 -1 -3 0 -2 |
- That part can be hard to take in at once. The best thing to do is
try what I describe yourself and then check your work with mine.
- Now we need to get M31 to have a zero too. To do this we will use the
same method as in the previous step except this time, the constant
which row one will be multiplied by is going to be 2. Here is the
matrix after that:
Row3 + (2 * Row1)
| 1 1 3/4 1/2 0 -1/2 |
= | 0 1 -3/2 12 1 19 |
| 0 0 1/2 -2 0 -3 |
- We are done with the first part! We got the first column to resemble
the identity matrix. Next, we need to get the second column to do the
same. We are going to start off by getting a one for element M22.
But, hey! There already is a one! Yes there is. Happy birthday, you
can just skip that step.
- But still not finished. We need to get all zeros in the second column
except for element M22. And yes, element M32 already has a zero
we do not even need to mess with it. M12 still has a non zero number
so to make it a zero we are going to do row operation #3. Last time
we added the first row times a constant to the other rows. This time
we are going to add the second row times a constant to the other rows.
If you look at the numbers you will see that the constant should be
a negative one. Another way to look at is to simply subtract row two
from row one:
Row1 + (-1 * Row2) or Row1 - Row2
| 1 0 9/4 -23/2 -1 -39/2 |
= | 0 1 -3/2 12 1 19 |
| 0 0 1/2 -2 0 -3 |
- Now the first and second columns resemble the identity matrix. We are
almost done. Only the third column left to deal with. But, let's
still take it one step at a time. First we need a one in element
M33. To do this we are going use row operation #2 and multiply
the third row by 2:
Row3 * 2
| 1 0 9/4 -23/2 -1 -39/2 |
= | 0 1 -3/2 12 1 19 |
| 0 0 1 -4 0 -6 |
- Next we are wanting to get the entire third column full of zeros
except for element M33. We are going to use row operation #3 once
again. Let's just deal with getting M13 to equal zero for now. This
time we are going to add row three times a constant to row one. The
constant will be -9/4:
Row1 + (-9/4 * Row3)
| 1 0 0 -5/2 -1 -6 |
= | 0 1 -3/2 12 1 19 |
| 0 0 1 -4 0 -6 |
- The best thing for you to do is to work it out yourself now that I
think about it. This is not easy stuff. The arithmetic is elementary
but putting everything together is a little harder.
- Well, this is our last step. All we need to do is get a zero for
element M32. To do this we are going to use row operation #3. The
constant this time will be 3/2:
Row2 + (3/2 * Row3)
| 1 0 0 -5/2 -1 -6 |
= | 0 1 0 6 1 10 |
| 0 0 1 -4 0 -6 |
Be proud of yourself if you made it through that! The last matrix is in
reduced row echelon form. Also don't get that part confused: the method we
just used is called row reducing and the form of the matrix after
row reduction is called reduced row echelon form. [A] = original matrix [A]T = transposed matrix // the transposed matrix
is denoted with a superscript "T"
[A] = | 3 5 7 2 1 | | 3 |
| 5 |
[A]T = | 7 |
| 2 |
| 1 |
So, something which can be quickly observed is that when transposing a row or column matrix you simply "flip" the matrix upright or sideways. A higher order matrix is almost the exact same thing. The best way to approach transposing a higher order matrix is to: 1.) draw out the brackets for the new matrix 2.) look at the first column of the original matrix, flip it ninety degrees counterclockwise 3.) put that in for the first row of your new matrix 4.) do again and again until finished. An example always helps: Transpose the following matrix:
| 6 4 8 1 4 2 |
[A] = | 9 1 4 3 8 5 |
| 3 6 1 3 6 1 |
- First look at the dimensions of the matrix. It is a 3 x 6 matrix, so
that means that the transposed matrix will by 6 x 3.
- Next, we are going to go through all the steps I discussed above.
Except I am going to put it all in one step because I don't want
to waste any more space:
| 6 9 3 |
| 4 1 6 |
| 8 4 1 |
[A]T = | 1 3 3 |
| 4 8 6 |
| 2 5 1 |
Nothing difficult. But so far we have only dealt with matrices which had a number of rows that were different from the number of columns. If a matrix has the dimensions of n by n (a square matrix) then you simply reflect the elements across the main diagonal (from top-left element to bottom-right element). An example of a 3 x 3 matrix: | 3 6 1 |
[A] = | 9 1 4 |
| 7 2 4 |
| 3 9 7 |
[A]T = | 6 1 2 |
| 1 4 4 |
As you can see the main diagonal did not change. All the other elements
were only reflected across that diagonal. [A] = | 3 |
det [A] = |A| = 3
-- read as the determinant of [A]
-- note that you have two notations. If you were doing work on a piece
of paper you would use straight lines to denote the determinant of a
I'll discuss how to find the determinant of higher order matrices in a
second, but I should probably explain this more. If a matrix has a
determinant of zero then it is not capable of being inverted. This can be
easily proven using the simplified case I gave above of a 1x1 matrix. The
inverse of the above matrix would simply be its only element's reciprocal,
so in this case it would be 1/3. If the element was zero you would have
1/0 and you cannot divide by zero. [A] = | A B |
| C D |
det [A] = A*D - B*C
That was a generalized example that way you can see whats going on
better. Now something I have been big on in all of my tutorials was that
it is far better knowing how to come up with a formula than
just memorizing it; thats probably the reason my tutorials get as long as
they do. However, this time I am not going to explain how this equation
came about. Mainly, because with the approach that we are taking I am not
sure there is a derivation. I've explained everything how it would be
taught in an Algebra class. The derivation of the determinant (as I know
it) comes from a geometric perspective. Perhaps one day I will write how
to derive the determinant in a geometric sense for those who *must* know
but for now I am going to keep Algebra and Geometry separate (even though
that, in a sense, is bad since everything goes together in the end
anyway). [A] = | 3 5 |
| -1 7 |
det [A] = 3*7 - 5*(-1) = 21 - (-5) = 26
So from the determinant we can tell that [A] is: 1.) invertible 2.)
anisotropic. | a11 a12 a13 |
[A] = | a21 a22 a23 |
| a31 a32 a33 |
-- next copy the first two columns of the matrix to the right
of the original matrix.
| a11 a12 a13 | a11 a12
[A] = | a21 a22 a23 | a21 a22
| a31 a32 a33 | a31 a32
-- Now you are going to add up the products of all the diagonals
(starting at a11) going from left to right and subtract the
products of the diagonals (starting at a13) going from right
to left:
| a11 a12 a13 | a11 a12
[A] = | a21 a22 a23 | a21 a22
| a31 a32 a33 | a31 a32
det [A] = [(a11*a22*a33) + (a12*a23*a31) + (a13*a21*a32)] -
[(a13*a22*a31) + (a11*a23*a32) + (a12*a21*a33)]
And that is pretty straight forward. Here are some general steps to finding the determinant of a m x m matrix: 1.) Copy the first (m - 1) columns to the right of the original matrix
2.) Add up the products of the diagonals going from left to right. Note:
the diagonals should have a total of three elements.
3.) Add up the products of the diagonals going from right to left. Once
again the diagonals should have three elements.
4.) Subtract the sum of the second diagonals from the sum of the first.
You should now know how to find the determinant of any m by
m matrix. We are nearing the end of the tutorial now. All that's
left are inverses. | A B C |
[A] = | D E F |
| G H I |
// extended matrix:
| A B C 1 0 0 |
[A] = | D E F 0 1 0 |
| G H I 0 0 1 |
Next you are going to row reduce the matrix into reduced row echelon form: | 1 0 0 A' B' C' |
[A] = | 0 1 0 D' E' F' |
| 0 0 1 G' H' I' |
// Note that A', B', C', ... are different numbers from A, B, C, ...
It would be hard to show a generalized version of what those numbers
are so I just denoted the change with a single quotation.
Once you have the matrix row reduced you drop the first m columns and that is your inverse matrix. I believe a sample problem is in order here: Find the inverse of the following matrix:
| 2 3 |
[A] = | -4 8 |
-- The first thing to do is to write down the new matrix when the
identity elements are added in.
| 2 3 1 0 |
= | -4 8 0 1 |
-- Now we start row reducing. The first thing you want to do is get a
one for A11. To do this we are going to multiply the first row by
one-half:
| 1 3/2 1/2 0 |
= | -4 8 0 1 |
-- Next we want to get a zero for A21. This is done by adding the first
row times a constant to the second row. The constant in this case
is going to be four:
| 1 3/2 1/2 0 |
= | 0 2 -3 1 |
-- We now want to get a one for element A22. To do this we are going
to multiply the second row by a constant. And the constant will be
one-half again:
| 1 3/2 1/2 0 |
= | 0 1 -3/2 1/2 |
-- And finally we need to get a zero for element A12. This will be done
by adding the second row times a constant to the first row. The
constant this time will be -3/2:
| 1 0 11/4 -3/4 |
= | 0 1 -3/2 1/2 |
-- You can now take away the identity part of the matrix and you have
your inverse:
| 11/4 -3/4 |
[A]-1 = | -3/2 1/2 |
And that's all there is to inverses. Hopefully it does not seem so
hard. Inverses also concludes this tutorial. After writing this I *really*
want to write another from a geometric point of view so you can see how
useful matrices and determinants are. Hopefully I will. As of right now I
have a tutorial on two dimensional collision physics almost entirely done
and then after that I have planned on writing about kinematics and
mechanics. But we'll see. |