How to convert a Taylor polynomial to a power series? With Maple I can write

g := 2/(1+x+sqrt((1+x)*(1-3*x)));
t := taylor(g,x=0,6);
coeffs(convert(t,polynom));


end get

1, 1, 1, 3, 6


Trying to do the same with Sage I tried

var('x')
g = 2/(1+x+sqrt((1+x)*(1-3*x)))
taylor(g, x, 0, n)


and get

NotImplementedError
Wrong arguments passed to taylor. See taylor? for more details.


I could not find the details I am missing by typing 'taylor?'. Then I tried

g = 2/(1+x+sqrt((1+x)*(1-3*x)))
def T(g, n): return taylor(g, x, 0, n)
T(g, 5)


and got

6*x^5 + 3*x^4 + x^3 + x^2 + O(0) + 1


which is almost what I want (although I fail to understand this 'workaround').

But when I tried next to convert this Taylor polynomial to a power series

g = 2/(1+x+sqrt((1+x)*(1-3*x)))
def T(g, n): return taylor(g, x, 0, n)
w = T(g, 5)
R.<x> = QQ[[]]


I got the error

TypeError: unable to convert O(0) to a rational


The question: How can I convert the Taylor polynomial of 2/(1+x+sqrt((1+x)(1-3x))) to a power series and then extract the coefficients?

Solution ??: With the help of the answer of calc314 below (but note that I am not using 'series') the best solution so far seems to be:

var('x')
n = 5
g = 2/(1+x+sqrt((1+x)*(1-3*x)))
p = taylor(g, x, 0, n).truncate()
print p, p.parent()
x = PowerSeriesRing(QQ,'x').gen()
R.<x> = QQ[[]]
P = R(p)
print P, P.parent()


which gives

6*x^5 + 3*x^4 + x^3 + x^2 + 1  Symbolic Ring
1 + x^2 + x^3 + 3*x^4 + 6*x^5  Power Series Ring in x over Rational Field
[1, 0, 1, 1, 3]


Two minutes later I wanted to wrap things in a function, making 'n' and 'g' parameters.

def GF(g, n):
x = SR.var('x')
p = taylor(g, x, 0, n).truncate()
print p, p.parent()
x = PowerSeriesRing(QQ,'x').gen()
R.<x> = QQ[[]]
P = R(p)
print P, P.parent()


Now what do you think

gf = 2/(1+x+sqrt((1+x)*(1-3*x)))
print GF(gf, 5)


gives?

TypeError: unable to convert O(x^20) to a rational


Round 3, but only small progress:

tmonteil writes in his answer below: "the lines x = SR.var('x') and x = PowerSeriesRing(QQ,'x').gen() have no effect on the rest of the computation, and could be safely removed".

This does not work for me: if I do not keep the line x = SR.var('x') I get "UnboundLocalError: local variable 'x' referenced before assignment". But the line "x = PowerSeriesRing(QQ,'x').gen()" can be skipped. So I have now

