Monday, November 05, 2007

Matrix multiplication order in CG

Matrices are used in transformations in both 2D and 3D graphics. Over the years, I've seen a number of individuals confused about the order in which matrices are multiplied in the construction of composite transformations. If you've done a lot of work with matrices, this post is pretty basic, but I'm simply offering it with the hope it will help clear up a what seems to be a point of confusion among the less initiated.

The greatest source of confusion with matrix multiplication order seems to be due to different but equivalent methods used in computer graphics texts. Some texts multiply rows by matrices. Other texts multiply matrices by columns. In the row case, the row vector comes before the transformation matrix. In the column case, the transformation matrix comes before the column vector.

Why do authors use two different approaches? In the case of row vectors, matrices are concatenated in the same order that the operations are applied, which is an attractive property vs the column method in which the matrices are multiplied in reverse order. On the other hand, the use of column vectors is also attractive because that's the form typically used in linear algebra texts.

In understanding the relationship between the two, it's helpful to rememember that the transpose of a matrix product is equal to the reverse product of the individual matrix transposes. Applying this rule with a transformation matrix M and a column vector C shows us the relationship between the column method and the row method:


Note: The transpose of a column vector (CT) is a row vector. The transformation matrix used with row method is the transpose of the transformation matrix used with the column method.

If you remember that row*matrix always yields a new row and matrix*column always yields a new column, the rest falls into place. Apply each transformation sequentially and the order of multiplication needed for a composite transformation will be clear.

In the case of rows, apply the first transformation:


Because this product is a row, we append the next matrix and multiply again to apply the second transformation:


Since matrix multiplication is associative, we can move the parentheses to show the composite transform constructed by multiplying the transformations in order:


This shows why transformations are applied in order with the row method.

In the case of the column method, the transformation matrix comes first, followed by the column vector. Apply the first transformation:


Since this results in a column, we prepend the second transformation matrix (because with the column method, the transformation matrix always comes before the column):


Again, since matrix multiplication is associative, we can reorder the parentheses:


This clearly shows why transformation matrices are multiplied in reverse order when the column method is used.

One final reminder. If you switch from one method to the other, remember that the matrix used in one method is the transpose of the matrix used in the other method; i.e., (MC)T = CTMT

See also: The Matrix Cookbook and Gamasutra: Complex Matrix Conventions.


Post a Comment

Subscribe to Post Comments [Atom]

<< Home