[R] how can I get the coefficients of x^0, x^1, x^2, . , x^6 from expansion of (1+x+x^2)^3

Gabor Grothendieck ggrothendieck at myway.com
Sat Dec 4 03:27:07 CET 2004


 

From: Peter Yang <peterwyang at gmail.com>

>I would like to get the coefficients of x^0, x^1, x^2, . , x^6 from
>expansion of (1+x+x^2)^3.


# modification of DD in example(D) to support 0th derivative
DD <- function(expr,name, order = 0) {
if(order == 0) 
     expr
else DD(D(expr, name), name, order - 1)
}

# take symbolic derivatives, evaluate at 0 and divide by factorial n
sapply(0:6, function(i) eval(DD(e,"x",i),list(x=0)))/factorial(0:6)


By the way, e in the above is your expression, in this case:

 e <- expression((1+x+x^2)^3)




More information about the R-help mailing list