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

Spencer Graves spencer.graves at pdf.com
Sat Dec 4 04:01:57 CET 2004


      Alternatively, how about the following: 

library(polynom)
coefficients(polynomial(c(1,1,1))^3)
[1] 1 3 6 7 6 3 1

      hope this helps.  spencer graves

Gabor Grothendieck wrote:

> 
>
>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)
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>  
>

-- 
Spencer Graves, PhD, Senior Development Engineer
O:  (408)938-4420;  mobile:  (408)655-4567




More information about the R-help mailing list