Matrices

We have used column and row matrices to represent points and vectors;

 

\[ \vec{a}= \left(\begin{array}{c} x \\ y \\ z \end{array} \right) \]

\[ \vec{p}= \left(\begin{array}{ccc} 23&45&3 \end{array} \right) \]

There is another kind of matrix called a square matrix which we will be using to apply operations to vectors and points. A square matrix is a 2x2 or 3x3 or 4x4 matrix, made up of rows and columns;

 

\[ \left(\begin{array}{ccccc} 4 & 6\\ 2 & 3\\ \end{array}\right) , \left(\begin{array}{ccccc} \alpha & \beta & \gamma \\ \delta & \epsilon & \phi \\ \iota & \eta & \sigma \end{array}\right) , \left(\begin{array}{ccccc} a & b & c & d \\ e& f & g & h \\ i& j & k & l \\ m & n & o & p \end{array}\right) \]

The elements of a matrix are usually numbers. The numbers of rows and columns of a matrix are called its dimensions. the name for a matrix is usually a capital letter in bold face, like A or M.

${\bf A}=\left(\begin{array}{ccccc} 4 & 6\\ 2 & 3 \end{array} \right)$

Two matrices are equal if the have the same dimensions and the coresponding elements are the same;

$ \left(\begin{array}{ccccc} 6 & 4\\ 2 & 3 \end{array} \right) \neq\left(\begin{array}{ccccc} 4 & 6\\ 2 & 3 \end{array} \right)$
$ \left(\begin{array}{ccccc} 4 & 6\\ 2 & 3 \end{array} \right) \neq\left(\begin{array}{ccccc} 4 & 6 &0\\ 2 & 3 &0\\ 0&0&0 \end{array} \right)$

Two matrices of the same dimensions can be added by adding the corresponding elements;

$ \left(\begin{array}{ccccc} 4 & 6\\ 2 & 3 \end{array} \right)+\left(\begin{array}{ccccc} 1 & 2 \\ 3 & 4 \end{array} \right)= \left(\begin{array}{ccccc} 4+1&6+2\\ 2+3&3+4 \end{array} \right)= \left(\begin{array}{ccccc} 5&8\\ 5&7 \end{array} \right)$

A matrix can be multiplied by a real number (scalar) by multiplying all the elements by the scalar;

$ 4\left(\begin{array}{ccccc} 4 & 6\\ 2 & 3 \end{array} \right) = \left(\begin{array}{ccccc} 16&24\\ 8&12 \end{array} \right)$

What is the negative of a matrix?

What happens if you add a matrix to its negative?

Two matrices of the same dimensions can be added by adding the corresponding elements;

$ \left(\begin{array}{ccccc} 4 & 6\\ 2 & 3 \end{array} \right)-\left(\begin{array}{ccccc} 1 & 2 \\ 3 & 4 \end{array} \right)= \left(\begin{array}{ccccc} 4-1&6-2\\ 2-3&3-4 \end{array} \right)= \left(\begin{array}{ccccc} 3&4\\ -1&-1 \end{array} \right)$

The Transpose of a matrix, is a new matrix whose rows are the columns of the original.

$ \left(\begin{array}{ccccc} 4 & 6\\ 2 & 3 \end{array} \right)^{T}=\left(\begin{array}{ccccc} 4 & 2 \\ 6 & 3 \end{array} \right)$

What happens if you transpose twice?

Matrix multiplication

A square matrix can multiply a column matrix if the number of columns in the square matrix is the same as the number of rows in the column matrix. For instance, a 3x3 matrix can multiply a 3 element column matrix. But a 4x4 matrix cannot multiply a 3 element column matrix.

The elements in the new matrix are the dot product of each row of the square matrix and the column matrix. The result of the multiplication is a new column matrix.

