PhpSnip.com

User Stats

Sergio

Calculation of a determinant of a matrix of the any order on the first line

Info

 Download  View Source (print view)
 Rating : 4.7  Views : 473

Source Code ( 25 lines )

function determinant($matrica) {
 $determinant=0;
 if (sizeof($matrica)==1)
 return $matrica[0][0];
 for ($i=0; $i<sizeof($matrica); $i++) {
  // We form a minor
  $row_index=0;
  for ($j=0; $j<sizeof($matrica); $j++) {
   $column_index=0;
   if($j!=0) {
    for ($k=0; $k<sizeof($matrica); $k++) {
     if ($k!=$i) {
      $minor[$i][$row_index][$column_index]=$matrica[$j][$k];
      $column_index++;
      }
     }
     $row_index++;
    }
   }
  // We form algebraic addition
  $algebraic_addition[$i]=pow(-1, (2+$i))*determinant($minor[$i]);
  $determinant+=$matrica[0][$i]*$algebraic_addition[$i];
 }
 return $determinant;
}

Search

Subscribe

  Rss Feeds

Sponsors

Advertise