Advertisement
cdsatrian

matrix multiplication

Sep 24th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | None | 0 0
  1. <?php
  2. //----------------------------
  3. //-- matrix multiplication
  4. //-- 120925 cahyadsn@yahoo.com
  5. //-----------------------------
  6. $a = array(array(2,1),array(4,5),array(6,7));
  7. print_matrix($a,$title='MATRIK A');
  8. $b = array(array(2,3,2),array(1,1,-3));
  9. print_matrix($b,$title='MATRIK B');
  10. if(count($a[0])==count($b)){
  11.   for($i=0;$i<count($a);$i++){
  12.     for($j=0;$j<count($b[0]);$j++){
  13.       $c[$i][$j]=0;
  14.       for($k=0;$k<count($b);$k++){
  15.         $c[$i][$j]+=$a[$i][$k]*$b[$k][$j];
  16.       }
  17.     }
  18.   }
  19.   print_matrix($c,$title='MATRIK C (AB)');
  20. }else{
  21.   echo "jumlah kolom matrik pertama != jumlah baris matrik kedua";
  22. }
  23.  
  24. function print_matrix($mat,$title=''){
  25.   echo "<pre>$title=\n";
  26.   for($i=0;$i<count($mat);$i++){
  27.     for($j=0;$j<count($mat[0]);$j++){
  28.       echo str_pad($mat[$i][$j],5,' ',STR_PAD_BOTH);
  29.     }
  30.     echo "\n";
  31.   }
  32.   echo "</pre>";
  33. }
  34. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement