# Matrices

A matrix is a rectangular array of numbers called elements. We can use matrices to solve linear algebra equations but more commonly they are used to store and manipulate changes made to objects in virtual space as in animated films and computer games graphics.

Matrix Addition and Subtraction can only be done when the number of rows in each matrix is the same and when the number of columns is the same. You simply add or subtract each like position value to get a third same shaped matrix .

Matrix Scalar Multiplication involves multiplying each matrix element by the scalar.

Matrix by Matrix Multiplication can only be done if the number of columns in Matrix A is the same as the number of rows in Matrix B. The resulting matrix has rows equal to the columns in A and columns equal to the columns in B.  It is important to realise that A x B is not the same as B x A, and may not be possible because of the rule. More on matrix multiplication later.

Transposition of a Matrix AT simply involves writing the column data in row format.

Types of Matrix
A vector matrix is usually a single row or column.
A null matrix is one having all elements equal to zero.
A diagonal matrix has numbers top left to bottom right and zero’s elsewhere.
A unit matrix is a diagonal matrix with 1 top left to bottom right.
A symmetric matrix is a square matrix with symmetry about the diagonal.
A skew symmetric matrixis also square. Its diagonal elements are zero and the symmetry about it involves opposite signs.
An orthogonal matrix  is square: multiplied by its transpose delivers a unit matrix.

The Inverse of a Square Matrix is written as A-1 When Matrix A is multiplied by its inverse A-1 a unit matrix is the result. We find the inverse of a matrix by dividing its adjoint    adj(A) by its determinant  |A|   (See below)

Matrix Determinant |A| is a single number. For a 2×2 matrix |A| is simply the difference in cross product that is the product of the lead diagonal numbers minus the product of the non leading diagonal numbers. For the 3 x 3 matrix shown the determinant is done either horizontally or vertically. Both give the same result. a(ei-fh) –b(di-fg) + c(dh-eg)    or   a(ei –fh)-d(bi-ch) +c(dh-eg)

Note the second term in each of these calculations is negative. In reality the cross product difference associated with any element is that of the 2 x 2 elements below it and right of it and applied in cyclic fashion so that the top is below the bottom and the left side follows the right (see diagram). The second terms are then + b(fg-di)  and +d(hc –ib) which equate to the second terms shown above. The reason we make terms negative is so that we do the cross products in an as seen manner. Each element of a matrix has a cross product difference termed a Minor of that element. The minor for h is cd – af, that for i is ae – bd.

The Cofactor of a matrix element is produced by replacing each element in the matrix with the Minor product difference associated with it. The cofactor of the example 3×3 matrix on the right is shown below it on the right.

Applying the cross product differences cyclically as described above avoids minus terms otherwise if calculating minors as seen then minus terms apply at positions containing -3, -2, 1 and 5

Adjoint Matrix : Every square matrix A has an adjoint matrix adj(A). It is obtained by changing each element in the matrix to its cofactor as described above and then transposing that matrix.

Example Solving Linear Equations : Given the following three equations, solve for x, y and z   x + y + z = 6 ,  2x +3y +4z = 20 4x + 2y +3z = 17

The above equations iare shown in the three matrices left. Consider them as being A, U and R where U is the unknown matrix containing x, y and z and R the results matrix.  To solve this matrix equation  A x U = R we multiply both sides of it by the inverse of A  (A-1)  so that U = R x A-1.

The inverse of A (A-1) is its Adjunct adj(A) divided by its Determinant |A| whilst  adj(A) is the Transpose of the Cofactor of A. Left we have the cofactor and right we see adj(A) being multiplied by R to give us a 3, 6, 9 matrix.

However we are not done yet because we haven’t divided by the Determinant. We calculate the determinant as under
|A| = 1(3 x 3 – 4 x 2) + 1(4 x 4 – 2×3) + 1(2 x 2 –3×4) = 1 +10 -8 = 3.

We now divide the 3, 6, 9 matrix by 3 to get the matrix R = (1,2,3) and thereby solve our three equations with the three unknowns.  Our solution is   x = 1,  y = 2 and z = 3

Transformations in 3D. We may view any 3 dimensional object in virtual space as constructed from many joined up points. Each object or part object can have its own coordinate system and each object point is referenced by that coordinate system as linked perhaps to some parent coordinate system. Everything in view and the camera view directions and locations are part of a global coordinate system. The impression of motion is created by repetitively calculating the revised locations of points changed by transform changes and repetitively rebuilding the scene.

Transformations are changes made to an object. We may translate (relocate) an object, change its orientation relative to other objects, reflect it so as to make it appear as if viewed in a mirror, change its size or distort it. All can be done by matrices.

For example a simple two dimensional object like a triangle would be constructed from 3 points. We could then apply a colour to that shape giving it appearance.

In our diagram each triangle point in the triangle on the right is multiplied by the same matrix. The three new points that result are used to construct the triangle on the left.

In a graphics application the triangle would now appear as if rotated through 90 degrees counter clockwise about the coordinate origin. If we had wanted that rotation to appear as a motion we would have multiplied the points over time by many intermediate matrices and rebuilt the triangle many times on its way to that final location

How did we do this multiplication and how did we do the multiplication of the earlier 3 x 3 matrix with a 3 (row) x 1(column) matrix. The number we put in row n column m of the output matrix is obtained by multiplying each number in row n by its like position numbers in column m and summing the results . Our 6 in the 2nd row of the earlier output 3, 6, 9 column matrix was the result of multiplying 10 x 6 , -1 x 20 and -2 x 17 and adding them

In 3D scenes the matrices used are generally of the 4 row x 4 column type. As such they are capable of holding all transformation data. The elements m, n and o in the 4 x 4 matrix shown right are the x, y and z translations that any point, to which this matrix is applied, will undergo. Most matrixces are applied to objects that have no scale, shear or squashing and in such cases elements d, h, l and p will have values 0, 0, 0 and 1.

The rotation elements are there in the first 3 columns of the first 3 rows. Elements a,b, c describe how points in the x, y plane will change; e,f, g describe how points in the y,z plane change and i, j, k how points in the z,x plane change. It means, and ignoring any translations, the point 1, 0, 0 will move to a,b, c ; the point 0, 1, 0 will move to e, f, g. Apply the rotation and then add in the m, n, o value x, y, z translations to each point.

It is important to realise that

In computer graphics involving 3d scenes the programmer’s program determines the transform matrix that is to be used and the computer does all the hard work of determining all the new object points and thereby creating the changed or changing image. Action matrices are generally of the four by four type as illustrated.