Tutorials Articles Misc

Crash Course in Linear Algebra [Wednesday, November 20th, 2002 - Alex]

A matrix can be thought of a shorthand representation for many equations.

For example, the following system of equations can be expressed as a matrix:

1x + 4y = 8
2x + 2y = 4
The matrix can be solved for x and y such that it has the form
| 1 0 a | <==> 1x + 0y = a    ==> x == a, where a & b are the answers
| 0 1 b | <==> 0x + 1y = b    ==> y == b
So, solving this matrix would give us the intersection point of x==0 and y==2.
The form | 1 0 0 ... a | ... is true for all matrices and is said to
         | 0 1 0 ... b |     be in reduced row-echelon form.
         | 0 0 1 ... c |
         | . . . ... . |
         | 0 0 0   1 n |

The form | 1 0 0 ... 0 | ... is called an *identity* matrix,
         | 0 1 0 ... 0 |     noted as I.
         | 0 0 1 ... 0 |
         | . . . ... . |
         | 0 0 0     1 |

Theory and Techniques

Linear algebra is called so because it deals with linear equations (ax + by == c). An equation can't have an ax^2 + b = c or axy + bz =- c, for example. It must be in the form ax + by + ...== c

For example, the system of equations

  x + 4y = 8
 2x + 2y = 4
can be written in matrix form as:
| 1   4    8 |
| 2   2    4 |

A matrix's size is described by row x columns.
The above is a 2 x 3 matrix (2 rows of 3 elements each).
We refer to each element by calling it Mij, where M is the matrix, i is the row number and j is the column number.

For example, in the sample matrix we'll call M, 8 is located at M13. It is commonly written this way, with the numbers interpreted as 1,3.

A matrix can be solved using 3 row operations:

  • A whole row can be multiplied by a constant
  • A multiple of one row can be added or subtracted from another row
  • The order of rows can be switched. For example,
    | 2 2 4 |  <= can be exchanged with =>   | 1 4 8 |
    | 1 4 8 |  <= row below (or wherever) => | 2 2 4 |
    

So using the rules above, with interpretation of the results at the end:
| 1  4   8 |
| 2  2   4 | Row2 += -2*Row1

| 1  4   8 |
| 0 -6 -12 | Row2 = Row2 / -6

| 1  4   8 | Row1 += -4*Row2
| 0  1   2 |

  x  y  ans
| 1  0   0 | ==> x==0
| 0  1   2 | ==> y==2
This same method can be done for any size matrix.

Combining Matrices

Multiple matrices can be combined into one matrix. This is incredibly useful for simplifying and speeding-up calculations.

Ways of combining matrices:

  • Adding and subtracting them from each other
  • Multiplying one by another
Note that you CANNOT divide one matrix by another (however, you CAN multiply by the inverse of a matrix to get the same result).

In order to add two matrices, the size of the matrices must be the same.

For example, let matrix P be 2x3, R be 2x3 and S be 3x4

P+R is possible because they have the same dimensions (2x3)
P+S is not possible because of different dimensions.

We can multiply two matrices provided that their dimensions are as such

Matrix 1 = m x n        n x p = Matrix 2
When combined, a new matrix of dimension m x p will be created.

For example, using the previous matrix examples, P*S is possible
(2x3 3x4), whereas P*R is not (2x3 2x3).

How to multiply matrices

Using the example given above, P*S, we multiply the matrices as shown below:
        S:|    A        B        C        D     |
          |    E        F        G        H     |
          |    I        J        K        L     |

P:|a b c || aA+bE+cI aB+bF+cJ aC+bG+cK aD+bH+cL |
  |d e f || dA+eE+fI dB+eF+fJ dC+eG+fK dD+eH+fL |

For a given row in the first matrix A and a given column in the second matrix B, multiply each component in A with B and add the results. It can also be written as a dot product of a row and column, for every row and column. This seems hard to read, I'm sure, but these are just the formulas used and they typically wouldn't be written out in the expanded form.

It is very important to keep in mind is that unlike algebra, matrix multiplication is not commutative. This means the order in which you multiply the matrices matters. In this case, A*B != B*A. This can be interpreted that the resulting vector A*B points in the opposite direction from B*A. It can be shown, however, that A*B == -B*A, which is useful for simplifying and equivalencies.

Rotation

We can take the rotation equations and place them in a matrix.
For example, the common 2D rotation equations,
x=cos(theta), y=sin(theta)
can be written as:
R =| cos(t) 0      |
   | 0      sin(t) |
For 3D rotation about the z-axis, it can be written as:
| cos  sin  0  |
| sin  cos  0  |
| 0    0    1  | Note: the z coord is not affected (1 * z == z)
For 3D rotation about the y-axis:
| cos  0   sin |
| 0    1   0   |
|-sin  0   cos |
For 3D rotation about the x-axis:
| 1    0    0  |
| 0  cos  sin  |
| 0 -sin  cos  |
When doing 3d programming, a 4x4 matrix is typically used. One rotation matrix, around the y axis, looks something like:
| cos  sin  0  0 |
| sin  cos  0  0 |
| 0    0    1  0 |
| 0    0    0  1 | Note: this line is our interest
The last row always puzzled me -- why is that fourth row there?
It turns out that the fourth row allows the object to be translated on the screen -- moved x units left or right, y units up or down, and z units in or out, or whatever way you've defined the coords to be.

Translation

The basis of a coordinate system is typically a 4x4 matrix. The first three rows and columns are used for the basis, and the 4th row and column is used for the translation amount. If a given vector is multiplied by the translation matrix below, the vector will be translated by <tx,ty,tz>
| 1 0 0 tx |
| 0 1 0 ty |
| 0 0 1 tz |
| 0 0 0 1  |
where tx,ty,tz are the amounts to translate the vector.

We can combine different rotations and translation into one matrix that will do all the same transforms as the individual ones

Given matrices TY, RotX, RotY, RotZ, we can combine them into a matrix TranYRotXYZ that will contain all the information to do all the translations and rotations:

TranYRotXYZ = TY * RotX * RotY * RotZ

Inverses

The inverse of a matrix is noted as A^-1. For example, in regular algebra, the inverse of x is 1/x, or x^-1. This is similar, but not the same, as the inverse of a matrix.

An inverse of a matrix could be defined as such that
A*A^-1 == I, where I is the identity matrix, provided A^-1 exists

(A^-1 might not necessarily exist, but determining this is a more advanced topic)

I've noticed that many people point out this fact and praise it, but then don't give a reason for why this is so useful. The reason is that it allows you to solve a matrix very quickly.

In algebra:
           Ax == b, solve for x
(1/a) * a * x == b * (1/a)
           1x == b/a
    answer: x == b/a

In linear algebra:
           Ax == B, solve for x, where A,x,B are matrices
   (A^-1)*A*x == (A^-1)*B
        by the fact A*A^-1 == I
          I*x == (A^-1)*B
    answer: x == (A^-1)*B

In matrix form, using the first example:

  x y ans
| 1 4 8 |
| 2 2 4 |
and converting it to the following form (because the X and Y are implied in the above matrix):
| 1 4 | * |x| == |8|
| 2 2 | * |y| == |4|
we can calculate the inverse and solve for x and y quickly instead of reducing the matrix to an identity one. (I realize that this form looks very different from the first example, but I ask that you trust that they are equivalent).

Final Comments

This wraps up the crash course in linear algebra; I hope it made sense. I tried to include some of the important topics but it was beyond the scope of this tutorial to cover everything. Linear algebra can initially be a confusing topic, so don't worry if your understanding is still clouded. Another good reference is by Lithium of VLA -- 3drotate.doc. It can either be found on one of the countless game programming sites or in the Game Programming Encyclopedia -- pcgpe10.zip.
-Alex (alexz (at) FusionIndustries.com)
Author's note:
I originally wrote this tutorial on January 30, 1999 for FusionDev, the new name for the development division of Fusion Industries. FusionDev was folded back into Fusion Industries shortly after its creation.
-Alex



[ News || Links || Archives || Downloads || Projects || Gallery || Lore, etc ]