Matrices and Multiplication

This is a discussion on Matrices and Multiplication within the Game Programming forums, part of the General Programming Boards category; I am reading the NeHe tutorial in Vectors and Matrices. In 7th grade, my math teacher there is no way ...

  1. #1
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question Matrices and Multiplication

    I am reading the NeHe tutorial in Vectors and Matrices. In 7th grade, my math teacher there is no way to multiply matrices with diff. columns and rows. I found different on the tutorial. Can someone explain exactly how to do this?

    In the tutorial:

    | 1 3 | | 1 3 5 |
    M = | 2 4 | X = | 2 4 6 |

    M*X= | 7 15 23 |
    | 10 22 34 |


    The tutorial says:

    Now, lets get on with doing the actual matrix multiplication! To do this, you multiply each row of the first matrix by
    each column of the second matrix.


    Can anyone explain how to get M*X?
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    In doing 3D, each matrix should be the same size so this does not matter.


    Code:
    void MatrixMultiply(double matrix1[4][4],double matrix2[4][4],double result[4][4])
    {
      for (int i=0;i<4;i++)
      {
         for (int j=0;j<4;j++)
         {
            result[i][j]=0;
            for (int k=0;k<4;k++)
            {
               result[i][j]+=(matrix1[i][k]*matrix2[k][j]);
            }
         }
       }
    }
    If you want it to be a bit faster concatenating then unroll the second loop.

    Code:
    void MatrixMultiply(double matrix1[4][4],double matrix2[4][4],double result[4][4])
    {
      for (int i=0;i<4;i++)
      {
         for (int j=0;j<4;j++)
         {
            result[i][j]=((matrix1[i][0]*matrix2[0][j])
               + (matrix1[i][1]*matrix2[1][j])
               + (matrix1[i][2]*matrix2[2][j])
               + (matrix1[i][3]*matrix2[3][j]));
            
         }
       }
    }
    Unrolling the first or second loop will not give you any significant speed gains.

    I'm not sure exactly what OpenGL does with its matrices or how it concatenates them. Seems that every program does it a bit different than the next.

    AFAIK, you cannot multiply matrices of different sizes - you cannot correctly concatenate matrices of unlike sizes. In using matrices your vertexes must be in the form x,y,z,t and your final screen coords will be x,y,t. The t is just so that the math comes out right and should always be 1. The only way I know that you could try to concatenate diff sizes is to change the i and j values in the for loop, but it will still not come out right mathematically.

    Perhaps there was a misprint on the page.

  3. #3
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    Matrix multiplication is this:
    any matrix A of size m x n can only be multiplied by matrix B if B is an n x k matrix. The resulting matrix will be an m x k matrix. The matrix form is rows x cols. It is possible to do multiplication of different sized matrices as long as this condition is met.
    So a 3x3 matrix can be multiplied by a 3x1 matrix to result in a 3x1 matrix. Notice that matrix multiplication is not necessarily associative.
    Many useful things can be derived from matrix manipulation. You can learn more about it in linear algebra.
    A hint- you can do systems of equations very quickly once you know some of these concepts, instead of doing it the long and hard way like you'll be taught in you math classes. I always hated how they made you do it the hard way for a while and then showed you the easy way. Walk before you can run I guess.

  4. #4
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    Oh, and Bubba's right; in game programming, you'll most likely dealing with 4x4 matrices. And most of your graphics APIs will have the functions to do the matrix operations for you so you won't really have to worry much about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Multiplication - Weird things happening
    By spoon_ in forum C Programming
    Replies: 4
    Last Post: 09-02-2005, 08:53 PM
  2. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-14-2004, 12:19 AM
  3. Very handy matrix functions - part 1
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 05-20-2004, 11:38 AM
  4. Matrix multiplication using 1-d arrays
    By Befuddled in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2003, 10:35 PM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 09:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21