Advertisement
cdsatrian

protoype process

Sep 27th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.21 KB | None | 0 0
  1. <?php
  2. function proses($a,$b)
  3. {
  4.   $row=count($a);
  5.   $col=count($b[0]);
  6.   $com=count($b);
  7.   $r_value=array();
  8.   if(count($a[0])!=$com){throw new Exception('Incompatible matrixes');}
  9.   for ($i=0;$i<$row;$i++)
  10.   {
  11.     $r_value[$i]=0;
  12.     for ($j=0;$j<$col;$j++)
  13.     {
  14.       $result[$i][$j]=0;
  15.       for($k=0;$k<$com;$k++){
  16.         $result[$i][$j] += $a[$i][$k]*$b[$k][$j];
  17.       }
  18.       $r_value[$i]+=$result[$i][$j];
  19.     }
  20.   }
  21.   $max=max($r_value);
  22.   $div_max=array();
  23.   foreach($r_value as $r_v)
  24.   {
  25.       $div_max[]=$max/$r_v;
  26.   }
  27.   return array($result,$r_value,$div_max,$div_max);
  28. }  
  29. function show_matrix($m)
  30. {
  31.   $row=count($m);
  32.   $col=count($m[0]);
  33.   for($i=0;$i<$row;$i++)
  34.   {
  35.     if($col!=1)
  36.     {
  37.       for($j=0;$j<$col;$j++)
  38.       {
  39.         echo str_pad(round($m[$i][$j],5),12,' ',STR_PAD_BOTH);
  40.       }
  41.     }
  42.     else
  43.     {
  44.       echo str_pad(round($m[$i],5),12,' ',STR_PAD_BOTH);
  45.     }
  46.     echo "\n";
  47.   }
  48. }
  49. function iterasi($data,$limit=0.05)
  50. {
  51.   $matrix1=$matrix2=$data;
  52.   $count=0;
  53.   $dimension=count($data);
  54.   $max=$temp_max=  str_split(str_repeat('100',$dimension ),3);
  55.   $arr_limit=str_split(str_repeat("$limit", $dimension),strlen("$limit"));
  56.   do
  57.   {
  58.     $max=$temp_max;
  59.     list($matrix3,$r_value,$div_max,$temp_max)=proses($matrix1,$matrix2);
  60.     echo "iterasi ".++$count.":\n";
  61.     show_matrix($matrix3);
  62.     echo "\nrow_value\n";
  63.     show_matrix($r_value);
  64.     echo "\ndiv_max\n";
  65.     show_matrix($div_max);
  66.     $condition=true;
  67.     $delta=array();
  68.     for($i=0;$i<$dimension;++$i)
  69.     {
  70.       $delta[$i]=abs($max[$i]-$temp_max[$i]);
  71.       if($delta[$i]>$arr_limit[$i])
  72.       {
  73.           $condition=false;
  74.       }
  75.     }
  76.     echo "limit\n";
  77.     show_matrix($arr_limit);
  78.     echo "\nselisih\n";
  79.     show_matrix($delta);
  80.     echo "\n---------------------------\n";
  81.     $matrix1=$matrix3;
  82.     $matrix2=($count==1)?$matrix3:$matrix2;
  83.     $condition=$count<2?false:$condition;
  84.   }while(!$condition);
  85.   return array($count,$delta);
  86. }
  87. //==== contoh proses 1
  88. $data =array(
  89.   array(1,4,3),
  90.   array(4,1,2),
  91.   array(3,2,1)
  92. );
  93. $limit=0.5;
  94. echo "<hr><h3>Data</h3><pre>";
  95. show_matrix($data);
  96. echo "</pre><h3>Prosess</h3><pre>";
  97. list($count,$delta)=iterasi($data,$limit);
  98. echo "</pre><h3>Hasil Akhir</h3>Jumlah Iterasi: <b>$count</b>
  99.      <br>Limit :<b>$limit</b><br>Selisih akhir:<br><pre>";
  100. show_matrix($delta);
  101. echo "</pre><hr />";
  102. //==== contoh proses 2
  103. $data =array(
  104.   array(1,4,3),
  105.   array(4,1,2),
  106.   array(3,2,1)
  107. );
  108. $limit=0.05;
  109. echo "<h3>Data</h3><pre>";
  110. show_matrix($data);
  111. echo "</pre><h3>Prosess</h3><pre>";
  112. list($count,$delta)=iterasi($data,$limit);
  113. echo "</pre><h3>Hasil Akhir</h3>Jumlah Iterasi: <b>$count</b>
  114.      <br>Limit :<b>$limit</b><br>Selisih akhir:<br><pre>";
  115. show_matrix($delta);
  116. echo "</pre><hr />";
  117.  
  118. //==== contoh proses 3
  119. $data = array(
  120.   array(1,-0.52836,0),
  121.   array(-0.52836,1,-0.198446885),
  122.   array(0,-0.198446885,1)
  123. );
  124. $limit=0.05;
  125. echo "<h3>Data</h3><pre>";
  126. show_matrix($data);
  127. echo "</pre><h3>Prosess</h3><pre>";
  128. list($count,$delta)=iterasi($data,$limit);
  129. echo "</pre><h3>Hasil Akhir</h3>Jumlah Iterasi: <b>$count</b>
  130.      <br>Limit :<b>$limit</b><br>Selisih akhir:<br><pre>";
  131. show_matrix($delta);
  132. echo "</pre><hr/>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement