WhatIsAMatrix?
From Odwiki
Fortunately some people know what a matrix is and are willing to tell you. No pills necessary. Very useful for WritingShaders.
General Matrix Information
Welcome to the wonderful world of the Matrix. Matrices are important in 3D graphics because they can be used to perform all sorts of useful transformations. If you have no idea what a Matrix is you may be surprised to know that you use them for just about everything in 3D on a daily basis. Some uses of matrices:
- Rotation
- Scale
- Reflection
- Shear
- Projection
Enter the Matrix
A matrix is a rectangular two-dimensional array of numbers sorted into rows and columns. Rows are Horizontal and Columns are Vertical. We define a matrix as being rows by columns so a 4x3 matrix looks like this:
A Matrix | 4 9 1 | | 8 7 5 | | 0 8 3 | | 3 6 2 |
The elements of a matrix are usually addressed using a variable with subscript numbers representing the row and column in a matrix. M12 (read "M one, two") refers to the element in the first row, second column. In the example Matrix above M12 is 9. By the same token, M43 is 2. Here's a complete example:
Indexing Matrix Elements | M11 M12 M13 | | M21 M22 M23 | | M31 M32 M33 | | M41 M42 M43 |
Square Matrices
Matrices with equal numbers of rows and columns are referred to as Square Matrices. Here is an example of a 3x3 square matrix:
A 3x3 Square Matrix | 1 2 3 | | 4 5 6 | | 7 8 9 |
Identity Matrices
In a square matrix, diagonal elements are those elements of the matrix where the row and column index numbers are the same (M11 M22 M33). If the diagonal elements are all 1 and the rest of the matrix is filled with 0's, we have what is known as the identity Matrix:
The 3x3 Identity Matrix | 1 0 0 | | 0 1 0 | | 0 0 1 | The 4x4 Identity Matrix | 1 0 0 0 | | 0 1 0 0 | | 0 0 1 0 | | 0 0 0 1 |
Above are examples of 3x3 and 4x4 Identity Matrices. The Identity Matrix is the multiplicative identity element for matrices, and is usually referred to as I. Identity Matrices are important because multiplying a matrix by the identity matrix results in the original matrix.
Vector Matrices
Vectors are also matrices. Vectors come in two flavors; row vectors and column vectors.
A row vector is written horizontally and is defined as a 1 x n (read "one by en") row vector, where n is the number of elements.
A 1x3 row vector | 1 2 3 | A 1x4 row vector | 1 2 3 4 |
A column vector is written vertically and is defined as a n x 1 (read "en by one") column vector, where n is the number of elements.
A 3x1 column vector | 1 | | 2 | | 3 | A 4x1 column vector | 1 | | 2 | | 3 | | 4 |
The notation of row vs. column is interchangeable, however when dealing with vectors and larger matrices it is important to understand the difference between the two.
Transposing Matrices
Matrix *M* is defined as r x c. This Matrix can be transposed, MT to yield c x r.
Original Matrix(M) Matrix M Transposed(MT) | 1 2 3 | | 1 4 7 | | 4 5 6 | | 2 5 8 | | 7 8 9 | | 3 6 9 |
Transposing matrices is the same principle as interchanging row and column vectors, we just have more vectors to deal with. It's important to note that when transposing a square matrix, the diagonal elements do not change.
Multiplying Matrices by Scalars
A scalar is simply a single individual number. You can multiply any matrix by a scalar. All you have to do is multiply each element of the matrix by the scalar.
Scalar(s) Matrix(M) Scalar * Matrix(sM)
| M11 M12 M13 | | sM11 sM12 sM13 |
s * | M21 M22 M23 | = | sM21 sM22 sM23 |
| M31 M32 M33 | | sM31 sM32 sM33 |
| 1 2 3 | | 2 4 6 |
2 * | 4 5 6 | = | 8 10 12 |
| 7 8 9 | | 14 16 18 |
Multiplying a Matrix by a Matrix
You can derive the product of a pair of matrices, however there are rules that must be followed. First, the number of columns in the first Matrix must be equal to the number of rows in the second matrix. If Matrix A is r x *n*, Matrix B must be *n* x c in order to derive a product, otherwise multiplication of the Matrices (AB) is undefined.
Multiplication of matrices works like this. Suppose you have matrix A (r x n) multiplied by matrix B (n x c) Your product matrix is C (r x c). Each element of C is equal to the vector dot product of row Ar of matrix A and column Bc of matrix B. It's not as complicated as it sounds; more like redundant and time consuming. Thankfully we have Houdini to do all this work for us.
Matrix A Matrix B Matrix C | A11 A12 | | B11 B12 B13 B14 | | C11 C12 C13 C14 | | A21 A22 | | B21 B22 B23 B24 | = | C21 C22 C23 C24 | | A31 A32 | | C31 C32 C33 C34 | C11 = A11*B11 + A12*B21 C12 = A11*B12 + A12*B22 C13 = A11*B13 + A12*B23 ... C33 = A31*B13 + A32*B23 C34 = A31*B14 + A32*B24
Notes concerning multiplying Matrices:
- Multiplying any Matrix (M) by the Identity Matrix (*I*) results in the original Matrix: MI == IM == M
- Matrix multiplication is not communicative: AB != BA
- Matrix multiplication is associative: (AB)C == A(BC)
Interesting Note about multiplying multiple Matrices: ABCDE == ((AB)C)D == A((BC)D) == (AB)(CD) However, depending on the Matrices it is possible that certain groupings will require fewer scalar multiplications to arrive at the product. Finding the parenthesization that minimizes the number of multiplications required to derive a product, is called the matrix chain problem.
- Multiplying any Matrix (M) by a Square Matrix (S) on either side, will produce a Matrix (*C*) with the same dimensions as Matrix M
- Multiplying matrices by scalars (s) and vectors (v) is also associative.
- Scalar Associative Example: (sA)B == s(AB) == A(sB)
- Vector Associative Example: (vA)B == v(AB) == A(vB)
- Transposing the product of two matrices yields the same result as taking the product of their transposes in reverse order. (AB)T == (BT)(AT)
Multiplying Matrices and Vectors
A vector is a matrix, so a vector and a matrix can be multiplied provided they meet the requirements stated above in Multiplying a Matrix by a Matrix. However there is more. Remember that matrix multiplication is not communicative, and whether the vector is a row vector or a column vector will determine whether it has to be premultiplied with the matrix or postmultiplied with the matrix.
A 1x3 Row Vector A 3x1 Column Vector A 3x3 Matrix
| 1 2 3 | | 1 | | 1 2 3 |
| 2 | | 4 5 6 |
| 3 | | 7 8 9 |
Remember in order to multiply a pair of matrices, the number of columns in matrix *A* must be equal to the number of rows in matrix *B*. So, in the example matrices above, the row vector would have to be premultiplied with the 3x3 matrix, and the column vector would have to be post multiplied. Trying to premultiply a column vector or post multiply a row vector with a 3x3 matrix is undefined. How you multiply a vector by a matrix will depend on whether you are pre or postmultiplying the vector.
Premultiplying a Row Vector and a 3x3 Matrix
| M11 M12 M13 |
| X Y Z | | M21 M22 M23 | = |(XM11 + YM21 + ZM31) (XM12 + YM22 + ZM32) (XM13 + YM23 + ZM33)|
| M31 M32 M33 |
| 1 6 7 |
| 2 5 4 | | 4 3 4 | = |( 2 + 20 + 8) ( 12 + 15 + 32 ) ( 14 + 20 + 28)| = | 30 59 62 |
| 2 8 7 |
Postmultiplying a Column Vector and a 3x3 Matrix | M11 M12 M13 | | X | |(XM11 + YM12 + ZM13)| | M21 M22 M23 | | Y | = |(XM21 + YM22 + ZM23)| | M31 M32 M33 | | Z | |(XM31 + YM32 + ZM33)| | 1 6 7 | | 2 | |( 2 + 30 + 28)| | 60 | | 4 3 4 | | 5 | = |( 8 + 15 + 16)| = | 39 | | 2 8 7 | | 4 | |( 4 + 40 + 28)| | 72 |
Note that premultiplying a row vector by a matrix results in a row vector, and post multiplying a column vector by a matrix results in a column vector.
Also note the following:
- Each element in the resulting vector is the dot product of the original vector and a row or column of the matrix.
- Matrix multiplication is distributive over vector addition. For vectors a and b and matrix M: (a+
M = aM + bM
Row Vectors vs. Column Vectors
As you can see from the example above premultiplying a row vector and postmultiplying a column vector by a given matrix yields different results despite the fact that the matrix and the vectors have the same elements. If the Matrix had been transposed then post multiplied by the column vector we would have gotten the same result. This is why it's important to chose either row vectors or column vectors in your work and stick with them. When using Houdini or any other 3D Software for that matter vectors are almost always represented visually to end users both in the interface and in scripting as row vectors, and the software takes care of the rest. Because of this, thankfully it likely won't be an issue in your work. It is however good to know this information should you decide to take up programming or find the need to work out vector/matrix multiplication for yourself.
Geometric Interpretation of Matrices
Now we get to the crux of why matrices are so important with regards to 3D computer graphics. Square matrices can be used to represent any linear transformation. A linear transformation preserves straight and parallel lines, but there is no translation; that is the origin does not move. We can use a square matrix to scale, stretch, shear, rotate, etc. any object in three dimensions.
Using a Matrix to Transform a Vector
A vector, for our purposes, is a series of displacements in X, Y, and Z. That is, the given vector | 0.5, -1, 3.2 | can be interpreted as a series of displacements; one half unit in X, negative one unit in Y, and three an 2 tenths units in Z.
| 0.5 | | 0 | | 0 |
| 0.5 -1 3.2 | = | 0 | + |-1 | + | 0 |
| 0 | | 0 | | 3.2 |
As you can see a vector can be expressed as a series of vectors with signed displacements parallel to the relative axis in X, Y, or Z. These individual vectors are called unit vectors. It can be said that a vector (in this case a 3x1 vector) is the sum of three unit vectors. Each unit vector in the sum contains a displacement in one axis. So vector *v* = x*p* + y*q* + z*r*.
v p q r
| 0.5 | | 0 | | 0 |
| 0.5 -1 3.2 | = | 0 | + |-1 | + | 0 |
| 0 | | 0 | | 3.2 |
This vector is starting to look a whole lot like a 3x3 matrix. In fact it is. The unit vectors that comprise vector *v* can be assembled into matrix *M*.
| p | | px py pz |
M = | q | = | qx qy qz |
| r | | rx ry rz |
Look what happens when you multiply this matrix by a vector
| px py pz |
| x y z | | qx qy qz | = | x px + y qx + z rx x py + y qy + z ry x pz + y qz + z rz |
| rx ry rz |
In simple terms; vM = x p + y q + z r. That's the same as the equation that describes a vector as unit vectors. So, a matrix can be interpreted as a series of unit vectors, which can be used to transform the coordinate space of a vector through multiplication. aM = b. Vector a multiplied by matrix M produces vector b. The product vector *b* has been transformed by matrix M.
Visualizing Matrices
With an understanding of how vectors and matrices work and how to use them mathematically the next important step as it pertains to 3D, is to be able to visualize what a matrix looks like. Since a matrix represents a transformation, you have to be able to visualize that transformation so you can construct a matrix that will perform the desired transformation.
Matrices in Houdini
...more to come



