2.3 Matrices

Matrices in Sh are supported for the representation of linear, affine, and projective transformations. As with tuples, dimensions of 1, 2, 3, and 4 in both dimensions are the most common, and are supported with explicit names, although a template mechanism is provided for matrices of larger dimensionality if necessary.

/ E   Matrices are not just arrays of tuples, they may include alternative internal representations of, for instance, diagonal matrices. Sh may also try to avoid computing and storing the transpose of a matrix; instead, it might swizzle the accesses to a matrix appropriately. Run-time access to elements is limited to constant indices supported using the “[]” and/or “()” operators, to be described later.

Matrices are defined with declarations of the form ShMatrixRxCs where R gives the number of rows, C the number of columns, and s the storage format. For instance ShMatrix4x4f is a legal (and common) matrix declaration.

General matrices can also be declared using template syntax by writing ShMatrix<R,C,b,s>, where b is the binding type (as in tuples) and has a default value of SH_TEMP. Matrices can be used and declared as parameters, input, output or input-output attributes, or local temporaries.

/ E  Special rules are applied with using matrices in expressions with tuples using the “|” matrix multiplication operator, which promotes sizes as necessary to make it possible to combine, for instance, a 3 4 affine matrix with a 4 4 projective matrix. Basically, the smaller matrix is enlarged by the number of rows (or columns) necessary from an identity matrix to make matrix multiplication possible.

/ E  Assigning a scalar to a matrix initializes that matrix with the identity matrix scaled by that value. Therefore, to initialize a matrix with the identity matrix, assign the value of 1 to it. Diagonal matrices can also be constructed using the diag constructor function, / E  and matrices can be built from tuples interpreted as rows (using the rowmat constructor) or as columns (using the colmat constructor).

Many library functions exist to manipulate matrices, including det (to compute the determinant), trace (to compute the trace), inverse, transpose, and adjoint (to compute the transpose of the cofactor matrix, which when divided by the determinant gives the inverse, but does not itself require divisions to compute). These operations are described in detail in Section 3.8.


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.