[R] Coefficients of a factorized polynomial
Serguei Kaniovski
kaniovsk at wsr.ac.at
Mon Oct 9 08:42:42 CEST 2006
Hi all,
starting from a vector "v[1:n]" I would like to compute the coefficients
of the polynomial (1+x^v[1])*(1+x^v[2])*...*(1+x^v[n]). The following
code works but is extremely slow for a large "n" due to, I believe, the
polynomial being factorized. I wanted to try the package "polynom"
command "unclass" but I could not figure out how to input a factorized
polynomial. Can you please help?
Serguei
v<-c(1,1,2,3,4,6)
e<-parse(text=paste("(1+x^", v, sep="", collapse="*", ")"))
DD<-function(expr, name, order = 0) {
if(order == 0)expr
else DD(D(expr, name), name, order - 1)
}
sapply(0:18, function(i) eval(DD(e, "x", i), list(x=0)))/factorial(0:18)
More information about the R-help
mailing list