Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html>
- <head>
- <title>Invers Matrix</title>
- </head>
- <body>
- <?php
- //************* INPUT ORDO MATRIX ************//
- if(!isset($_POST['step']))
- {
- ?>
- <fieldset>
- <legend>Ordo Matrix</legend>
- <form method='post'>
- <input type='hidden' name='step' value='1'/>
- <div>
- <label>Jumlah</label>
- <select id='ordo' name='ordo'>
- <?php
- for($i=2;$i<=6;++$i){
- echo "<option value='$i'>$i</option>\n";
- }
- ?>
- </select>
- <input type='submit' name='step1' value='proses' />
- </div>
- </form>
- </fieldset>
- <?php
- }
- //************ INPUT DATA MATRIX **************//
- elseif(isset($_POST['step']) && $_POST['step']==1)
- {
- ?>
- <fieldset>
- <legend>Input Matrix</legend>
- <form method='post'>
- <input type='hidden' name='step' value='2'/>
- <div>
- <label>Masukkan nilai matrix</label>
- <table>
- <?php
- for($i=0;$i<$_POST['ordo'];++$i){
- echo "<tr>\n";
- for($j=0;$j<$_POST['ordo'];++$j){
- echo "<td><input type='text' name='data[{$i}][{$j}]' /></td>";
- }
- echo "</tr>\n";
- }
- ?>
- </table>
- <input type='submit' name='step2' value='proses' />
- </div>
- </form>
- </fieldset>
- <?php
- }
- //*********** SHOW RESULT MATRIX *************//
- elseif(isset($_POST['step']) && $_POST['step']==2)
- {
- function invert($a, $debug = FALSE)
- {
- $n = count($a);
- $identity = identity_matrix($n);
- for ($i = 0; $i < $n; ++ $i) {
- $a[$i] = array_merge($a[$i], $identity[$i]);
- }
- if ($debug) {
- echo "\nStarting matrix: ";
- print_matrix($a);
- }
- for ($j = 0; $j < $n-1; ++ $j) {
- // for all remaining rows (diagonally)
- for ($i = $j+1; $i < $n; ++ $i) {
- // adjust scale to pivot row
- // subtract pivot row from current
- $scalar = $a[$j][$j] / $a[$i][$j];
- for ($jj = $j; $jj < $n*2; ++ $jj) {
- $a[$i][$jj] *= $scalar;
- $a[$i][$jj] -= $a[$j][$jj];
- }
- }
- if ($debug) {
- echo "\nForward iteration $j: ";
- print_matrix($a);
- }
- }
- for ($j = $n-1; $j > 0; -- $j) {
- for ($i = $j-1; $i >= 0; -- $i) {
- $scalar = $a[$j][$j] / $a[$i][$j];
- for ($jj = $i; $jj < $n*2; ++ $jj) {
- $a[$i][$jj] *= $scalar;
- $a[$i][$jj] -= $a[$j][$jj];
- }
- }
- if ($debug) {
- echo "\nReverse iteration $j: ";
- print_matrix($a);
- }
- }
- for ($j = 0; $j < $n; ++ $j){
- if ($a[$j][$j] !== 1) {
- $scalar = 1 / $a[$j][$j];
- for ($jj = $j; $jj < $n*2; ++ $jj) {
- $a[$j][$jj] *= $scalar;
- //echo"<br>";
- }
- }
- if ($debug) {
- echo "\n1-out iteration $j: ";
- print_matrix($a);
- }
- }
- // take out the matrix inverse to return
- $invers = array();
- for ($i = 0; $i < $n; ++ $i) {
- $invers[$i] = array_slice($a[$i], $n);
- }
- return $invers;
- }
- function print_matrix($a, $decimals = 6)
- {
- foreach ($a as $row) {
- echo "\n\t[";
- foreach ($row as $i) {
- echo "\t" . sprintf("%01.{$decimals}f", round($i, $decimals));
- }
- echo "\t]";
- }
- }
- function identity_matrix($n)
- {
- $identity = array();
- for ($i = 0; $i < $n; ++$i) {
- for ($j = 0; $j < $n; ++$j) {
- $identity[$i][$j] = ($i == $j) ? 1 : 0;
- }
- }
- return $identity;
- }
- if(isset($_POST['data'])){
- $a=$_POST['data'];
- echo "\nMatrix:<pre>";
- print_matrix($a);
- echo "\n";
- $b = invert($a,1);
- echo "\nInversion result:<pre>";
- print_matrix($b);
- echo "</pre>\n<a href='matrix.php'>reset</a>";
- }
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement