jhylands

Det(A)

Mar 8th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1. <?php
  2. $A[0][0] = 1;
  3. $A[0][1] = 2;
  4. $A[0][2] = 4;
  5. $A[0][3] = 6;
  6. $A[1][0] = 3;
  7. $A[1][1] = 2;
  8. $A[1][2] = 1;
  9. $A[1][3] = 4;
  10. $A[2][0] = -1;
  11. $A[2][1] = 4;
  12. $A[2][2] = 3;
  13. $A[2][3] = -5;
  14. $A[3][0] = -2;
  15. $A[3][1] = -4;
  16. $A[3][2] = -3;
  17. $A[3][3] = 5;
  18.  
  19.  
  20.  
  21. echo "|A|=" .  det($A);
  22. function det($A){
  23.     //echo "|A|" . count($A);
  24.     //if $A is a 1x1 matrix return the number formatted as a number
  25.     if(count($A)==1){
  26.         return $A[0][0];
  27.     }else{
  28.         //value will hold the adding up of each crossing out thing
  29.         $value = 0;
  30.         //polarity will be switched to follow...
  31. /*
  32. +-+
  33. -+-
  34. +-+
  35. */
  36.         $polarity = 1;
  37.         for($i=0;$i<count($A);$i++){
  38.             //echo "i:" . $i . "<br />";
  39.             //create the sub matrix
  40.             $y=0;
  41.             for($n=0;$n<count($A);$n++){
  42.             //not the current column (its been crossed out)
  43.             if($n!=$i){
  44.                 //start at 1 because row 0 has been crossed out
  45.                 for($m=1;$m<count($A[$n]);$m++){
  46.                     //if the array B is defined write to it otherwise create it and write to it
  47.                     $B[$y][$m-1] = $A[$n][$m];
  48.                 }
  49.                 //echo "y:" . $y . "<br />";
  50.                 $y++;
  51.             }
  52.             }
  53.             $value = $value + $polarity * $A[$i][0] * det($B);
  54.             $polarity = $polarity * -1;
  55.         }
  56.         return $value;
  57.     }
  58. }
  59. ?>
Advertisement
Add Comment
Please, Sign In to add comment