
Matrices are often used to perform transformations on coordinates. They
can do so in any number of dimensions. Matrices representing transformations
can be multiplied together combining all of the translation matrices into
one. This means that matrices can save time when performing translations,
- multiple rotations, translations, enlargements, etc can be combined into
one matrix and can be executed in a single matrix operation.
Matrices
A matrix is a 2D array of numbers which can have any
width and height. Below is an example of a 4 by 2 matrix. Note that usually
matrices are stated as height by width, not the other way around :
Matrix
Math
Below are detailed (algebraicly, descriptions are given where necessary)
matrix math functions (addition, multiplication)
Addition
Simply add the values of the positions to each other. This means that the
matrices to be added must be the same size, - you cannot add a 4x2 to a
3x5, only to another 4x2.
Subtraction
Same as addition, but subtracting instead. Once again, matrices must be
of the same size.
Multiplication
Three examples are given below, because multiplication is a more complex
process.
To find the value of a point at row i, column j, you multiply
the contents of row i in the first matrix by the contents of column
j of the second matrix, as shown in the above examples. If you study
the above examples, you can see that the length of the rows in the first
matrix must be the same as the length of the colums in the second matrix,
- for multiplication to be possible, the width of matrix 1 bust equal the
height of matrix 2, - a 4x2 cannot be multiplied by a 5x7 or another 4x2,
it can however be multiplied by a 2x4 or a 2x3. As discussed earlier, matrices
can be used to represent transformations, and transformation matrices can
be combined by multiplying the transformation matrices together (Note that
matrix multiplication is not commutative (a*b != b*a) so the order in which
they are multiplied DOES matter). Because of the above (matrix 1 width
= matrix 2 height) transformation matrices are therefore square, - 3x3
or 4x4 are commonly used for 3D transformations. More on this later.
Scaling
Each position in the matrix is multiplied by a single value, resulting
in a matrix of the same same size :-
Transpose
To transpose a matrix, swap the rows with the columns :
Determinant
The determinant of a matrix (|m| where 'm' is a matrix) is a value which
can be used in finding the matrix's inverse. Calculating the determinant
is a fairly complex process in comparison to the other operations covered.
So far as I know this operation can only be performed on square matrices,
- the width must equal the height. The determinant of a 2x2 matrix is as
below.
To calculate
the determinant of a larger matrix (ie 3x3 or 4x4) you "exclude" the top
row of the matrix, and each column in turn. You then take what is left
and turn it into a submatrix and calculate the determinant of this submatrix.
You multiply the determinant of the submatrix by the value at the column
that was excluded, row 1 (top row), and multiply that by
Where
j is the number of the column that was excluded. You then sum all
of these values. Below is a diagram of how to obtain a submatrix from a
4x4 matrix when column 2 and row 1 are excluded (they are highlighted):
The submatrix is on the right. You find the determinant of this submatrix
and multiply it by b in this case, and then by
We will call this the 2nd submatrix, since row 2 was excluded. Below is
an example of finding the determinant of a 3x3 matrix and a 4x4 matrix
(algebraicly) :
Notes
My code is written so it recursively splits into submatrices, until it
reaches a 2x2, when is uses the formula above, although you could split
further to 1x1 matrices whose determinants are their only value. The
can be replaced by the C code
if (j % 2) //for non-C-programmers
this means if the remainder of j / 2 is non-zero
determinantMultipliedByNumber = -determinantMultipliedByNumber;
which would
save time.
Inverse
The inverse of a matrix is a matrix of the same size, which undoes any
transformation performed by the original matrix. The determinant of the
matrix is involved in finding the inverse. Also, the matrix has NO inverse
if it's determinant is 0. You will see why. To find the value of a particular
position (column i, row j) you generate a submatrix by excluding
the appropriate rows. In the example below, i = 2, j = 3
:
You calculate
the determinant of the submatrix, divide that by the determinant of the
matrix (not the submatrix), and multiply by
You repeat the above for each position in the inverse matrix. You then
transpose the (inverse) matrix. Thats it.
Transformations
Here is some info on how to perform transformations using matrices. In
the examples, it is assumed that we are working in three dimensions, and
want our matrices to have to ability to perform translations, so 4x4 matrices
will be used. (If translations were not required a 3x3 could be used. For
translations, the matrix width and height are equal to dimension+1, ie
for 2 dimensions, you use 3x3)
Vectors
A 3D vector can be converted into a 1x4 matrix as shown below :
Where x,
y, and z are the componentos of the vector. Matrices are
also converted back to vectors by the reverse process. (the '1' is ignored)
Combining
transformations
Transformations are combined by multiplying the transformation matrices
together. Note that when combining transformations, the order in which
they are performed (as if done separately) is b then a, then
if r is
the combined transformation.
Identity
The identity transformation has no effect, - coordinates remain the same.
Translation
The matrix below translates through the vector (x,y,z).
Scaling
(enlargement)
Scales by a factor of (x,y,z) on the x, y, and z axes respectively.
Rotation
Rotates through theta degrees about the x-axis.
about the y-axis :
about the z-axis :
Well, hopefully thats enough to go on. The above could be coded in a little
library or something.
If anyone has any questions, ideas for optimiztions, etc, don't hesitate to mail me at :
mrmeanie@easynet.co.uk