#### Archived

This topic is now archived and is closed to further replies.

# Multiplying matrices...

This topic is 6611 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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

##### Share on other sites
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

Edited by - Muzzafarath on June 14, 2000 4:16:53 PM

##### Share on other sites
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[ i][j] = m1.v[ i][0] * m2.v[0][j] + m1.v[ i][1] * m2.v[1][j] + m1.v[ i][2] * m2.v[2][j] + m1.v[ i][3] * m2.v[3][j]; } } return result;}
/. Muzzafarath

Edited by - Muzzafarath on June 14, 2000 4:29:34 PM

Edited by - Muzzafarath on June 14, 2000 4:30:36 PM

##### Share on other sites
If you are using direct3d you can just do this

D3DMATRIX a = whatever;
D3DMATRIX b = whatever;

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

##### Share on other sites
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

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002295
×