firedrop84 Posted March 24, 2010 Share Posted March 24, 2010 Hi Everyone, I have a problem in regards the coding that I am trying to develop. I am trying to implement some concept which uses matrix but I didn't find any matrix function in php which does that. I done some calculation which is working fine and now I have two arrays which I need to multiplie the first and the second array and get a new array. I have to do it twice for a two dimminssional array and a single diminssioanl array. Can any help advice how this can be done. p.s. I already found this one site which has a class for it but it's quite complex. Any other site you know about it or any ideas you can give that might help http://www.phpclasses.org/browse/package/2859.html. Your advice is greatly approciated. Quote Link to comment https://forums.phpfreaks.com/topic/196332-matrix-issue/ Share on other sites More sharing options...
Daniel0 Posted March 24, 2010 Share Posted March 24, 2010 So what exactly do you need to do? Quote Link to comment https://forums.phpfreaks.com/topic/196332-matrix-issue/#findComment-1030942 Share on other sites More sharing options...
firedrop84 Posted March 24, 2010 Author Share Posted March 24, 2010 I am basically trying to know how can I multiple two matrix in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/196332-matrix-issue/#findComment-1031002 Share on other sites More sharing options...
Daniel0 Posted March 24, 2010 Share Posted March 24, 2010 Do you know how to do it using pen and paper? The naive O(n^3) algorithm is just three nested loops. Take a look at http://en.wikipedia.org/wiki/Matrix_multiplication Quote Link to comment https://forums.phpfreaks.com/topic/196332-matrix-issue/#findComment-1031041 Share on other sites More sharing options...
firedrop84 Posted March 25, 2010 Author Share Posted March 25, 2010 Thanks Daniel, your support is greatly approciated and I really need it. I am studying my masters but I am from Information System background not Computer Science which they gave us almost no math in uni. Matrix is a new concept for me. However, I asked a guy who graduated in math and he explained it for me. He doesn't have any IT knowledge. Based on what he said, I developed the sheet attached. The stuff I wrote in red in the attachment it's really just the index that it will be in the array. I thought if I have two arrays and each as two colomuns and two rows. Then, I thought maybe then I need to divide them into four arrays which they are C11, C12, C21, C22. You will see with the black pen is where I am getting the elements I need from each array. Then, I finish it by having a final array by adding the result into a final array. hmmm, nested for loop. Let me try to think how can I do it. to make my question clearler . This is a link of a thesis. Please look at page 294 - 299 which explains what I am trying to do. http://www.debii.curtin.edu.au/~ponny/doc/PhD_THESIS.pdf [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/196332-matrix-issue/#findComment-1031682 Share on other sites More sharing options...
Daniel0 Posted March 25, 2010 Share Posted March 25, 2010 Ah, well, it's pretty easy actually: <?php function matrixMult(array $m1, array $m2) { $m1rows = count($m1); $m1cols = count($m1[0]); $m2cols = count($m2[0]); for ($i = 0; $i < $m1rows; ++$i) { for ($j = 0; $j < $m2cols; ++$j) { for ($k = 0; $k < $m1cols; ++$k) { if (!isset($m3[$i])) $m3[$i] = array(); if (!isset($m3[$i][$j])) $m3[$i][$j] = 0; $m3[$i][$j] += $m1[$i][$k] * $m2[$k][$j]; } } } return $m3; } $m1 = array( array(1, 2), array(3, 4), array(5, 6), ); $m2 = array( array(1, 2, 3), array(4, 5, 6), ); print_r(matrixMult($m1, $m2)); Quote Link to comment https://forums.phpfreaks.com/topic/196332-matrix-issue/#findComment-1031691 Share on other sites More sharing options...
firedrop84 Posted March 26, 2010 Author Share Posted March 26, 2010 Thanks Daniel. Let me try it and I will let you know. Quote Link to comment https://forums.phpfreaks.com/topic/196332-matrix-issue/#findComment-1032202 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.