kaitokid41 Posted February 22, 2012 Share Posted February 22, 2012 Good day, guys... it seems I'm messing up my algorithm to multiply two(2) 4x4 Matrix and put it in a table. Can somebody point out what's wrong with my code? THANKS IN ADVANCE!!! Here's the situation. I'm getting values from two(2) 4x4 tables, multiply them, and store them in another table. Here's the "math" part of my code: <?php echo "<br/><br/><br/><br/><br/><br/><br/><br/><br/>"; $i=0; for($row = 0; $row < 4; $row++){ for($column = 0; $column < 4; $column++){ $sum = 0; for($ctr = 0; $ctr < 4; $ctr++){ $sum = $sum + ($cell[$i][$row][$ctr] * $cell[$i+1][$ctr][$row]); } $sol[$i][$row][$column] = $sum; } } ?> <table width="50" border="5" align = "left" cellpadding="5"> <tr> <td><?php echo $sol[$i][0][0] ?></td> <td><?php echo $sol[$i][0][1] ?></td> <td><?php echo $sol[$i][0][2] ?></td> <td><?php echo $sol[$i][0][3] ?></td> </tr> <tr> <td><?php echo $sol[$i][1][0] ?></td> <td><?php echo $sol[$i][1][1] ?></td> <td><?php echo $sol[$i][1][2] ?></td> <td><?php echo $sol[$i][1][3] ?></td> </tr> <tr> <td><?php echo $sol[$i][2][0] ?></td> <td><?php echo $sol[$i][2][1] ?></td> <td><?php echo $sol[$i][2][2] ?></td> <td><?php echo $sol[$i][2][3] ?></td> </tr> <tr> <td><?php echo $sol[$i][3][0] ?></td> <td><?php echo $sol[$i][3][1] ?></td> <td><?php echo $sol[$i][3][2] ?></td> <td><?php echo $sol[$i][3][3] ?></td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/257559-4x4-matrix-solver-multiplication/ Share on other sites More sharing options...
requinix Posted February 22, 2012 Share Posted February 22, 2012 That's not all your code. Can you post the whole thing? Quote Link to comment https://forums.phpfreaks.com/topic/257559-4x4-matrix-solver-multiplication/#findComment-1320214 Share on other sites More sharing options...
sasa Posted February 23, 2012 Share Posted February 23, 2012 change $sum = $sum + ($cell[$i][$row][$ctr] * $cell[$i+1][$ctr][$row]); to $sum = $sum + ($cell[$i][$row][$ctr] * $cell[$i+1][$ctr][$column]); Quote Link to comment https://forums.phpfreaks.com/topic/257559-4x4-matrix-solver-multiplication/#findComment-1320297 Share on other sites More sharing options...
kaitokid41 Posted February 23, 2012 Author Share Posted February 23, 2012 change $sum = $sum + ($cell[$i][$row][$ctr] * $cell[$i+1][$ctr][$row]); to $sum = $sum + ($cell[$i][$row][$ctr] * $cell[$i+1][$ctr][$column]); THANKS! So that's why it gives the wrong solution. I multiplied it in the with the wrong row/column! THANK YOU SO MUCH!!! That's not all your code. Can you post the whole thing? Sure, cuz I need help for something more than just this. Hehe. Here's my code. After that multiplication, I should copy the succeeding 4 matrices and place it beside my solution. Then I multiply the first 2 again, until one matrix is left. Any idea how will I do it? By the way, here's the 'string': rotx(90);trans(2,1,-1);roty(90);rotz(90);trans(1,-1,2);roty(90) ...THANKS! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Matrix Solver</title> </head> <body> <?php $transformation = $_POST['string']; $transbits = explode(";",$transformation); for($i=0; $i<count($transbits);$i++){ if(strcmp(substr($transbits[$i],0,5),"trans")==0){ preg_match('#\((.*?)\)#', $transbits[$i], $match); $extrans = explode(",",$match[1]); ?> <table width="50" border="5" align="left" cellpadding="5"> <tr> <td><?php $cell[$i][0][0] = 1; echo $cell[$i][0][0] ?></td> <td><?php $cell[$i][0][1] = 0; echo $cell[$i][0][1] ?></td> <td><?php $cell[$i][0][2] = 0; echo $cell[$i][0][2] ?></td> <td><?php $cell[$i][0][3] = $extrans[0]; echo $cell[$i][0][3] ?></td> </tr> <tr> <td><?php $cell[$i][1][0] = 0; echo $cell[$i][1][0] ?></td> <td><?php $cell[$i][1][1] = 1; echo $cell[$i][1][1] ?></td> <td><?php $cell[$i][1][2] = 0; echo $cell[$i][1][2] ?></td> <td><?php $cell[$i][1][3] = $extrans[1]; echo $cell[$i][1][3] ?></td> </tr> <tr> <td><?php $cell[$i][2][0] = 0; echo $cell[$i][2][0] ?></td> <td><?php $cell[$i][2][1] = 0; echo $cell[$i][2][1] ?></td> <td><?php $cell[$i][2][2] = 1; echo $cell[$i][2][2] ?></td> <td><?php $cell[$i][2][3] = $extrans[2]; echo $cell[$i][2][3] ?></td> </tr> <tr> <td><?php $cell[$i][3][0] = 0; echo $cell[$i][3][0] ?></td> <td><?php $cell[$i][3][1] = 0; echo $cell[$i][3][1] ?></td> <td><?php $cell[$i][3][2] = 0; echo $cell[$i][3][2] ?></td> <td><?php $cell[$i][3][3] = 1; echo $cell[$i][3][3] ?></td> </tr> </table> <?php } else if(strcmp(substr($transbits[$i],0,3),"rot")==0){ preg_match('#\((.*?)\)#', $transbits[$i], $match); //echo "$match[1] <br/>"; if (strcmp(substr($transbits[$i],3,1),"x")==0){ ?> <table width="50" border="5" align="left" cellpadding="5"> <tr> <td><?php $cell[$i][0][0] = 1; echo $cell[$i][0][0] ?></td> <td><?php $cell[$i][0][1] = 0; echo $cell[$i][0][1] ?></td> <td><?php $cell[$i][0][2] = 0; echo $cell[$i][0][2] ?></td> <td><?php $cell[$i][0][3] = 0; echo $cell[$i][0][3] ?></td> </tr> <tr> <td><?php $cell[$i][1][0] = 0; echo $cell[$i][1][0] ?></td> <td><?php $cell[$i][1][1] = intval(cos(deg2rad($match[1]))); echo $cell[$i][1][1] ?></td> <td><?php $cell[$i][1][2] = intval(-sin(deg2rad($match[1]))); echo $cell[$i][1][2] ?></td> <td><?php $cell[$i][1][3] = 0; echo $cell[$i][1][3] ?></td> </tr> <tr> <td><?php $cell[$i][2][0] = 0; echo $cell[$i][2][0] ?></td> <td><?php $cell[$i][2][1] = intval(sin(deg2rad($match[1]))); echo $cell[$i][2][1] ?></td> <td><?php $cell[$i][2][2] = intval(cos(deg2rad($match[1]))); echo $cell[$i][2][2] ?></td> <td><?php $cell[$i][2][3] = 0; echo $cell[$i][2][3] ?></td> </tr> <tr> <td><?php $cell[$i][3][0] = 0; echo $cell[$i][3][0] ?></td> <td><?php $cell[$i][3][1] = 0; echo $cell[$i][3][1] ?></td> <td><?php $cell[$i][3][2] = 0; echo $cell[$i][3][2] ?></td> <td><?php $cell[$i][3][3] = 1; echo $cell[$i][3][3] ?></td> </tr> </table> <?php } else if (strcmp(substr($transbits[$i],3,1),"y")==0){ ?> <table width="50" border="5" align="left" cellpadding="5"> <tr> <td><?php $cell[$i][0][0] = intval(cos(deg2rad($match[1]))); echo $cell[$i][0][0] ?></td> <td><?php $cell[$i][0][1] = 0; echo $cell[$i][0][1] ?></td> <td><?php $cell[$i][0][2] = intval(sin(deg2rad($match[1]))); echo $cell[$i][0][2] ?></td> <td><?php $cell[$i][0][3] = 0; echo $cell[$i][0][3] ?></td> </tr> <tr> <td><?php $cell[$i][1][0] = 0; echo $cell[$i][1][0] ?></td> <td><?php $cell[$i][1][1] = 1; echo $cell[$i][1][1] ?></td> <td><?php $cell[$i][1][2] = 0; echo $cell[$i][1][2] ?></td> <td><?php $cell[$i][1][3] = 0; echo $cell[$i][1][3] ?></td> </tr> <tr> <td><?php $cell[$i][2][0] = intval(-sin(deg2rad($match[1]))); echo $cell[$i][2][0] ?></td> <td><?php $cell[$i][2][1] = 0; echo $cell[$i][2][1] ?></td> <td><?php $cell[$i][2][2] = intval(cos(deg2rad($match[1]))); echo $cell[$i][2][2] ?></td> <td><?php $cell[$i][2][3] = 0; echo $cell[$i][2][3] ?></td> </tr> <tr> <td><?php $cell[$i][3][0] = 0; echo $cell[$i][3][0] ?></td> <td><?php $cell[$i][3][1] = 0; echo $cell[$i][3][1] ?></td> <td><?php $cell[$i][3][2] = 0; echo $cell[$i][3][2] ?></td> <td><?php $cell[$i][3][3] = 1; echo $cell[$i][3][3] ?></td> </tr> </table> <?php } else if (strcmp(substr($transbits[$i],3,1),"z")==0){ ?> <table width="50" border="5" align="left" cellpadding="5"> <tr> <td><?php $cell[$i][0][0] = intval(cos(deg2rad($match[1]))); echo $cell[$i][0][0] ?></td> <td><?php $cell[$i][0][1] = intval(-sin(deg2rad($match[1]))); echo $cell[$i][0][1] ?></td> <td><?php $cell[$i][0][2] = 0; echo $cell[$i][0][2] ?></td> <td><?php $cell[$i][0][3] = 0; echo $cell[$i][0][3] ?></td> </tr> <tr> <td><?php $cell[$i][1][0] = intval(sin(deg2rad($match[1]))); echo $cell[$i][1][0] ?></td> <td><?php $cell[$i][1][1] = intval(cos(deg2rad($match[1]))); echo $cell[$i][1][1] ?></td> <td><?php $cell[$i][1][2] = 0; echo $cell[$i][1][2] ?></td> <td><?php $cell[$i][1][3] = 0; echo $cell[$i][1][3] ?></td> </tr> <tr> <td><?php $cell[$i][2][0] = 0; echo $cell[$i][2][0] ?></td> <td><?php $cell[$i][2][1] = 0; echo $cell[$i][2][1] ?></td> <td><?php $cell[$i][2][2] = 1; echo $cell[$i][2][2] ?></td> <td><?php $cell[$i][2][3] = 0; echo $cell[$i][2][3] ?></td> </tr> <tr> <td><?php $cell[$i][3][0] = 0; echo $cell[$i][3][0] ?></td> <td><?php $cell[$i][3][1] = 0; echo $cell[$i][3][1] ?></td> <td><?php $cell[$i][3][2] = 0; echo $cell[$i][3][2] ?></td> <td><?php $cell[$i][3][3] = 1; echo $cell[$i][3][3] ?></td> </tr> </table> <?php } } } ?> <?php echo "<br/><br/><br/><br/><br/><br/><br/><br/><br/>"; $i=0; for($row = 0; $row < 4; $row++){ for($column = 0; $column < 4; $column++){ $sum = 0; for($ctr = 0; $ctr < 4; $ctr++){ $sum = $sum + ($cell[$i][$row][$ctr] * $cell[$i+1][$ctr][$column]); } $sol[$i][$row][$column] = $sum; } } ?> <table width="50" border="5" align = "left" cellpadding="5"> <tr> <td><?php echo $sol[$i][0][0] ?></td> <td><?php echo $sol[$i][0][1] ?></td> <td><?php echo $sol[$i][0][2] ?></td> <td><?php echo $sol[$i][0][3] ?></td> </tr> <tr> <td><?php echo $sol[$i][1][0] ?></td> <td><?php echo $sol[$i][1][1] ?></td> <td><?php echo $sol[$i][1][2] ?></td> <td><?php echo $sol[$i][1][3] ?></td> </tr> <tr> <td><?php echo $sol[$i][2][0] ?></td> <td><?php echo $sol[$i][2][1] ?></td> <td><?php echo $sol[$i][2][2] ?></td> <td><?php echo $sol[$i][2][3] ?></td> </tr> <tr> <td><?php echo $sol[$i][3][0] ?></td> <td><?php echo $sol[$i][3][1] ?></td> <td><?php echo $sol[$i][3][2] ?></td> <td><?php echo $sol[$i][3][3] ?></td> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/257559-4x4-matrix-solver-multiplication/#findComment-1320308 Share on other sites More sharing options...
kaitokid41 Posted February 23, 2012 Author Share Posted February 23, 2012 HI! I'm almost finished with my code. And I need one last help. I don't think I'm doing this correctly in the 2nd iteration when $i = 1? And how Do I get rid of the last (unnecessary and empty) matrix? THANKS! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Matrix Solver</title> </head> <body> <?php $transformation = $_POST['string']; $transbits = explode(";",$transformation); for($i=0; $i<count($transbits);$i++){ if(strcmp(substr($transbits[$i],0,5),"trans")==0){ preg_match('#\((.*?)\)#', $transbits[$i], $match); $extrans = explode(",",$match[1]); ?> <table width="50" border="5" align="left" cellpadding="5"> <tr> <td><?php $cell[$i][0][0] = 1; echo $cell[$i][0][0] ?></td> <td><?php $cell[$i][0][1] = 0; echo $cell[$i][0][1] ?></td> <td><?php $cell[$i][0][2] = 0; echo $cell[$i][0][2] ?></td> <td><?php $cell[$i][0][3] = $extrans[0]; echo $cell[$i][0][3] ?></td> </tr> <tr> <td><?php $cell[$i][1][0] = 0; echo $cell[$i][1][0] ?></td> <td><?php $cell[$i][1][1] = 1; echo $cell[$i][1][1] ?></td> <td><?php $cell[$i][1][2] = 0; echo $cell[$i][1][2] ?></td> <td><?php $cell[$i][1][3] = $extrans[1]; echo $cell[$i][1][3] ?></td> </tr> <tr> <td><?php $cell[$i][2][0] = 0; echo $cell[$i][2][0] ?></td> <td><?php $cell[$i][2][1] = 0; echo $cell[$i][2][1] ?></td> <td><?php $cell[$i][2][2] = 1; echo $cell[$i][2][2] ?></td> <td><?php $cell[$i][2][3] = $extrans[2]; echo $cell[$i][2][3] ?></td> </tr> <tr> <td><?php $cell[$i][3][0] = 0; echo $cell[$i][3][0] ?></td> <td><?php $cell[$i][3][1] = 0; echo $cell[$i][3][1] ?></td> <td><?php $cell[$i][3][2] = 0; echo $cell[$i][3][2] ?></td> <td><?php $cell[$i][3][3] = 1; echo $cell[$i][3][3] ?></td> </tr> </table> <?php } else if(strcmp(substr($transbits[$i],0,3),"rot")==0){ preg_match('#\((.*?)\)#', $transbits[$i], $match); //echo "$match[1] <br/>"; if (strcmp(substr($transbits[$i],3,1),"x")==0){ ?> <table width="50" border="5" align="left" cellpadding="5"> <tr> <td><?php $cell[$i][0][0] = 1; echo $cell[$i][0][0] ?></td> <td><?php $cell[$i][0][1] = 0; echo $cell[$i][0][1] ?></td> <td><?php $cell[$i][0][2] = 0; echo $cell[$i][0][2] ?></td> <td><?php $cell[$i][0][3] = 0; echo $cell[$i][0][3] ?></td> </tr> <tr> <td><?php $cell[$i][1][0] = 0; echo $cell[$i][1][0] ?></td> <td><?php $cell[$i][1][1] = intval(cos(deg2rad($match[1]))); echo $cell[$i][1][1] ?></td> <td><?php $cell[$i][1][2] = intval(-sin(deg2rad($match[1]))); echo $cell[$i][1][2] ?></td> <td><?php $cell[$i][1][3] = 0; echo $cell[$i][1][3] ?></td> </tr> <tr> <td><?php $cell[$i][2][0] = 0; echo $cell[$i][2][0] ?></td> <td><?php $cell[$i][2][1] = intval(sin(deg2rad($match[1]))); echo $cell[$i][2][1] ?></td> <td><?php $cell[$i][2][2] = intval(cos(deg2rad($match[1]))); echo $cell[$i][2][2] ?></td> <td><?php $cell[$i][2][3] = 0; echo $cell[$i][2][3] ?></td> </tr> <tr> <td><?php $cell[$i][3][0] = 0; echo $cell[$i][3][0] ?></td> <td><?php $cell[$i][3][1] = 0; echo $cell[$i][3][1] ?></td> <td><?php $cell[$i][3][2] = 0; echo $cell[$i][3][2] ?></td> <td><?php $cell[$i][3][3] = 1; echo $cell[$i][3][3] ?></td> </tr> </table> <?php } else if (strcmp(substr($transbits[$i],3,1),"y")==0){ ?> <table width="50" border="5" align="left" cellpadding="5"> <tr> <td><?php $cell[$i][0][0] = intval(cos(deg2rad($match[1]))); echo $cell[$i][0][0] ?></td> <td><?php $cell[$i][0][1] = 0; echo $cell[$i][0][1] ?></td> <td><?php $cell[$i][0][2] = intval(sin(deg2rad($match[1]))); echo $cell[$i][0][2] ?></td> <td><?php $cell[$i][0][3] = 0; echo $cell[$i][0][3] ?></td> </tr> <tr> <td><?php $cell[$i][1][0] = 0; echo $cell[$i][1][0] ?></td> <td><?php $cell[$i][1][1] = 1; echo $cell[$i][1][1] ?></td> <td><?php $cell[$i][1][2] = 0; echo $cell[$i][1][2] ?></td> <td><?php $cell[$i][1][3] = 0; echo $cell[$i][1][3] ?></td> </tr> <tr> <td><?php $cell[$i][2][0] = intval(-sin(deg2rad($match[1]))); echo $cell[$i][2][0] ?></td> <td><?php $cell[$i][2][1] = 0; echo $cell[$i][2][1] ?></td> <td><?php $cell[$i][2][2] = intval(cos(deg2rad($match[1]))); echo $cell[$i][2][2] ?></td> <td><?php $cell[$i][2][3] = 0; echo $cell[$i][2][3] ?></td> </tr> <tr> <td><?php $cell[$i][3][0] = 0; echo $cell[$i][3][0] ?></td> <td><?php $cell[$i][3][1] = 0; echo $cell[$i][3][1] ?></td> <td><?php $cell[$i][3][2] = 0; echo $cell[$i][3][2] ?></td> <td><?php $cell[$i][3][3] = 1; echo $cell[$i][3][3] ?></td> </tr> </table> <?php } else if (strcmp(substr($transbits[$i],3,1),"z")==0){ ?> <table width="50" border="5" align="left" cellpadding="5"> <tr> <td><?php $cell[$i][0][0] = intval(cos(deg2rad($match[1]))); echo $cell[$i][0][0] ?></td> <td><?php $cell[$i][0][1] = intval(-sin(deg2rad($match[1]))); echo $cell[$i][0][1] ?></td> <td><?php $cell[$i][0][2] = 0; echo $cell[$i][0][2] ?></td> <td><?php $cell[$i][0][3] = 0; echo $cell[$i][0][3] ?></td> </tr> <tr> <td><?php $cell[$i][1][0] = intval(sin(deg2rad($match[1]))); echo $cell[$i][1][0] ?></td> <td><?php $cell[$i][1][1] = intval(cos(deg2rad($match[1]))); echo $cell[$i][1][1] ?></td> <td><?php $cell[$i][1][2] = 0; echo $cell[$i][1][2] ?></td> <td><?php $cell[$i][1][3] = 0; echo $cell[$i][1][3] ?></td> </tr> <tr> <td><?php $cell[$i][2][0] = 0; echo $cell[$i][2][0] ?></td> <td><?php $cell[$i][2][1] = 0; echo $cell[$i][2][1] ?></td> <td><?php $cell[$i][2][2] = 1; echo $cell[$i][2][2] ?></td> <td><?php $cell[$i][2][3] = 0; echo $cell[$i][2][3] ?></td> </tr> <tr> <td><?php $cell[$i][3][0] = 0; echo $cell[$i][3][0] ?></td> <td><?php $cell[$i][3][1] = 0; echo $cell[$i][3][1] ?></td> <td><?php $cell[$i][3][2] = 0; echo $cell[$i][3][2] ?></td> <td><?php $cell[$i][3][3] = 1; echo $cell[$i][3][3] ?></td> </tr> </table> <?php } } } ?> <?php for($i=0; $i<count($transbits);$i++){ echo "<br/><br/><br/><br/><br/><br/><br/><br/><br/>"; for($row = 0; $row < 4; $row++){ for($column = 0; $column < 4; $column++){ $sum = 0; for($ctr = 0; $ctr < 4; $ctr++){ $sum = $sum + ($cell[$i][$row][$ctr] * $cell[$i+1][$ctr][$column]); } $cell[$i][$row][$column] = $sum; } } ?> <table width="50" border="5" align = "left" cellpadding="5"> <tr> <td><?php echo $cell[$i][0][0] ?></td> <td><?php echo $cell[$i][0][1] ?></td> <td><?php echo $cell[$i][0][2] ?></td> <td><?php echo $cell[$i][0][3] ?></td> </tr> <tr> <td><?php echo $cell[$i][1][0] ?></td> <td><?php echo $cell[$i][1][1] ?></td> <td><?php echo $cell[$i][1][2] ?></td> <td><?php echo $cell[$i][1][3] ?></td> </tr> <tr> <td><?php echo $cell[$i][2][0] ?></td> <td><?php echo $cell[$i][2][1] ?></td> <td><?php echo $cell[$i][2][2] ?></td> <td><?php echo $cell[$i][2][3] ?></td> </tr> <tr> <td><?php echo $cell[$i][3][0] ?></td> <td><?php echo $cell[$i][3][1] ?></td> <td><?php echo $cell[$i][3][2] ?></td> <td><?php echo $cell[$i][3][3] ?></td> </tr> </table> <?php for($j=$i+2; $j<(count($transbits));$j++){ ?> <table width="50" border="5" align = "left" cellpadding="5"> <tr> <td><?php echo $cell[$j][0][0] ?></td> <td><?php echo $cell[$j][0][1] ?></td> <td><?php echo $cell[$j][0][2] ?></td> <td><?php echo $cell[$j][0][3] ?></td> </tr> <tr> <td><?php echo $cell[$j][1][0] ?></td> <td><?php echo $cell[$j][1][1] ?></td> <td><?php echo $cell[$j][1][2] ?></td> <td><?php echo $cell[$j][1][3] ?></td> </tr> <tr> <td><?php echo $cell[$j][2][0] ?></td> <td><?php echo $cell[$j][2][1] ?></td> <td><?php echo $cell[$j][2][2] ?></td> <td><?php echo $cell[$j][2][3] ?></td> </tr> <tr> <td><?php echo $cell[$j][3][0] ?></td> <td><?php echo $cell[$j][3][1] ?></td> <td><?php echo $cell[$j][3][2] ?></td> <td><?php echo $cell[$j][3][3] ?></td> </tr> </table> <?php } } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/257559-4x4-matrix-solver-multiplication/#findComment-1320500 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.