$ \begin{eqnarray*} &&\left(\begin{array}{ccccc} 4 & 6 & -2\\ 2 & 3 & 1 \\ 3 & -1 & 4 \end{array} \right)\left(\begin{array}{ccccc} 2 \\ -3 \\ 1 \end{array} \right) \\ &=& \left(\begin{array}{ccccc} 4.2+6.-3+-2.1\\ 2.2+3.-3+1.1\\ 3.2+-1.-3+4.1 \end{array} \right) \\ &=& \left(\begin{array}{ccccc} 8+(-18)+(-2)\\ 4+(-9)+1\\ 6+3+4 \end{array} \right)\\ &=& \left(\begin{array}{ccccc} -12\\ -4\\ 13 \end{array} \right)\end{eqnarray*}$

A square matrix cannot multiply a row matrix.

A row matrix can multiply a square matrix iff the number of columns in the row matrix is the same as the number of rows in the square matrix.

Again, the elements are the dot product of the row in the first matrix and the columns of the second;

$ \begin{eqnarray*} &&\left(\begin{array}{ccccc} 2 & -3 & 1 \end{array} \right)\left(\begin{array}{ccccc} 4 & 6 & -2\\ 2 & 3 & 1 \\ 3 & -1 & 4 \end{array} \right) \\ &=& \left(\begin{array}{ccccc} 2.4+-3.2+1.3 & 2.6+-3.3+1.-1 & 2.-2+-3.1+1.1 \end{array} \right)\\ &=& \left(\begin{array}{ccccc} 8+(-6)+4 & 12+(-9)-1 & -4+(-3)+1 \end{array} \right)\\ &=& \left(\begin{array}{ccccc} 6 & 2 & -6 \end{array} \right)\end{eqnarray*} $

Multiplying square matrices

Two square matrices can be multiplied if they are the same size. The result is a square matrix. The multiplication is performed by getting the dot product of the rows of the first matrix with the columns of the second.

$ \begin{eqnarray*} &&\left(\begin{array}{ccccc} 2 & -3 &1 \\ 1 & 2 & 3 \\ 3 &-2 & 0 \end{array} \right)\left(\begin{array}{ccccc} 4 & 6 & -2\\ 2 & 3 & 1 \\ 3 & -1 & 4 \end{array} \right) \\ &=& \left(\begin{array}{ccccc} 2.4+-3.2+1.3 & 2.6+-3.3+1.-1 & 2.-2+-3.1+1.1 \\ 1.4+2.2+3.3 & 1.6+2.3+3.-1 & 1.-2+2.1+3.4 \\ 3.4+-2.2+0.3 & 3.6+-2.3+0.-1& 3.-2+-2.1+0.4 \end{array} \right)\\ &=& \left(\begin{array}{ccccc} 8+(-6)+4 & 12+(-9)-1 & -4+(-3)+1 \\ 4+4+9 & 6+6-3 & -2+2+12 \\ 12-4+0 & 18-6+0 & -6-2+0 \end{array} \right)\\ &=& \left(\begin{array}{ccccc} 6 & 2 & -6 \\ 17 & 9 & 12 \\ 8 & 12 & -8 \end{array} \right)\end{eqnarray*} $

Matrix Identity and matrix inverse

The real number contain a special number called the identity ($i=1$) such that for any real number x;

$ ix=x\textrm{ and } xi=x$

Matrix multiplication also has an identity (I); such that for any square matrix M

$ IM=M \textrm{ and } MI=M$

For a 3x3 matrix $ I=\left(\begin{array}{ccccc} 1&0& 0 \\ 0& 1 & 0 \\ 0 & 0 & 1\end{array} \right)$

Real numbers also have an inverse, such that

$ xx^{-1}=1 \textrm{ and } x^{-1}x=1$

Most square matrices also have an inverse such that;

$ MM^{-1}=I\textrm{ and } M^{-1}M=I$

Finding the inverse of 3x3 and 4x4 matrices is difficult to do by hand. But the existence of matrix inverses makes algebreaic manipulation possible.

Matrix Transformations

In computer graphics models (object that we want to view) defined in terms of a collection of vertices. The best way to manipulate these vertices is to use transformation transformations to move, strech and rotate them.

These changes can be achieved by applying an affine transformation matrix to the vertices of the polygon mesh. Geometrically, an affine transformation in Euclidean space is one that preserves

  1. The collinearity relation between points; i.e., three points which lie on a line continue to be collinear after the transformation
  2. Ratios of distances along a line; i.e., for distinct collinear points p1, p2, p3, the ratio | p2 − p1 | / | p3 − p2 | is preserved

