Matrices Function

This is a discussion on Matrices Function within the C Programming forums, part of the General Programming Boards category; I have another noob question about matrices, again. Say I have a matrix A and a vector b. I want ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    14

    Matrices Function

    I have another noob question about matrices, again.

    Say I have a matrix A and a vector b. I want to create a function that will give me a new matrix C = A*A and also in the same function, calculate another vector D = A*b. If I call the function from main, how do I go about getting the function to return either C or D, or both C and D? Is this possible? Before, I used if/else for different equations in a function but this is matrices, so I'm confused on how to specify it. Any help is appreciated, thanks!

  2. #2
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    I guess you are declaring your matrix as a two dimensional float array. Right?
    You can make your function return a pointer to that array.
    A function can have more than one return statements - like
    Code:
      if (flag)
         return a;
       else
         return b;
    But, the function cannot return both.
    You can also try declaring the arrays in the global space. That way you can have both the arrays modified after the function call...
    In the middle of difficulty, lies opportunity

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Code:
    func ( const mat *A, const vec *b, mat *C, vec *D );
    You just pass relevant pointers to where you want the various answers stored, and you can obtain as many results as you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    14
    Thanks for the help but I have a question about the pointers. I'm starting to use malloc and calloc so I'm wondering if this is the correct way of doing it--because I've run into errors while compiling. I suspect it might be the malloc and calloc.

    Code:
    double **A, *b;
    A = (double **)calloc(3, sizeof(double *)); 
    for(row=0; row<3; row++) A[row] = (double *)calloc(3, sizeof(double));
    b = (double *)malloc(3 * sizeof(double));
    Also if I have pointers like that, and I want to create the function as I mentioned above, then my func would be something like this?

    Code:
    void multmatrix(double **A[3][3], double *b[3])
    {
    blah here;
    return;
    }
    And when I call it, I should use:

    Code:
    multmatrix(A,b);

    Am I close?

    Nevermind! I got it! Thank much for the help!
    Last edited by lazyturtle; 06-06-2007 at 11:52 PM.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    If you know the size of both matrices and those sizes will not change then it is much easier to just do:

    Code:
    void MatMult4x4(float mat1[4][4],float mat2[4][4],float result[4][4])
    {
      for (int i=0;i<4;i++)
      {
         for (int j=0;j<4;j++)
         {
            result[i][j]=0.0f;
            for (int k=0;k<4;k++)
            {
               result[i][j]+=mat1[i][k]*mat2[k][j];
            }
          }
      }
    }
    If you have variable sized matrices and are not against using C++ then I would recommend creating a matrix class that can handle all types and sizes of matrices. You could then write generic C functions that know how to concatenate as well as IF two matrices can be concatenated.

    This is the approach I used in the past before the D3DX library was widely used and available.
    My matrix class would control all aspects of a matrix and I had a generic matrix library that could multiply different sized matrices together provided the sizes were provided by the caller.

    There are cases in which two matrices cannot be concatenated and your code should check for this. It is not the easiest task to create an all-inclusive matrix library that can handle any size matrix. My use of matrices has always been directly linked to doing 3D graphics and as such I rarely concatenate any matrix over 4x4 or under 3x3.

    In order to code a function to multiply a vector by a matrix you also must satsify certain conditions. If you know how many components a vector has and the size of the matrix then the code is easy. Coding a universal function to multiply matrices and vectors regardless of size is also not the easiest task.

    This is how you multiply a 3D vector by a 4x4 matrix.

    Code:
    struct Vector3
    {
       float x,y,z;
    };
    
    void VectorMul4x4(const Vector3 in,const float Mat[4][4],Vector3 &out)
    {
      out.x=in.x*mat[0][0]+in.y*mat[0][1]+in.z*mat[0][2]+mat[0][3];
      out.y=in.x*mat[1][0]+in.y*mat[1][1]+in.z*mat[1][2]+mat[1][3];
      out.z=in.x*mat[2][0]+in.y*mat[2][1]+in.z*mat[2][2]+mat[2][3];
    }
    Note that this code only works for right handed coordinate systems.

    I would not create a function that did everything you wanted in one swoop. It is much easier to create several generic functions that will concatenate matrices and that will multiply vectors by matrices or more appropriately transform a vector by a matrix. If you have the Direct3D SDK you can look in the help file and it will show you how to transform matrices and vectors and the math behind it. There is also tons of information on the net about this topic. I recommend www.gamedev.net which has several articles on this topic as well as a lot of source code. Another is www.gamasutra.com which also has tons of information on this very topic. Since 3D graphics are so popular and since matrices and 3D graphics are like bread and butter, it's very easy now to find tons of resources about operations on vectors and matrices. I recommend you read up on vectors and matrices and the different operations for both before coding anything.
    Last edited by VirtualAce; 06-07-2007 at 01:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM

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