Créer une présentation
Télécharger la présentation

Télécharger la présentation
## Chapter 6 More on Matrices

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Chapter 6 More on Matrices**Fletcher Dunn • Valve Software Ian Parberry University of North Texas 3D Math Primer for Graphics & Game Development**What You’ll See in This Chapter**This chapter completes our coverage of matrices by discussing a few more interesting and useful matrix operations. It is divided into five sections. Section 6.1 covers the determinant of a matrix. Section 6.2 covers the inverse of a matrix. Section 6.3 discusses orthogonal matrices. Section 6.4 introduces homogeneous vectors and 4×4 matrices, and shows how they can be used to perform affine transformations in 3D. Section 6.5 discusses perspective projection and shows how to do it with a 4×4 matrix. 3D Math Primer for Graphics & Game Dev**Word Cloud**3D Math Primer for Graphics & Game Dev**Section 6.1:Determinant of a Matrix**3D Math Primer for Graphics & Game Dev**Determinant**Determinant is defined for square matrices. Denoted |M| or detM. Determinant of a 2x2 matrix is 3D Math Primer for Graphics & Game Dev**2 x 2 Example**3D Math Primer for Graphics & Game Dev**3 x 3 Determinant**3D Math Primer for Graphics & Game Dev**3 x 3 Example**3D Math Primer for Graphics & Game Dev**Triple Product**If we interpret the rows of a 3x3 matrix as three vectors, then the determinant of the matrix is equivalent to the so-called triple product of the three vectors: 3D Math Primer for Graphics & Game Dev**Minors**Let M be an r x c matrix. Consider the matrix obtained by deleting row i and column j from M. This matrix will obviously be r-1 x c-1. The determinant of this submatrix, denoted M{ij} is known as a minor of M. For example, the minor M{12} is the determinant of the 2 x 2 matrix that is the result of deleting the 1st row and 2nd column from M: 3D Math Primer for Graphics & Game Dev**Cofactors**The cofactor of a square matrix M at a given row and column is the same as the corresponding minor, only every alternating minor is negated. We will use the notation C{12} to denote the cofactor of M in row i, column j. Use (-1)(i+j) term to negate alternating minors. 3D Math Primer for Graphics & Game Dev**Negating Alternating Minors**The (-1)(i+j) term negates alternating minors in this pattern: 3D Math Primer for Graphics & Game Dev**n x n Determinant**The definition we will now consider expresses a determinant in terms of its cofactors. This definition is recursive, since cofactors are themselves signed determinants. First, we arbitrarily select a row or column from the matrix. Now, for each element in the row or column, we multiply this element by the corresponding cofactor. Summing these products yields the determinant of the matrix. 3D Math Primer for Graphics & Game Dev**n x n Determinant**For example, arbitrarily selecting row i, the determinant can be computed by: 3D Math Primer for Graphics & Game Dev**3 x 3 Determinant**3D Math Primer for Graphics & Game Dev**4 x 4 Determinant**3D Math Primer for Graphics & Game Dev**Expanding Cofactors This Equals**3D Math Primer for Graphics & Game Dev**Important Determinant Facts**The identity matrix has determinant 1:|I| = 1. The determinant of a matrix product is equal to the product of the determinants: |AB| = |A||B|. This extends to multiple matrices: |M1M2…Mn-1Mn| = |M1||M2|… |Mn-1||Mn|. The determinant of the transpose of a matrix is equal to the original. |MT| = |M|. 3D Math Primer for Graphics & Game Dev**Zero Row or Column**If any row of column in a matrix contains all zeros, then the determinant of that matrix is zero. 3D Math Primer for Graphics & Game Dev**Exchanging Rows or Columns**Exchanging any pair of rows or columns negates the determinant. 3D Math Primer for Graphics & Game Dev**Adding a Multiple of a Row or Column**Adding any multiple of a row (or column) to another row (or column) does not change the value of the determinant. This explains why shear matrices from Chapter 5 have a determinant of 1. 3D Math Primer for Graphics & Game Dev**Geometric Interpretation**In 2D, the determinant is equal to the signed area of the parallelogram or skew box that has the basis vectors as two sides. By signed area, we mean that the area is negative if the skew box is flipped relative to its original orientation. 3D Math Primer for Graphics & Game Dev**2 x 2 Determinant as Area**3D Math Primer for Graphics & Game Dev**3 x 3 Determinant as Volume**In 3D, the determinant is the volume of the parallelepiped that has the transformed basis vectors as three edges. It will be negative if the object is reflected (turned inside out) as a result of the transformation. 3D Math Primer for Graphics & Game Dev**Uses of the Determinant**The determinant is related to the change in size that results from transforming by the matrix. The absolute value of the determinant is related to the change in area (in 2D) or volume (in 3D) that will occur as a result of transforming an object by the matrix. The determinant of the matrix can also be used to help classify the type of transformation represented by a matrix. If the determinant of a matrix is zero, then the matrix contains a projection. If the determinant of a matrix is negative, then the matrix contains a reflection. 3D Math Primer for Graphics & Game Dev**Section 6.2:Inverse of a Matrix**3D Math Primer for Graphics & Game Dev**Inverse of a Matrix**The inverse of a square matrix M, denoted M-1 is the matrix such that when we multiply by M-1, the result is the identity matrix. M M-1 = M-1M = I. Not all matrices have an inverse. An obvious example is a matrix with a row or column of zeros: no matter what you multiply this matrix by, you will end up with the zero matrix. If a matrix has an inverse, it is said to be invertible or non-singular. A matrix that does not have an inverse is said to be non-invertible or singular. 3D Math Primer for Graphics & Game Dev**Invertibility and Linear Independence**For any invertible matrix M, the vector equality vM = 0 is true only when v = 0. Furthermore, the rows of an invertible matrix are linearly independent, as are the columns. The rows and columns of a singular matrix are linearly dependent. 3D Math Primer for Graphics & Game Dev**Determinant and Invertibility**The determinant of a singular matrix is zero and the determinant of a non-singular matrix is non-zero. Checking the magnitude of the determinant is the most commonly used test for invertibility, because it's the easiest and quickest. 3D Math Primer for Graphics & Game Dev**The Classical Adjoint**Our method for computing the inverse of a matrix is based on the classical adjoint. The classical adjoint of a matrix M, denoted adjM, is defined to be the transpose of the matrix of cofactors of M. For example, let: 3D Math Primer for Graphics & Game Dev**Computing the Cofactors**Compute the cofactors of M: 3D Math Primer for Graphics & Game Dev**Classical Adjoint of M**The classical adjoint of M is the transpose of the matrix of cofactors: 3D Math Primer for Graphics & Game Dev**Back to the Inverse**The inverse of a matrix is its classical adjoint divided by its determinant: M-1 = adjM / |M|. If the determinant is zero, the division is undefined, which jives with our earlier statement that matrices with a zero determinant are non-invertible. 3D Math Primer for Graphics & Game Dev**Example of Matrix Inverse**If: 3D Math Primer for Graphics & Game Dev**Gaussian Elimination**There are other techniques that can be used to compute the inverse of a matrix, such as Gaussian elimination. Many linear algebra textbooks incorrectly assert that such techniques are better suited for implementation on a computer because they require fewer arithmetic operations. This is true for large matrices, or for matrices with a structure that can be exploited. However, for arbitrary matrices of smaller order like the 2 x 2, 3 x 3, and 4 x 4 used most often in geometric applications, the classical adjoint method is faster. The reason is that the classical adjoint method provides for a branchless implementation, meaning there are no if statements or loops that cannot be unrolled statically. This is a big win on today's superscalar architectures and vector processors. 3D Math Primer for Graphics & Game Dev**Facts About Matrix Inverse**The inverse of the inverse of a matrix is the original matrix. If M is nonsingular, (M-1)-1 = M. The identity matrix is its own inverse: I-1 = I. Note that there are other matrices that are their own inverse, such as any reflection matrix, or a matrix that rotates 180° about any axis. The inverse of the transpose of a matrix is the transpose of the inverse: (MT)-1 = (M-1)T 3D Math Primer for Graphics & Game Dev**More Facts About Matrix Inverse**The inverse of a product is equal to the product of the inverses in reverse order. (AB)-1 = B-1A-1 This extends to more than two matrices: (M1M2…Mn-1Mn)-1 = Mn-1Mn-1-1…. M2-1M1-1 The determinant of the inverse is the inverse of the determinant: |M-1| = 1/|M|. 3D Math Primer for Graphics & Game Dev**Geometric Interpretation of Inverse**The inverse of a matrix is useful geometrically because it allows us to compute the reverse or opposite of a transformation – a transformation that undoes another transformation if they are performed in sequence. So, if we take a vector v, transform it by a matrix M, and then transform it by the inverse M-1 of M, then we will get v back. We can easily verify this algebraically: (vM)M-1 = v(MM-1) = vI = v 3D Math Primer for Graphics & Game Dev**Section 6.3:Orthogonal Matrices**3D Math Primer for Graphics & Game Dev**Orthogonal Matrices**A square matrix M is orthogonal if and only if the product of the matrix and its transpose is the identity matrix: MMT = I. If a matrix is orthogonal, its transpose and the inverse are equal: MT = M-1. If we know that our matrix is orthogonal, we can essentially avoid computing the inverse, which is a relatively costly computation. For example, rotation and reflection matrices are orthogonal. 3D Math Primer for Graphics & Game Dev**Testing Orthogonality**Let M be a 3 x 3 matrix. Let's see exactly what it means when MMT = I. 3D Math Primer for Graphics & Game Dev**9 Equations**This gives us 9 equations, all of which must be true in order for M to be orthogonal: 3D Math Primer for Graphics & Game Dev**Consider the Rows**Let the vectors r1, r2, r3 stand for the rows of M: 3D Math Primer for Graphics & Game Dev**9 Equations Using Dot Product**Now we can re-write the 9 equations more compactly: 3D Math Primer for Graphics & Game Dev**Two Observations**First, the dot product of a vector with itself is 1 if and only if the vector is a unit vector. Therefore, the equations with a 1 on the right hand side of the equals sign will only be true when r1, r2, and r3 are unit vectors. Second, the dot product of two vectors is 0 if and only if they are perpendicular. Therefore, the other six equations (with 0 on the right hand side of the equals sign) are true when r1, r2, and r3 are mutually perpendicular. 3D Math Primer for Graphics & Game Dev**Conclusion**• So, for a matrix to be orthogonal, the following must be true: • Each row of the matrix must be a unit vector. • The rows of the matrix must be mutually perpendicular. • Similar statements can be made regarding the columns of the matrix, since if M is orthogonal, then MT must be orthogonal. 3D Math Primer for Graphics & Game Dev**Orthonormal Bases Revisited**Notice that these criteria are precisely those that we said in Chapter 3 were satisfied by an orthonormal set of basis vectors. There we also noted that an orthonormal basis was particularly useful because we could perform the “opposite” coordinate transform from the one that is always available, using the dot product. When we say that the transpose of an orthogonal matrix is its inverse, we are just restating this fact in the formal language of linear algebra. 3D Math Primer for Graphics & Game Dev**9 is Actually 6**Also notice that 3 of the orthogonality equations are duplicates (since dot product is commutative), and between these 9 equations, we actually have 6 constraints, leaving 3 degrees of freedom. This is interesting, since 3 is the number of degrees of freedom inherent in a rotation matrix. But again note that rotation matrices cannot compute a reflection, so there is slightly more freedom in the set of orthogonal matrices than in the set of orientations in 3D. 3D Math Primer for Graphics & Game Dev**Caveats**When computing a matrix inverse we will usually only take advantage of orthogonality if we know a priori that a matrix is orthogonal. If we don't know in advance, it's probably a waste of time checking. Finally, even matrices which are orthogonal in the abstract may not be exactly orthogonal when represented in floating point, and so we must use tolerances, which have to be tuned. 3D Math Primer for Graphics & Game Dev