For a given matrix, return the determinant and the permanent of the matrix.

The determinant is given by

 



det
(
A
)
=

∑

σ


sgn
⁡
(
σ
)

∏

i
=
1


n



M

i
,

σ

i






{\displaystyle \det(A)=\sum _{\sigma }\operatorname {sgn}(\sigma )\prod _{i=1}^{n}M_{i,\sigma _{i}}}


while the permanent is given by

 



perm
⁡
(
A
)
=

∑

σ



∏

i
=
1


n



M

i
,

σ

i






{\displaystyle \operatorname {perm} (A)=\sum _{\sigma }\prod _{i=1}^{n}M_{i,\sigma _{i}}}


In both cases the sum is over the permutations 



σ


{\displaystyle \sigma }

 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.



Related task

 Permutations by swapping


 
function det-perm ($array) {
    if($array) {
        $size = $array.Count
        function prod($A) {
            $prod = 1
            if($A) { $A | foreach{$prod *= $_} }
            $prod
        }
        function generate($sign, $n, $A) {
            if($n -eq 1) {
                $i = 0               
                $prod = prod @($A | foreach{$array[$i++][$_]})
                [pscustomobject]@{det = $sign*$prod; perm = $prod}
            }
            else{
                for($i = 0; $i -lt ($n - 1); $i += 1) {
                    generate $sign ($n - 1) $A
                    if($n % 2 -eq 0){
                        $i1, $i2 = $i, ($n-1)
                        $A[$i1], $A[$i2] = $A[$i2], $A[$i1]
                    }
                    else{
                        $i1, $i2 = 0, ($n-1)
                        $A[$i1], $A[$i2] = $A[$i2], $A[$i1]
                    }
                    $sign *= -1
                }
                generate $sign ($n - 1) $A
            }
        }
        $det = $perm = 0
        generate 1 $size @(0..($size-1)) | foreach{
            $det += $_.det
            $perm += $_.perm
        }
        [pscustomobject]@{det =  "$det"; perm = "$perm"}
    } else {Write-Error "empty array"}
}
det-perm 5
det-perm @(@(1,0,0),@(0,1,0),@(0,0,1))
det-perm @(@(0,0,1),@(0,1,0),@(1,0,0))
det-perm @(@(4,3),@(2,5))
det-perm @(@(2,5),@(4,3))
det-perm @(@(4,4),@(2,2))
 