[R] Permanent of a matrix
Mark Novak
mnovak at uchicago.edu
Thu May 31 23:47:31 CEST 2007
Hello all,
Does anyone know of a function for calculating the permanent of a matrix?
I have found only the function for calculating a matrix's determinant,
and my beginner attempts to figure out a working script (see below) have
failed. (That is, I don't get the same answer as when I calculate the
permanent in Maple or Mathematica.)
My understanding and experience is that it can be an exceedingly
difficult (memory intensive) calculation for matrices larger than ~24x24
(which also suggests that there is something wrong in the way I've coded
this) but I can't seem to figure out where my error lies; whether it's
in my code or in my understanding of how a matrix permanent is defined.
Thanks in advance
-Mark
perm<-function(A){
m<-dim(A)[1]
M1<-dim(0)
for (i in
1:nrow(A)){temp.M<-c(A)[i+0:(m-i)*(m+1)];M1<-c(M1,prod(temp.M))}
M2<-dim(0)
for (i in
2:nrow(A)){temp.M<-c(t(A))[i+0:(m-i)*(m+1)];M2<-c(M2,prod(temp.M))}
B<-A[nrow(A):1,]
M3<-dim(0)
for (i in
1:nrow(B)){temp.M<-c(B)[i+0:(m-i)*(m+1)];M3<-c(M3,prod(temp.M))}
M4<-dim(0)
for (i in
2:nrow(B)){temp.M<-c(t(B))[i+0:(m-i)*(m+1)];M4<-c(M4,prod(temp.M))}
Perm<-sum(M1,M2,M3,M4)
Perm
}
S<-4
A<-array(seq(1,S^2),dim=c(S,S))
perm(A)
--
~~~~~~~~~~~~~~~~~~~~~~~~~~
Dept. of Ecology & Evolution
1101 E. 57th St., U. of Chicago
Chicago, IL 60637
Office/Lab: 773-702-4815
Cell: 773-256-8645
Fax: 773-702-9740
http://home.uchicago.edu/~mnovak/ <http://home.uchicago.edu/%7Emnovak/>
~~~~~~~~~~~~~~~~~~~~~~~~~~
More information about the R-help
mailing list