%mathpiper,def="CoFactor"

Function("CoFactor",{matrix,ii,jj})
[
  Local(perms,indices,result);
  indices:=Table(i,i,1,Length(matrix),1);
  perms:=PermutationsList(indices);
  result:=0;
  ForEach(item,perms)
     If(item[ii] = jj,
       result:=result+
         Product(i,1,Length(matrix),
         If(ii=i,1,matrix[i][item[i] ])
                  )*LeviCivita(item));
  result;
];

%/mathpiper



%mathpiper_docs,name="CoFactor",categories="User Functions;Linear Algebra"
*CMD CoFactor --- cofactor of a matrix
*STD
*CALL
	CoFactor(M,i,j)

*PARMS

{M} -- a matrix

{i}, {j} - positive integers

*DESC

{CoFactor} returns the cofactor of a matrix around
the element ($i$, $j$). The cofactor is the minor times
$(-1)^(i+j)$.

*E.G.

In> A := {{1,2,3}, {4,5,6}, {7,8,9}};
Result: {{1,2,3},{4,5,6},{7,8,9}};
In> PrettyForm(A);
	
	/                    \
	| ( 1 ) ( 2 ) ( 3 )  |
	|                    |
	| ( 4 ) ( 5 ) ( 6 )  |
	|                    |
	| ( 7 ) ( 8 ) ( 9 )  |
	\                    /
Result: True;
In> CoFactor(A,1,2);
Result: 6;
In> Minor(A,1,2);
Result: -6;
In> Minor(A,1,2) * (-1)^(1+2);
Result: 6;

*SEE Minor, Determinant, Inverse
%/mathpiper_docs