Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function proses($a,$b)
- {
- $row=count($a);
- $col=count($b[0]);
- $com=count($b);
- $r_value=array();
- if(count($a[0])!=$com){throw new Exception('Incompatible matrixes');}
- for ($i=0;$i<$row;$i++)
- {
- $r_value[$i]=0;
- for ($j=0;$j<$col;$j++)
- {
- $result[$i][$j]=0;
- for($k=0;$k<$com;$k++){
- $result[$i][$j] += $a[$i][$k]*$b[$k][$j];
- }
- $r_value[$i]+=$result[$i][$j];
- }
- }
- $max=max($r_value);
- $div_max=array();
- foreach($r_value as $r_v)
- {
- $div_max[]=$max/$r_v;
- }
- return array($result,$r_value,$div_max,$div_max);
- }
- function show_matrix($m)
- {
- $row=count($m);
- $col=count($m[0]);
- for($i=0;$i<$row;$i++)
- {
- if($col!=1)
- {
- for($j=0;$j<$col;$j++)
- {
- echo str_pad(round($m[$i][$j],5),12,' ',STR_PAD_BOTH);
- }
- }
- else
- {
- echo str_pad(round($m[$i],5),12,' ',STR_PAD_BOTH);
- }
- echo "\n";
- }
- }
- function iterasi($data,$limit=0.05)
- {
- $matrix1=$matrix2=$data;
- $count=0;
- $dimension=count($data);
- $max=$temp_max= str_split(str_repeat('100',$dimension ),3);
- $arr_limit=str_split(str_repeat("$limit", $dimension),strlen("$limit"));
- do
- {
- $max=$temp_max;
- list($matrix3,$r_value,$div_max,$temp_max)=proses($matrix1,$matrix2);
- echo "iterasi ".++$count.":\n";
- show_matrix($matrix3);
- echo "\nrow_value\n";
- show_matrix($r_value);
- echo "\ndiv_max\n";
- show_matrix($div_max);
- $condition=true;
- $delta=array();
- for($i=0;$i<$dimension;++$i)
- {
- $delta[$i]=abs($max[$i]-$temp_max[$i]);
- if($delta[$i]>$arr_limit[$i])
- {
- $condition=false;
- }
- }
- echo "limit\n";
- show_matrix($arr_limit);
- echo "\nselisih\n";
- show_matrix($delta);
- echo "\n---------------------------\n";
- $matrix1=$matrix3;
- $matrix2=($count==1)?$matrix3:$matrix2;
- $condition=$count<2?false:$condition;
- }while(!$condition);
- return array($count,$delta);
- }
- //==== contoh proses 1
- $data =array(
- array(1,4,3),
- array(4,1,2),
- array(3,2,1)
- );
- $limit=0.5;
- echo "<hr><h3>Data</h3><pre>";
- show_matrix($data);
- echo "</pre><h3>Prosess</h3><pre>";
- list($count,$delta)=iterasi($data,$limit);
- echo "</pre><h3>Hasil Akhir</h3>Jumlah Iterasi: <b>$count</b>
- <br>Limit :<b>$limit</b><br>Selisih akhir:<br><pre>";
- show_matrix($delta);
- echo "</pre><hr />";
- //==== contoh proses 2
- $data =array(
- array(1,4,3),
- array(4,1,2),
- array(3,2,1)
- );
- $limit=0.05;
- echo "<h3>Data</h3><pre>";
- show_matrix($data);
- echo "</pre><h3>Prosess</h3><pre>";
- list($count,$delta)=iterasi($data,$limit);
- echo "</pre><h3>Hasil Akhir</h3>Jumlah Iterasi: <b>$count</b>
- <br>Limit :<b>$limit</b><br>Selisih akhir:<br><pre>";
- show_matrix($delta);
- echo "</pre><hr />";
- //==== contoh proses 3
- $data = array(
- array(1,-0.52836,0),
- array(-0.52836,1,-0.198446885),
- array(0,-0.198446885,1)
- );
- $limit=0.05;
- echo "<h3>Data</h3><pre>";
- show_matrix($data);
- echo "</pre><h3>Prosess</h3><pre>";
- list($count,$delta)=iterasi($data,$limit);
- echo "</pre><h3>Hasil Akhir</h3>Jumlah Iterasi: <b>$count</b>
- <br>Limit :<b>$limit</b><br>Selisih akhir:<br><pre>";
- show_matrix($delta);
- echo "</pre><hr/>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement