Determinant and permanent

From Rosetta Code
Revision as of 13:18, 29 June 2012 by rosettacode>CRGreathouse (draft task)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Determinant and permanent is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Given a matrix, return the determinant and the permanent of the matrix.

The determinant is given by

while the permanent is given by

In both cases the sum is over the permutations of the permutations of 1, 2, ..., n. (A permutation's sign is 1 if there are an even number of inversions and -1 otherwise; see parity of a permutation.)

More efficient algorithms for the determinant are known: LU decomposition, see for example wp:LU decomposition#Computing the determinant. Efficient methods for calculating the permanent are not known.

PARI/GP

The determinant is built in: <lang parigp>matdet(M)</lang> and the permanent can be defined as <lang parigp>matperm(M)=my(n=#M,t);sum(i=1,n!,t=numtoperm(n,i);prod(j=1,n,M[j,t[j]]))</lang>