[R] Elementary Symmetric Polynomials

Austin H. Jones joneah6 at wfu.edu
Wed Jul 22 00:10:56 CEST 2009


We are interested in obtaining an efficient function that for a given 
vector of length t will output a vector of length t+1 that contains the 
associated values of the elementary symmetric polynomials in t 
variables. Below is what we have at the moment, but it is a little slow 
for our needs. Any suggestions?

Thanks ahead of time for any help you can offer,

Austin H. Jones
Department of Mathematics
Wake Forest University



f<-function(v)
{
prodsub<-function(v,q){prod(v[q])}
t<-length(v)
C<-vector("list",t)
for (i in 1:t)
{C[[i]]<-combn(1:t,i)}
e<-rep(0,t)
for (i in 1:(t))
{
e[i]<-sum(apply(C[[i]],2,prodsub,v=v))
}
e<-c(1,e)
e
}

Examples:


 > f(c(1,2,3))
[1]  1  6 11  6


 > f(c(4,6,3,1,9))
[1]    1   23  193  729 1206  648




More information about the R-help mailing list