Matrix Addition and Subtraction

 < Day Day Up > 

Now that we've discussed checking for equal matrices, addition and subtraction will follow quite naturally. The preceding section said that corresponding entries are positioned in the same row and column location, and if two matrices are the same size with equal corresponding entries, they must be equal. For you to add matrices, they must also have the same dimensions. You just add corresponding entries.

Adding Matrices

For two matrices of the same size, add the corresponding entries.


NOTE

Always remember that two matrices must have the same dimensions for you to add them (just like equal matrices).

Also, some math texts refer to a matrix's dimensions as its order , so don't be thrown if you see that term used.


Example 5.4: Adding Matrices

Find A + B for matrices A and B in Figure 5.10.

Figure 5.10. Adding matrices A and B.

graphics/05fig10.gif

Solution
  1. Always make sure that both matrices are the same size before you attempt to add them. In this case, A and B are both 3x3, so you can add them together.

  2. Add all the corresponding entries. Add the two entries in row 0, column 0, which means that 1 + (3) = 2, and so forth.

    Figure 5.11 shows the end result of adding all the corresponding entries.

    Figure 5.11. Matrix A + B.

    graphics/05fig11.gif

Here is a sample function that shows how to add matrices in code:

 Matrix3X3 addMatrices(Matrix3X3 a, Matrix3X3 b)     {        Matrix3X3 temp;          for(int i = 0;i<3;i++){              for(int j=0;j<3;j++){                 temp.index[i][j] = (a.index[i][j] + b.index[i][j]);                  }             }         return temp;     } 

This function returns a Matrix3x3 that contains the result of the addition. We access each location adding the values contained and store them out into a separate matrix to return. Check the Chapter 5 programming examples to see an example of matrix addition using this function.

One quick tip! A 4x4 matrix has 16 locations to hold data. If you are adding two of them, you have to manually enter 32 individual data locations. This is not a bad thing unless you actually have work to do. It's useful to create quick random matrices based on a user -entered seed value to speed up the process to verify that your code is working. Here is a quick example:

 //create a random matrix controlled by the seed Matrix3X3 createRandom3X3Matrix(int seed)     {      Matrix3X3 temp;         for(int i = 0;i<3;i++)             {             for(int j=0;j<3;j++)                 {                  temp.index[i][j] = rand()%seed;                 }             }         return temp;     } 

Using random isn't really a great way to get truly random data, but that is a discussion for another time.

Subtracting matrices works exactly the same way; you just subtract corresponding entries.

Subtracting Matrices

For two matrices of the same size, subtract the corresponding entries.


NOTE

When subtracting matrices by hand, it's easy to lose track of plus and minus signs. You might want to consider adding the negative of the second matrix. Just flip the signs of each entry in the second matrix, and then add them.


Example 5.5: Subtracting Matrices

Find A B for matrices A and B in Figure 5.10 (see Example 5.4).

Solution
  1. Always make sure that both matrices are the same size before you attempt to subtract them. In this case, A and B are both 3x3, so you can subtract them.

  2. Subtract all the corresponding entries. Subtract the two entries in row 0, column 0, which means that 1 (3) = 4, and so forth.

  3. Figure 5.12 shows the end result of subtracting all the corresponding entries.

    Figure 5.12. Matrix A B.

    graphics/05fig12.gif

Subtracting matrices in code is just like working with addition. Here is a function that adds two 4x4 matrices:

 Matrix4X4 subtractMatrices(Matrix4X4 a, Matrix4X4 b) {       Matrix4X4 temp;         for(int i = 0;i<4;i++)             {             for(int j=0;j<4;j++)                 {                 temp.index[i][j] = (a.index[i][j] - b.index[i][j]);                  }             }         return temp;     } 

The same principle applies here as with addition. Visit each index in the array and subtract the components from each other. The sample code for Chapter 5 contains a functional example using this code.

The most important thing to remember with adding and subtracting matrices is that they must be the same size. If you ask the computer to add or subtract matrices that are different sizes, it will most likely crash. The other place to be careful is when subtracting matrices; it's very easy to make a mistake when subtracting negative entries. Quite often, careless errors in subtracting matrices lead to the most frustrating bugs to locate, so take your time.

Self-Assessment

1.

Find C + D for matrices C and D in Figure 5.13.

Figure 5.13. Matrices C and D.

graphics/05fig13.gif

2.

Find C D for matrices C and D in Figure 5.13.

3.

Find F + G for matrices F and G in Figure 5.14.

Figure 5.14. Matrices F and G.

graphics/05fig14.gif

4.

Find F G for matrices F and G in Figure 5.14.


 < Day Day Up > 


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