1. ## 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. 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...

3. 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.

4. 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!

5. 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.