I'm having some problems implementing a function that'll multiply two matrices. I've been testing it with the identity matrix, since I've heard that any matrix multiplied with the identity matrix won't change (which makes it good for testing my function
). Here's my matrix struct:
typedef struct
{
float v[4][4];
} MATRIX;
Here's how I construct my identity matrix:
const MATRIX identity_matrix(void)
{
MATRIX result;
memset(&result, 0, sizeof(result));
result.v[0][0] = result.v[1][1] = result.v[2][2] = result.v[3][3] = 1;
return result;
}
And this is the function that should multiply two matrices:
MATRIX matrix_mul(MATRIX m1, MATRIX m2)
{
MATRIX result;
int col, row;
float sum;
for(col = 0; col < 4; col++)
{
sum = 0;
for (row = 0; row < 4; row++)
{
sum += m1.v[row][col] * m2.v[row][col];
result.v[row][col] = sum;
}
}
return result;
}
And this is the function I use to print matrices to screen (but I don't think that there's anything wrong with it):
void matrix_print(MATRIX m)
{
printf("[%.0f, %.0f, %.0f, %.0f]\n", m.v[0][0], m.v[0][1], m.v[0][2], m.v[0][3]);
printf("[%.0f, %.0f, %.0f, %.0f]\n", m.v[1][0], m.v[1][1], m.v[1][2], m.v[1][3]);
printf("[%.0f, %.0f, %.0f, %.0f]\n", m.v[2][0], m.v[2][1], m.v[2][2], m.v[2][3]);
printf("[%.0f, %.0f, %.0f, %.0f]\n", m.v[3][0], m.v[3][1], m.v[3][2], m.v[3][3]);
}
I'm sorry if the code looks weird, but it's not my fault, it's the forums fault
I can add and subtract matrices, but this multiplying stuff really makes my head hurt
Can anyone help me figure out what's wrong with the matrix_mul() function?
/.
Muzzafarath
Mad House Software
Edited by - Muzzafarath on 6/14/00 12:37:23 PM
Edited by - Muzzafarath on 6/14/00 12:38:02 PM
Edited by - Muzzafarath on 6/14/00 12:39:05 PM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall