%mathpiper,def="RecursiveDeterminant"

/* Recursive calculation of determinant, provided by Sebastian Ferraro
 */
20 # RecursiveDeterminant(_matrix) <--
[
  /*
  
  */
  Local(result);
  If(IsEqual(Length(matrix),1),matrix[1][1],[
    result:=0;
    ForEach(i,1 .. Length(matrix))
      //Consider only non-zero entries
      If(Not(IsEqual(matrix[1][i],0)),
        //Transpose and Drop eliminate row 1, column i
        result:=result+matrix[1][i]*(-1)^(i+1)* RecursiveDeterminant(Transpose(Drop(Transpose(Drop(matrix,{1,1})),{i,i}))));
    result;
  ]);
];

%/mathpiper




%mathpiper_docs,name="RecursiveDeterminant",categories="User Functions;Linear Algebra"
*CMD RecursiveDeterminant --- computes a determinant recursively
*STD
*CALL
	RecursiveDeterminant(matrix)

*PARMS
{matrix} -- a matrix
*DESC
Computes a determinant recursively by summing the product of each (nonzero) element 
on the first row of the matrix by +/- the determinant of the submatrix with the 
corresponding row and column deleted.

*E.G.
todo
%/mathpiper_docs

    %output,preserve="false"
      
.   %/output

