Scalar Multiplication

 < Day Day Up > 

The preceding chapter discussed scalar multiplication with vectors. Remember, a vector is just a single row or single-column matrix, so scalar multiplication works the same way with matrices as it does with vectors. In fact, the preceding section had an example of scalar multiplication in the discussion of subtraction. At one point, there was a suggestion that rather than subtracting corresponding entries, you might want to add the negative of the second matrix. Flipping the sign of each entry is the same as multiplying the matrix by 1.

graphics/05equ01.gif


for any scalar value c and any size matrix A.


Just like flipping the sign of each entry, simply multiply each entry by the scalar value. This process works for any size matrix (not just square matrices) and any scalar (even fractions and negative numbers ).

Example 5.6: Scalar * Matrix

Find 5B for matrix B = graphics/05inl01.gif

Solution

Simply multiply each entry by 5:

graphics/05equ02.gif


Processing scalar multiplication is the first step to doing some seriously cool work with matrices. This function shows how to multiply matrices by scalar values.

 Matrix3X3 scalarMultiply(Matrix3X3 a, float scale)     {       Matrix3X3 temp;       for(int i = 0;i<3;i++)         {         for(int j=0;j<3;j++)           {            temp.index[i][j] = (a.index[i][j] * scale);            }          }       return temp;     } 

Again, we visit each position and multiply it through by the specified scale parameter. Hopefully, you are beginning to see a pattern with matrix usage. The sample programs for this chapter contain example programs that can perform scalar multiplication. Be sure to check them out!

Now that you can add and subtract matrices as well as multiply and divide by scalars, you can perform any combination of those operations to algebraically manipulate matrices in equations. The same rules of algebra and order of operations for scalar numbers apply to matrices as well. The only difference is that you're adding or multiplying matrices rather than single numbers.

Example 5.7: Equations with Matrices

Given matrices A = graphics/05inl02.gif =, find matrix X if 2X = 3A B.

Solution
  1. Look at the equation 2X = 3A B. If you need to solve for matrix X, you need to use the rules of algebra to get X by itself, which means multiplying both sides by (or dividing by 2):

    2X = 3A B

    (2X) = (3A B)

    X = (3A B)

  2. The same order of operations you would use on numbers applies here as well. This means that you start inside the parentheses with the 3A B:

    graphics/05equ03.gif


  3. The last step in this case is to multiply each entry in matrix (3A B) by (or divide by 2):

    graphics/05equ04.gif


So far, we've discussed adding and subtracting matrices as well as multiplying and dividing matrices by scalar quantities . This lets you algebraically manipulate matrices in an equation just as you would plain old numbers and variables . The next natural question to ask is, "How do I multiply a matrix times a matrix?" That is precisely what the next section addresses.

Self-Assessment

1.

graphics/05nil16.gif

2.

graphics/05nil02.gif

3.

Using matrices A and B from questions 1 and 2, find matrix X if



Beginning Math and Physics for Game Programmers
Beginning Math and Physics for Game Programmers
ISBN: 0735713901
EAN: 2147483647
Year: 2004
Pages: 143
Authors: Wendy Stahler

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net