def GF(g, n):
x = SR.var('x')
p = taylor(g, x, 0 ...
edit retag close merge delete

By the way I am using SageMathCloud which uses sage-6.3.beta6.

I updated my answer to make the use of g.variables() more explicit regarding your round 3.

Thanks tmonteil. But when I write p = taylor(g, g.variables(), 0, n).truncate() I get: 'sage.rings.power_series_poly.PowerSeries_poly' object has no attribute 'variables'. I give up now and think that rws in his comment above is right: there is a defect somewhere.

I do not see any defect. Please read my answer below for a detailed explanation. You got this answer because, at the time you type g.variables() , g is not a symbolic expression but a power series. You should understand that when you define g = 2/(1+x+sqrt((1+x)*(1-3*x))), the nature of g (symbolic expression, power series,...) depends on the nature of x (symbolic expression, power series,...) at the same time. Please do not hesitate to ask if something is still not clear.

Sort by » oldest newest most voted

Concerning your last question, it seems you are confused with the nature of x. You should notice that the "python variable" (a.k.a. "python name") x sometimes points to the "symbolic variable" (a.k.a. "symbol") x, and sometimes points to the power series x, which is of different nature.

If i open a fresh Sage session, i have no problem:

sage: def GF(g, n):
....:         x = SR.var('x')
....:         p = taylor(g, x, 0, n).truncate()
....:         print p, p.parent()
....:         x = PowerSeriesRing(QQ,'x').gen()
....:         R.<x> = QQ[[]]
....:         P = R(p)
....:         print P, P.parent()
sage: gf = 2/(1+x+sqrt((1+x)*(1-3*x)))
sage: print GF(gf, 5)
6*x^5 + 3*x^4 + x^3 + x^2 + 1 Symbolic Ring
1 + x^2 + x^3 + 3*x^4 + 6*x^5 Power Series Ring in x over Rational Field
[1, 0, 1, 1, 3]


The problem you encountered comes from the fact that, when you wrote R.<x> = QQ[[]] before this block, you redefined the python variable x as a particular power series. Hence, when you then wrote gf = 2/(1+x+sqrt((1+x)*(1-3*x))), you actually defined a power series, not a symbolic expression:

sage: R.<x> = QQ[[]]
sage: x.parent()
Power Series Ring in x over Rational Field
sage: gf = 2/(1+x+sqrt((1+x)*(1-3*x)))
sage: gf
1 + x^2 + x^3 + 3*x^4 + 6*x^5 + 15*x^6 + 36*x^7 + 91*x^8 + 232*x^9 + 603*x^10 + 1585*x^11 + 4213*x^12 + 11298*x^13 + 30537*x^14 + 83097*x^15 + 227475*x^16 + 625992*x^17 + 1730787*x^18 + 4805595*x^19 + O(x^20)
sage: gf.parent()
Power Series Ring in x over Rational Field


Also, note that, in the definition of your function GF, the lines x = SR.var('x') and x = PowerSeriesRing(QQ,'x').gen() have no effect on the rest of the computation, and could be safely removed:

In the first case, this will not redefine the nature of x in g, and if you need the name of the variable of g for the computation of the taylor expansion, you can use g.variables() as follows : p = taylor(g, g.variables(), 0, n).truncate() . This has the advantage to work even if g is an expression using the symbolic variable y (for example).

In the second case, when you write R.<x>=QQ[[]], this actually defines the python variable x as the generator of QQ[[]], making the preceding x = PowerSeriesRing(QQ,'x').gen() obsolete:

sage: x.parent()
Symbolic Ring
sage: R.<x> = QQ[[]]
sage: x.parent()
Power Series Ring in x over Rational Field


By the way, a simple way to get the coefficients of the taylor expansion, you can simply do:

sage: taylor(gf, x, 0, 5).coeffs()
[[1, 0], [1, 2], [1, 3], [3, 4], [6, 5]]

more

"If i open a fresh Sage session, i have no problem:" I opened a fresh session, executed the function version, and the problem was seemingly gone. Next I executed the non-function version and again the function version: the TypeError was back! "the lines x = SR.var('x') and x = PowerSeriesRing(QQ,'x').gen() have no effect on the rest of the computation, and could be safely removed:" OK, I did so only to learn: "UnboundLocalError: local variable 'x' referenced before assignment". Restart. And again the error.

"a simple way to get the coefficients of the taylor expansion, you can simply do:" Yes, I expected this (see my Maple version above). However the reason I want to use power series in this context can be seen in my last question (ask 24713). Sage's 'taylor' seems not to work with integrals and so I wanted to use the integral of the power series. Thanks for your explanations. I learned a lot.

In your first bit of code, specifying $n$ will make the command work without your workaround. That is, try:

var('x')
g = 2/(1+x+sqrt((1+x)*(1-3*x)))
T=taylor(g, x, 0, 5)
T


You can get a list of lists which each contain the coefficient and degree of the term by using

T.coeffs()


I'm using SageMathCloud and do not get the O(0) in the result. So, I'm not sure what's going on. I wonder if this is a difference in versions of Sage.

However, if I use the series command, I do get the order of the expansion included. But, this can be removed using truncate. For example,

S=T.series(x,5)
S.truncate()


I think this is what you are looking for.

more