3.8 Linear Algebra

Matrices use both the “*” and “|” operator for matrix-matrix multiplication, matrix-vector products, and vector-matrix products. Matrix-vector and vector-matrix products actually apply to all matrix-tuple and tuple-matrix products, but special rules are used for ShTexCoord, ShVector, ShNormal, ShPlane, and ShPoint types to infer missing homogeneous coordinates and the use of matrices to represent affine and projective transformations. In matrix-vector and vector-matrix products, the output tuple type is the same as the input type.

On matrix-tuple products with the tuple on the right, the tuple (i.e. any subclass of ShAttrib) is treated as a column vector and the usual rules of matrix-vector multiplication are applied. The dimensions of the matrix and tuple must match; in particular, for this situation, the number of columns in the matrix must match the number of elements in the tuple, and the matrix-tuple product describes a linear combination of the columns of the matrix. The result is a tuple of the same type as the rightmost argument that is a result of a linear combination of the columns of the matrix.

Exceptions to the size-matching rule are made for the ShTexCoord , ShVector, ShNormal, ShPlane and ShPoint types to account for missing homogeneous coordinates. If an ShVector or ShNormal is the argument on the right and the matrix has one more column than the number of elements in the vector, the weight on the last column of the matrix is assumed to be zero, i.e. the ShVector or ShNormal is automatically promoted by one dimension and the extra homogeneous element given the value of zero. For an ShTexCoord , ShPoint or ShPlane, something similar happens, but the inferred homogeneous element is given a value of one.

When tuples are given on the left side and matrices on the left, similar rules apply, but the tuple is interpreted as a row vector and the result is a linear combination of the rows of the matrix.

The “|” operator can also be used for matrix multiplication, as described in the next section.

In order to support the convenient composition of affine and projective transformation matrices, if rows are missing on the bottom of a matrix or columns on the left they are automatically drawn from an identity matrix of the correct size. This is also true for matrix functions such as “inverse”, shown in Table 3.15. Matrix-vector functions and operators are listed in Table 3.16.

Normally we would multiply vectors and points on the right of matrices and normals and planes on the left. However, we permit the placement of these types on the “wrong” side of expressions to avoid the use of explicit transposes in expressions like x * Q * x, which computes the quadratic form xT Qx if x is a point and Q is a coefficient matrix. This works correctly even if x is a ShPoint3f and Q is a Matrix4x4f due to the homogeneous promotion rules outlined above, which work on both the right and the left.





Operation  r <-- a,b,c,... Description






+  N M <-- N M,N Mcomponentwise addition



-  N M <-- N M,N Mcomponentwise subtraction



-  N M <-- N M componentwise unary negation



*  N P <-- N M,M P matrix product



|  N P <-- N M,M P matrix product



/ E  inverse N N <-- N N inverse of a square matrix



transpose  N M <-- M N transpose of a matrix



/ E  adjoint N N <-- N N adjoint of a square matrix (the transpose of the cofactor matrix).



/ E  det  1 <-- N N determinant of a square matrix



/ E  trace  1 <-- N N trace of a square matrix



 


Table 3.15: Matrix functions. Compute various quantities for matrices or operate on matrices.





Operation r <-- a,b,c,... Description






*  N M <-- 1,N Mscalar-matrix product



*  N M <-- N M,1matrix-scalar product



/ E  *  N <-- N M,M matrix-column vector product



/ E  *  M <-- N,N M row vector-matrix product



|  N <-- N M,M matrix-column vector product



/ E  |  M <-- N,N M row vector-matrix product



 


Table 3.16: Matrix-vector functions. Various functions related to combining matrices and vectors together.





Operation r <-- a,b,c,... Description






/ E  rowmat M N <-- N1,...,NMbuild a matrix, using the given tuples as rows



/ E  colmat N M <-- N1,...,NMbuild a matrix, using the given tuples as columns



/ E  diag  N N <-- N build a matrix, using the given tuple as diagonal



 


Table 3.17: Matrix constructors. Functions to construct matrices from tuples.





Operation  r <-- a,b,c,...Description






/ E  rotate  4 4 <-- 3,1 build an 4 4 affine transformation matrix representing a 3D rotation, given an axis vector in a (which does not need to be normalized) and an angle (in radians) about that axis in b. The generated matrix will rotate column vectors by the given angle about the given axis, using the right-hand rule.



/ E  rotate  3 3 <-- 1 build an 3 3 affine transformation matrix representing a 2D rotation, given need an angle (in radians). The generated matrix will rotate column vectors counter-clockwise by the given angle.



/ E  translate 4 4 <-- 3 build a 4 4 affine transformation matrix representing a 3D translation, given a 3D translation vector in a.



/ E  translate 3 3 <-- 2 build a 3 3 affine transformation matrix representing a 2D translation, given a 2D translation vector in a.



/ E  scale  4 4 <-- 3 build a 4 4 affine transformation matrix representing a 3D non-uniform scale, given a three-tuple of scale factors in a.



/ E  scale  3 3 <-- 2 build a 3 3 affine transformation matrix representing a 2D non-uniform scale, given a two-tuple of scale factors in a.



/ E  scale<N>  N N <-- 1 build a N N affine transformation matrix representing a (N - 1)D uniform scale, given a single scale factor in a.



 


Table 3.18: Matrix transformations. Functions to construct primitive transformation matrices.


Note: This manual is available as a bound book from AK Peters, including better formatting, in-depth examples, and about 200 pages not available on-line.