Peter Yang
2004-Dec-03 19:56 UTC
[R] how can I get the coefficients of x^0, x^1, x^2, . , x^6 from expansion of (1+x+x^2)^3
Hi, I would like to get the coefficients of x^0, x^1, x^2, . , x^6 from expansion of (1+x+x^2)^3. The result should be 1, 3, 6, 7, 6, 3, 1; How can I calculate in R? You help will be greatly appreciated. Peter [[alternative HTML version deleted]]
james.holtman@convergys.com
2004-Dec-03 20:43 UTC
[R] how can I get the coefficients of x^0, x^1, x^2, . , x^6 from expansion of (1+x+x^2)^3
Use the 'polynom' library:> p <- as.polynomial(c(1,1,1)) > p1 + x + x^2> p^31 + 3*x + 6*x^2 + 7*x^3 + 6*x^4 + 3*x^5 + x^6 > unclass(p^3) [1] 1 3 6 7 6 3 1>__________________________________________________________ James Holtman "What is the problem you are trying to solve?" Executive Technical Consultant -- Office of Technology, Convergys james.holtman at convergys.com +1 (513) 723-2929 "Peter Yang" <peterwyang at gmail.com To: <r-help at stat.math.ethz.ch> > cc: Sent by: Subject: [R] how can I get the coefficients of x^0, x^1, x^2, . , x^6 from r-help-bounces at stat.m expansion of (1+x+x^2)^3 ath.ethz.ch 12/03/2004 14:56 Hi, I would like to get the coefficients of x^0, x^1, x^2, . , x^6 from expansion of (1+x+x^2)^3. The result should be 1, 3, 6, 7, 6, 3, 1; How can I calculate in R? You help will be greatly appreciated. Peter [[alternative HTML version deleted]] ______________________________________________ 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
2004-Dec-04 02:08 UTC
[R] how can I get the coefficients of x^0, x^1, x^2, . , x^6 from expansion of (1+x+x^2)^3
Have you considered library(polynom)? If you don't already have
it but have R, install.packages("polynom") should get it.
hope this helps. spencer graves
Peter Yang wrote:
>Hi,
>
>
>
>I would like to get the coefficients of x^0, x^1, x^2, . , x^6 from
>expansion of (1+x+x^2)^3.
>
>The result should be 1, 3, 6, 7, 6, 3, 1;
>
>
>
>How can I calculate in R?
>
>
>
>You help will be greatly appreciated.
>
>
>
>Peter
>
>
>
>
>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>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
Gabor Grothendieck
2004-Dec-04 02:24 UTC
[R] how can I get the coefficients of x^0, x^1, x^2, . , x^6 from expansion of (1+x+x^2)^3
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)
Gabor Grothendieck
2004-Dec-04 02:27 UTC
[R] how can I get the coefficients of x^0, x^1, x^2, . , x^6 from expansion of (1+x+x^2)^3
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)
Spencer Graves
2004-Dec-04 03:01 UTC
[R] how can I get the coefficients of x^0, x^1, x^2, . , x^6 from expansion of (1+x+x^2)^3
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