Advertisement
cdsatrian

Invers Matrix

Oct 31st, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.89 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3.   <head>
  4.     <title>Invers Matrix</title>
  5.   </head>
  6.   <body>
  7. <?php
  8. //************* INPUT  ORDO MATRIX ************//
  9. if(!isset($_POST['step']))
  10. {
  11. ?>
  12.     <fieldset>
  13.       <legend>Ordo Matrix</legend>
  14.       <form method='post'>
  15.         <input type='hidden' name='step' value='1'/>
  16.         <div>
  17.           <label>Jumlah</label>
  18.           <select id='ordo' name='ordo'>
  19.             <?php
  20.             for($i=2;$i<=6;++$i){
  21.               echo "<option value='$i'>$i</option>\n";
  22.             }
  23.             ?>
  24.           </select>
  25.           <input type='submit' name='step1' value='proses' />
  26.         </div>
  27.       </form>
  28.     </fieldset>
  29. <?php
  30. }
  31. //************ INPUT DATA MATRIX **************//
  32. elseif(isset($_POST['step']) && $_POST['step']==1)
  33. {
  34. ?>
  35.     <fieldset>
  36.       <legend>Input Matrix</legend>
  37.       <form method='post'>
  38.         <input type='hidden' name='step' value='2'/>
  39.         <div>
  40.           <label>Masukkan nilai matrix</label>
  41.           <table>
  42.           <?php
  43.           for($i=0;$i<$_POST['ordo'];++$i){
  44.             echo "<tr>\n";
  45.             for($j=0;$j<$_POST['ordo'];++$j){
  46.               echo "<td><input type='text' name='data[{$i}][{$j}]' /></td>";
  47.             }
  48.             echo "</tr>\n";
  49.           }  
  50.           ?>  
  51.           </table>
  52.           <input type='submit' name='step2' value='proses' />
  53.         </div>
  54.       </form>
  55.     </fieldset>    
  56. <?php
  57. }
  58. //*********** SHOW RESULT MATRIX *************//
  59. elseif(isset($_POST['step']) && $_POST['step']==2)
  60. {
  61.   function invert($a, $debug = FALSE)
  62.   {
  63.     $n = count($a);
  64.     $identity = identity_matrix($n);
  65.     for ($i = 0; $i < $n; ++ $i) {
  66.       $a[$i] = array_merge($a[$i], $identity[$i]);
  67.     }
  68.     if ($debug) {
  69.       echo "\nStarting matrix: ";
  70.       print_matrix($a);
  71.     }
  72.     for ($j = 0; $j < $n-1; ++ $j) {
  73.       // for all remaining rows (diagonally)
  74.       for ($i = $j+1; $i < $n; ++ $i) {
  75.       // adjust scale to pivot row
  76.       // subtract pivot row from current
  77.         $scalar = $a[$j][$j] / $a[$i][$j];
  78.         for ($jj = $j; $jj < $n*2; ++ $jj) {
  79.           $a[$i][$jj] *= $scalar;
  80.           $a[$i][$jj] -= $a[$j][$jj];
  81.         }
  82.       }
  83.       if ($debug) {
  84.         echo "\nForward iteration $j: ";
  85.         print_matrix($a);
  86.       }
  87.     }
  88.     for ($j = $n-1; $j > 0; -- $j) {
  89.       for ($i = $j-1; $i >= 0; -- $i) {
  90.         $scalar = $a[$j][$j] / $a[$i][$j];
  91.         for ($jj = $i; $jj < $n*2; ++ $jj) {
  92.           $a[$i][$jj] *= $scalar;
  93.           $a[$i][$jj] -= $a[$j][$jj];
  94.         }
  95.       }
  96.       if ($debug) {
  97.         echo "\nReverse iteration $j: ";
  98.         print_matrix($a);
  99.       }
  100.     }
  101.     for ($j = 0; $j < $n; ++ $j){
  102.       if ($a[$j][$j] !== 1) {
  103.         $scalar = 1 / $a[$j][$j];
  104.         for ($jj = $j; $jj < $n*2; ++ $jj) {
  105.           $a[$j][$jj] *= $scalar;
  106.           //echo"<br>";
  107.         }
  108.       }
  109.       if ($debug) {
  110.         echo "\n1-out iteration $j: ";
  111.         print_matrix($a);
  112.       }
  113.     }
  114.     // take out the matrix inverse to return
  115.     $invers = array();
  116.     for ($i = 0; $i < $n; ++ $i) {
  117.       $invers[$i] = array_slice($a[$i], $n);
  118.     }
  119.     return $invers;
  120.   }
  121.  
  122.   function print_matrix($a, $decimals = 6)
  123.   {
  124.     foreach ($a as $row) {
  125.       echo "\n\t[";
  126.       foreach ($row as $i) {
  127.         echo "\t" . sprintf("%01.{$decimals}f", round($i, $decimals));
  128.       }
  129.       echo "\t]";
  130.     }
  131.   }
  132.  
  133.   function identity_matrix($n)
  134.   {
  135.     $identity = array();
  136.     for ($i = 0; $i < $n; ++$i) {
  137.       for ($j = 0; $j < $n; ++$j) {
  138.         $identity[$i][$j] = ($i == $j) ? 1 : 0;
  139.       }
  140.     }
  141.     return $identity;
  142.   }
  143.   if(isset($_POST['data'])){
  144.     $a=$_POST['data'];
  145.     echo "\nMatrix:<pre>";
  146.     print_matrix($a);
  147.     echo "\n";
  148.     $b = invert($a,1);
  149.     echo "\nInversion result:<pre>";
  150.     print_matrix($b);
  151.     echo "</pre>\n<a href='matrix.php'>reset</a>";
  152.   }
  153. }
  154. ?>
  155.   </body>
  156. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement