%mathpiper,def="Inverse"

Function("Inverse",{matrix})
[
  Local(perms,indices,inv,det,n);
  n:=Length(matrix);
  indices:=Table(i,i,1,n,1);
  perms:=PermutationsList(indices);
  inv:=ZeroMatrix(n,n);
  det:=0;
  ForEach(item,perms)
  [
    Local(i,lc);
    lc := LeviCivita(item);
    det:=det+Product(i,1,n,matrix[i][item[i] ])* lc;
    For(i:=1,i<=n,i++)
        [
         inv[item[i] ][i] := inv[item[i] ][i]+
           Product(j,1,n,
             If(j=i,1,matrix[j][item[j] ]))*lc;
        ];
  ];
  Check(det != 0, "Math", "Zero determinant");
  (1/det)*inv;
];

%/mathpiper



%mathpiper_docs,name="Inverse",categories="User Functions;Linear Algebra"
*CMD Inverse --- get inverse of a matrix
*STD
*CALL
	Inverse(M)

*PARMS

{M} -- a matrix

*DESC

Inverse returns the inverse of matrix $M$. The determinant of $M$ should
be non-zero. Because this function uses {Determinant} for calculating
the inverse of a matrix, you can supply matrices with non-numeric (symbolic)
matrix elements.

*E.G.

In> A:=DiagonalMatrix({a,b,c})
Result: {{a,0,0},{0,b,0},{0,0,c}};
In> B:=Inverse(A)
Result: {{(b*c)/(a*b*c),0,0},{0,(a*c)/(a*b*c),0},
	{0,0,(a*b)/(a*b*c)}};
In> Simplify(B)
Result: {{1/a,0,0},{0,1/b,0},{0,0,1/c}};

*SEE Determinant
%/mathpiper_docs