Multiplying matrices...

Started by
5 comments, last by Muzzafarath 23 years, 10 months ago
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
Advertisement
The solution:

sum += m1.v[row][col] * m2.v[col][row];

Check this out: HREF = "http://members.xoom.com/mattstud"
Check this out: Matthew Studios
when you multiply two matrices, the result gives a new matrix. Every element is the result of the dot-product of a row of the first matrix, with a column of the second matrix. That''s what you are doing wrong (i think :-)

This is the line that''s wrong:

sum += m1.v[row][col] * m2.v[row][col];

It should be:

sum += m1.v[row][col] * m2.v[col][row];

Example:

say your first matrix looks like this:

a11 a12 a13
a21 a22 a23
a31 a32 a33

and your second one like this:

b11 b12 b13
b21 b22 b23
b31 b32 b33


Asume the result wil look like this:

c11 c12 c13
c21 c22 c23
c31 c32 c33

Element c11 would be:

c11 = (a11*b11) + (a12*b21) + (a13*b31)

Element c23 would then be:

c23 = (a21*b13) + (a22*b23) + (a23*b33)

There you go. Hope this helps,

Koen


Thanks for the replies, but I don't think it's working. I've changed the matrix_mul() function to this:

MATRIX matrix_mul(MATRIX m1, MATRIX m2)
{
MATRIX result;

int col, row;
long sum;

for(col = 0; col < 4; col++)
{
sum = 0;

for (row = 0; row < 4; row++)
{
sum += m1.v[row][col] * m2.v[col][row];

result.v[row][col] = sum;

}
}

return result;
}

When I multiply the matrix

[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 1, 0]
[0, 0, 0, 1]

with

[5, 0, 0, 3]
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 7, 0]

I get

[5, 0, 0, 0]
[5, 0, 0, 0]
[5, 0, 0, 0]
[5, 0, 0, 0]

Shouldn't I get

[5, 0, 0, 3]
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 7, 0]

since the first matrix is the identity matrix, and identity matrix multiplied with some other matrix returns "the other matrix"?

I've got to get a book on this subject! It's way too hard to learn this stuff from tutorials on the internet

/. Muzzafarath
Mad House Software

Edited by - Muzzafarath on June 14, 2000 4:16:53 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
Nevermind, I figured it out my self (well, not on my own, someone helped me, but it solved my problem ):

MATRIX matrix_mul(MATRIX m1, MATRIX m2)
{
MATRIX result;
int i, j;

for (j = 0; j < 4; j++)
{
for (i = 0; i < 4; i++)
{
result.v[j] = m1.v[0] * m2.v[0][j] + m1.v[1] * m2.v[1][j] +<br> m1.v[2] * m2.v[2][j] + m1.v[3] * m2.v[3][j];<br> }<br> }<br><br> return result;<br>}<br></code><br>/. <a href=mailto:de@goteborg.mail.teila.com>Muzzafarath</a><br><a href=http://w1.315.telia.com/~u31504183/>Mad House Software</a><br><br>Edited by - Muzzafarath on June 14, 2000 4:29:34 PM<br><br>Edited by - Muzzafarath on June 14, 2000 4:30:36 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
If you are using direct3d you can just do this

D3DMATRIX a = whatever;
D3DMATRIX b = whatever;
D3DMATRIX answer;
answer = a * b;

and that will multiply them
direct3d has the multiply function overloaded to multiply matrices


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Yes I know that D3D has premade matrix operators and such, but I''ll learn a lot more about matrices if I write my own

/. Muzzafarath
Mad House Software
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

This topic is closed to new replies.

Advertisement