How's this? f2 <- function(v) { n <- length(v) s <- matrix(0,n,n) for(i in 1:n) for(j in 1:i) s[j,i] <- ifelse(i>1, s[j,i-1]+v[i]*ifelse(j>1, s[j-1,i-1], 1), v[i]) c(1,s[,n]) }
> system.time(f2.result <- f2(1:20)) user system elapsed 0 0 0 > system.time(f.result <- f(1:20)) user system elapsed 29.50 0.04 29.58 > all.equal(f.result, f2.result) [1] TRUE -- David Reiner -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Austin H. Jones Sent: Tuesday, July 21, 2009 5:11 PM To: r-help@r-project.org Subject: [R] Elementary Symmetric Polynomials 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 ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. This e-mail and any materials attached hereto, including, without limitation, all content hereof and thereof (collectively, "Rho Content") are confidential and proprietary to Rho Trading Securities, LLC ("Rho") and/or its affiliates, and are protected by intellectual property laws. Without the prior written consent of Rho, the Rho Content may not (i) be disclosed to any third party or (ii) be reproduced or otherwise used by anyone other than current employees of Rho or its affiliates, on behalf of Rho or its affiliates. THE RHO CONTENT IS PROVIDED AS IS, WITHOUT REPRESENTATIONS OR WARRANTIES OF ANY KIND. TO THE MAXIMUM EXTENT PERMISSIBLE UNDER APPLICABLE LAW, RHO HEREBY DISCLAIMS ANY AND ALL WARRANTIES, EXPRESS AND IMPLIED, RELATING TO THE RHO CONTENT, AND NEITHER RHO NOR ANY OF ITS AFFILIATES SHALL IN ANY EVENT BE LIABLE FOR ANY DAMAGES OF ANY NATURE WHATSOEVER, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, CONSEQUENTIAL, SPECIAL AND PUNITIVE DAMAGES, LOSS OF PROFITS AND TRADING LOSSES, RESULTING FROM ANY PERSON'S USE OR RELIANCE UPON, OR INABILITY TO USE, ANY RHO CONTENT, EVEN IF RHO IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES OR IF SUCH DAMAGES WERE FORESEEABLE. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.