Determinant and permanent: Difference between revisions

+Stata
(+Stata)
Line 2,123:
 
[http://fricas.github.io/api/Matrix.html?highlight=matrix Domain:Matrix(R)]
 
=={{header|Stata}}==
 
Two auxiliary functions: '''range1(n,i)''' returns the column vector with numbers 1 to n except i is removed.
And '''submat(a,i,j)''' returns matrix a with row i and column j removed.
For x=-1, the main function '''sumrec(a,x)''' computes the determinant of a by developping the determinant along the first column. For x=1, one gets the permanent.
 
<lang>real vector range1(real scalar n, real scalar i) {
if (i < 1 | i > n) {
return(1::n)
} else if (i == 1) {
return(2::n)
} else if (i == n) {
return(1::n-1)
} else {
return(1::i-1\i+1::n)
}
}
 
real matrix submat(real matrix a, real scalar i, real scalar j) {
return(a[range1(rows(a), i), range1(cols(a), j)])
}
 
real scalar sumrec(real matrix a, real scalar x) {
real scalar n, s, p
n = rows(a)
if (n==1) return(a[1,1])
s = 0
p = 1
for (i=1; i<=n; i++) {
s = s+p*a[i,1]*sumrec(submat(a, i, 1), x)
p = p*x
}
return(s)
}</lang>
 
Example:
 
<lang stata>: a=1,1,1,0\1,1,0,1\1,0,1,1\0,1,1,1
: a
[symmetric]
1 2 3 4
+-----------------+
1 | 1 |
2 | 1 1 |
3 | 1 0 1 |
4 | 0 1 1 1 |
+-----------------+
 
: det(a)
-3
 
: sumrec(a,-1)
-3
 
: sumrec(a,1)
9</lang>
 
=={{header|Tcl}}==
1,336

edits