Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $A[0][0] = 1;
- $A[0][1] = 2;
- $A[0][2] = 4;
- $A[0][3] = 6;
- $A[1][0] = 3;
- $A[1][1] = 2;
- $A[1][2] = 1;
- $A[1][3] = 4;
- $A[2][0] = -1;
- $A[2][1] = 4;
- $A[2][2] = 3;
- $A[2][3] = -5;
- $A[3][0] = -2;
- $A[3][1] = -4;
- $A[3][2] = -3;
- $A[3][3] = 5;
- echo "|A|=" . det($A);
- function det($A){
- //echo "|A|" . count($A);
- //if $A is a 1x1 matrix return the number formatted as a number
- if(count($A)==1){
- return $A[0][0];
- }else{
- //value will hold the adding up of each crossing out thing
- $value = 0;
- //polarity will be switched to follow...
- /*
- +-+
- -+-
- +-+
- */
- $polarity = 1;
- for($i=0;$i<count($A);$i++){
- //echo "i:" . $i . "<br />";
- //create the sub matrix
- $y=0;
- for($n=0;$n<count($A);$n++){
- //not the current column (its been crossed out)
- if($n!=$i){
- //start at 1 because row 0 has been crossed out
- for($m=1;$m<count($A[$n]);$m++){
- //if the array B is defined write to it otherwise create it and write to it
- $B[$y][$m-1] = $A[$n][$m];
- }
- //echo "y:" . $y . "<br />";
- $y++;
- }
- }
- $value = $value + $polarity * $A[$i][0] * det($B);
- $polarity = $polarity * -1;
- }
- return $value;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment