Muzzafarath 146 Report post Posted June 14, 2000 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 0 Share this post Link to post Share on other sites

Matt2000 122 Report post Posted June 14, 2000 The solution:sum += m1.v[row][col] * m2.v[col][row]; Check this out: HREF = "http://members.xoom.com/mattstud" 0 Share this post Link to post Share on other sites

Guest Anonymous Poster Report post Posted June 14, 2000 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 a33and your second one like this: b11 b12 b13 b21 b22 b23 b31 b32 b33Asume the result wil look like this: c11 c12 c13 c21 c22 c23 c31 c32 c33Element 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 0 Share this post Link to post Share on other sites

Muzzafarath 146 Report post Posted June 14, 2000 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 /. MuzzafarathMad House SoftwareEdited by - Muzzafarath on June 14, 2000 4:16:53 PM 0 Share this post Link to post Share on other sites

Muzzafarath 146 Report post Posted June 14, 2000 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;}/. MuzzafarathMad House SoftwareEdited by - Muzzafarath on June 14, 2000 4:29:34 PMEdited by - Muzzafarath on June 14, 2000 4:30:36 PM 0 Share this post Link to post Share on other sites

ncsu121978 1344 Report post Posted June 14, 2000 If you are using direct3d you can just do thisD3DMATRIX a = whatever;D3DMATRIX b = whatever;D3DMATRIX answer;answer = a * b;and that will multiply themdirect3d has the multiply function overloaded to multiply matrices "Now go away or I shall taunt you a second time" - Monty Python and the Holy GrailthemGames Productions 0 Share this post Link to post Share on other sites

Muzzafarath 146 Report post Posted June 15, 2000 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 /. MuzzafarathMad House Software 0 Share this post Link to post Share on other sites