Determinant and permanent: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 33: Line 33:
The permanent does not have this problem in this example (the matrix contains no negative values and permanent does not use subtraction):
The permanent does not have this problem in this example (the matrix contains no negative values and permanent does not use subtraction):


<lang J> +/ .* i. 5 5x
<lang J> +/ .* i. 5 5
6778800</lang>
6778800</lang>



Revision as of 15:04, 29 June 2012

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.

J

Given the example matrix:

<lang J> i. 5 5

0  1  2  3  4
5  6  7  8  9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24</lang>

It's determinant is 0. When we use IEEE floating point, we only get an approximation of this result:

<lang J> -/ .* i. 5 5 _1.30277e_44</lang>

If we use exact (rational) arithmetic, we get a precise result:

<lang J> -/ .* i. 5 5x 0</lang>

The permanent does not have this problem in this example (the matrix contains no negative values and permanent does not use subtraction):

<lang J> +/ .* i. 5 5 6778800</lang>

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>