• No results found

Chapter 2: Matrix Algebra Overview

2.5 The Inverse of a Matrix

Matrix algebra does not define a division operation, but it does define a multiplicative inverse operation. The following list summarizes the important information about inverses:

Only square matrices have inverses; therefore, when we speak of matrix inverses we assume we are dealing with a square matrix.

The inverse of an n × n matrix M is an n × n matrix denoted as M-1. The inverse is unique.

Not every square matrix has an inverse. A matrix that does have an inverse is said to be invertible , and a matrix that does not have an inverse is said to be singular .

Multiplying a matrix with its inverse results in the identity matrix: MM-1 = M-1M = I. Note that multiplying a matrix with its inverse is a case when matrix multiplication is commutative.

Matrix inverses are useful for solving for other matrices in a matrix equation. For example, suppose that we are given and we are given the change of frame

matrix, M, from frame A to frame B; that is, . We want to solve for . In other words, instead of mapping from A to B, we want the matrix

that maps us from B to A. Then given we can find . To find this matrix, suppose that M is invertible (i.e., M-1 exists). We can solve for like so:

Multiply both sides of the equation by M-1.

MM-1 = I, by definition of inverse.

, by definition of the identity matrix.

Thus the matrix M-1 is the change of frame matrix from B to A.

Figure 2.1 illustrates the relationship between a change of frame matrix and its inverse. Also note that all of the change of frame mappings that we do in this book will be invertible, so we won't have to worry about whether or not the inverse exists.

Figure 2.1: M maps from A to B and M-1 maps from B to A.

Techniques for finding inverses are beyond the scope of this book, but they are described in any linear algebra textbook (it is not difficult; it is just not worth digressing into the procedure here). In §2.6 we will learn about a D3DX function that will find the inverse of a matrix for us, and in the next chapter we will simply give the inverses of the important types of matrices that we will work with in this book.

To conclude this section on inverses we present the following useful property for the inverse of a product: (GH)-1 = H-1 G-1. This property assumes both G and H are invertible and that they are both square matrices of the same dimension. This property is easy to see if we view the matrices G and H as change of frame matrices, as shown in Figure 2.2.

Figure 2.2: G maps from A to B, H maps from B to C, and GH maps from A directly to C. H-1 maps from C to B, G-1 maps from B to A, and H-1 G-1 maps from C directly to A.

2.6 D3DX Matrices

As equation 2.3 illustrated, we can use 1 × 4 and 4 × 4 matrices to map a vector from one frame to another. In the next chapter, we will find that 1 × 4 and 4 × 4 matrices can be used to geometrically transform vectors as well. However, it is important to note that in this book, we generally use 3D vectors up until the time we are ready to multiply them by a matrix; then, right before we do the multiplication, we augment the 3D vector to a 4D vector and place a 0 or 1 in the fourth component based on whether the vector represents a point or a magnitude and direction quantity.

To represent matrices in D3DX, we use the D3DXMATRIX class, which is defined as follows:

typedef struct D3DXMATRIX : public D3DMATRIX {

D3DXMATRIX& operator *= ( CONST D3DXMATRIX& );

D3DXMATRIX& operator += ( CONST D3DXMATRIX& );

D3DXMATRIX& operator - = ( CONST D3DXMATRIX& );

Observe that D3DXMATRIX inherits its data members from D3DMATRIX , which is defined as:

typedef struct _D3DMATRIX {

The D3DXMATRIX class has a myriad of useful operators, such as testing for equality, adding, subtracting, and multiplying matrices. In addition, the overloaded parenthesis operator provides a convenient syntax for accessing the elements in a matrix.

In addition to the above class, the D3DX library includes the following four useful functions for obtaining the 4 × 4 identity matrix, computing the transpose of a matrix, computing the inverse of a matrix, and multiplying a 1 × 4 vector and a 4 × 4 matrix.

D3DXMATRIX *D3DXMatrixIdentity(

Note Remember to link the d3dx9.lib library file with your application to use any D3DX code; moreover, you will also need to #include <d3dx9.h>.

The following code provides some examples on how to use the D3DXMATRIX class and the four functions listed above.

#include <d3dx9.h>

#include <iostream>

using namespace std;

// Overload the "<<" operators so that we can use cout to // output D3DXVECTOR4 and D3DXMATRIX objects.

ostream& operator<<(ostream& os, D3DXVECTOR4& v) {

os << "(" << v.x << ", " << v.y << ", "

<< v.z << ", " << v.w << ")";

return os;

}

ostream& operator<<(ostream& os, D3DXMATRIX& m) {

D3DXMatrixTranspose(&D, &A);

D3DXMatrixInverse(&E, 0, &A);

2.7 Summary

An m × n matrix M is a rectangular array of real numbers with m rows and n columns. Two matrices of the same dimensions are equal if and only if their corresponding components are equal. We add two matrices of the same dimension by adding their corresponding elements. We multiply a scalar and a matrix by multiplying the scalar with every element in the matrix.

If A is an m × n matrix and B is an n × p matrix, then the product AB is defined and is an m × p matrix C, where the ijth entry of the product C is

given by taking the dot product of the ith row vector in A with the jth column vector in B; that is, .

Given = (x, y, z, w) that specifies a vector (if w = 0) or point (if w = 1) relative to frame A, we obtain the same vector/point identified by = (x', y', z', w) relative to frame B by performing the vector-matrix multiplication:

where the vectors , , , and describe the origin, x-axis, y-axis, and z-axis, respectively, of frame A relative to frame B.

Suppose we have three frames, F, G, and H, and let A be the change of frame matrix from F to G, and let B be the change of frame matrix from G to H. Using matrix-matrix multiplication, the matrix C = AB can be thought of as the change of frame matrix from F directly to H; that is, matrix-matrix

multiplication combines the effects of A and B into one net matrix, and so we can write: .

The transpose of a matrix is found by interchanging the rows and columns of the matrix. Thus the transpose of an m × n matrix is an n × m matrix.

We denote the transpose of a matrix M as MT.

The identity matrix is a square matrix that has zeros for all elements except along the main diagonal, and moreover, the elements along the main diagonal are all ones.

Multiplying a matrix with its inverse results in the identity matrix: MM-1 = M-1M = I. The inverse of a matrix, if it exists, is unique. Only square matrices have inverses and even then, a square matrix may not be invertible.

2.8 Exercises

1. Let

Compute the following matrix products: ST, TS, , , and . Does ST = TS?

2. Show that

3. Using S and T from exercise 1, compute ST, TT, and (ST)T. What is (ST)T and (TT)T? Does (ST)T = TT ST? 4. Using S and T from exercise 1, verify that (ST)-1 = T-1 S-1. (Use D3DXMatrixInverse to do the calculations.)

5. Write the following linear combination as a vector-matrix multiplication: = 2(1, 2, 3) + -4(-5, 0, -1) + 3(2, -2, -3) 6. Redo exercise 11 from Chapter 1 using Equation 2.3.

7. Show that

Chapter 3: Transformations and Planes