In general, an affine transformation is composed of linear transformations (rotation, scaling or shear) and a translation (or "shift"). Several linear transformations can be combined into a single one, so that the general formula given above is still applicable.

These transformations are implemented by multiplying the appropriate vertex by a matrix representing the transformation.

2D transfomations applet (http://www.cs.princeton.edu/%7Emin/cs426/jar/transform.html)

Scaling

To scale a mesh (stretch along x-axis). Apply the following to each point;

\[ \left(\begin{array}{ccccc} s & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array} \right)\left(\begin{array}{ccccc}x \\ y \\ z \end{array} \right)= \left(\begin{array}{ccccc}s.x\\y\\z \end{array} \right) \]

In this case, x component of each point has been multiplied by s.

This scaling matrix can be generalised as;

\[ \left( \begin{array}{ccc}sx&0&0\\0&sy&0\\0&0&sz\end{array}\right) \]

Where $sx$ , $sy$ and $sz$ are scaling factors along the x-axis, y-axis and z-axis respectively.

Beware when scaling objects which are not positioned at the origin.

Rotation

To rotate a point $\theta$ degrees about the z-axis, multiply the point by the following matrix;

\[ R_{z\theta} = \left(\begin{array}{ccccc} \cos \theta & -\sin \theta & 0 \\ \sin \theta & \cos \theta & 0 \\ 0 & 0 & 1 \end{array} \right) \]

For rotations about the x-axis and y-axis, apply the following;

\[ R_{x\theta} = \left(\begin{array}{ccccc} 1 & 0 & 0 \\ 0 & \cos \theta & -\sin \theta \\ 0 & \sin \theta & \cos \theta \\ \end{array} \right)\qquad R_{y\theta} = \left(\begin{array}{ccccc} \cos \theta & 0 & \sin \theta \\ 0 & 1 & 0\\ -\sin \theta & 0 & \cos \theta \\ \end{array} \right) \]

Translation

In order to translate a point we can add a vector to it;

\[ \left(\begin{array}{ccccc}x&y&z \end{array} \right)+\left(\begin{array}{ccccc}dx&dy&dz \end{array} \right)= \left(\begin{array}{ccccc}x+dx & y+dy & z+dz \end{array} \right) \]

Homogeneous Co-ordinates

Scaling and rotation are performed by matrix multiplication, and translation is performed by vector addition. It would be simplify matters if we could also apply translation by matrix multiplication. Vector addition can be simulated by multiplication by using Homogeneous co-ordinates. To convert a vertex to Homogeneous co-ordinates, simply add a 4th co-ordinate, with a value of one;

\[ \left(\begin{array}{ccccc}x&y&z \end{array} \right)\quad \rightarrow \quad \left(\begin{array}{ccccc}x&y&z&1 \end{array} \right) \]

to convert a matrix to homogeneous co-ordinates add an extra row and an extra column;

\[ \left(\begin{array}{ccccc}a&b&c\\d&e&f\\h&i&j \end{array} \right)\quad \rightarrow \quad \left(\begin{array}{ccccc} a&b&c&0\\d&e&f&0\\h&i&j&0\\0&0&0&1 \end{array} \right) \]

Using the homogeneous matrix, translation can be implemented by matrix multiplication;

\[ \left(\begin{array}{ccccc}1&0&0&dx \\0&1&0&dy \\0&0&1&dz \\0&0&0&1 \end{array}\right) \left(\begin{array}{ccccc}x\\y\\z\\1 \end{array} \right) = \left(\begin{array}{ccccc}x+dx \\ y+dy \\ z+dz \\ 1\end{array} \right) \]

This representation simplifies calculation in computer graphics as all necessary transformations can be performed by matrix multiplications. As a result, a series of affine transformations can be combined simply by multiplying successive matrices together. This is at the heart of real-time graphics systems such as OpenGL and DirectX which can use modern graphics cards to perform operations with homogeneous coordinates.