{"id":907,"date":"2011-02-08T23:29:40","date_gmt":"2011-02-08T21:29:40","guid":{"rendered":"http:\/\/sickel.net\/blogg\/?p=907"},"modified":"2011-05-05T22:09:48","modified_gmt":"2011-05-05T20:09:48","slug":"php-matrix-multiplication","status":"publish","type":"post","link":"http:\/\/sickel.net\/blogg\/?p=907","title":{"rendered":"PHP Matrix multiplication"},"content":{"rendered":"<p>Needing to do some <a href=\"http:\/\/en.wikipedia.org\/wiki\/Matrix_multiplication\">matrix multiplication<\/a> in php, I found <a href=\"http:\/\/dada.perl.it\/shootout\/matrix_allsrc.html\">some code<\/a> that looked quite promising, but it had a couple of problems: In addition to the matrixes, it needed the max dimentions of the matrixes as arguments and it returned a matrix with these max dimentions, eg, I needed to multiply a 4&#215;4 with a 1&#215;4 matrix and got the answer as a 4&#215;4 matrix, although if I ignored all the zeros, the anwer was correct, I didn&#8217;t want to use that code, so go on:<\/p>\n<pre>\r\nfunction matrixmult($m1,$m2){\r\n\t$r=count($m1);\r\n\t$c=count($m2[0]);\r\n\t$p=count($m2);\r\n\tif(count($m1[0])!=$p){throw new Exception('Incompatible matrixes');}\r\n\t$m3=array();\r\n\tfor ($i=0;$i< $r;$i++){\r\n\t\tfor($j=0;$j<$c;$j++){\r\n\t\t\t$m3[$i][$j]=0;\r\n\t\t\tfor($k=0;$k<$p;$k++){\r\n\t\t\t\t$m3[$i][$j]+=$m1[$i][$k]*$m2[$k][$j];\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn($m3);\r\n}\r\n<\/pre>\n<p>It could have been made a bit more robust by adding some checks if all the subarrays were the same size, but I have so far not bothered to do that.<\/p>\n<p>And in addition, I'll throw in a matrix transposer for free:<\/p>\n<pre>\r\nfunction matrixtransp($m){\r\n\t$r=count($m);\r\n\t$c=count($m[0]);\r\n\t$mt=array();\r\n\tfor($i=0;$i< $r;$i++){\r\n\t\tfor($j=0;$j<$c;$j++){\r\n\t\t\t$mt[$j][$i]=$m[$i][$j];\r\n\t\t}\r\n\t}\r\n\treturn($mt);\r\n}\r\n<\/pre>\n<p>Again, I could do some checks to see that the matrix really is a matrix and not a random collection of arrays, but not now.<\/p>\n<p>Quite simple code, but I have not been able to find anything that works well anywhere.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Needing to do some matrix multiplication in php, I found some code that looked quite promising, but it had a couple of problems: In addition to the matrixes, it needed the max dimentions of the matrixes as arguments and it &hellip; <a href=\"http:\/\/sickel.net\/blogg\/?p=907\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[28],"tags":[],"class_list":["post-907","post","type-post","status-publish","format-standard","hentry","category-php"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pnVtD-eD","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=907"}],"version-history":[{"count":6,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/907\/revisions"}],"predecessor-version":[{"id":921,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/907\/revisions\/921"}],"wp:attachment":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=907"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}