# 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[[]]
R(w).polynomial().padded_list(5)
```

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()
P.padded_list(n)
```

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()
return P.padded_list(n)
```

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 ...
```

With respect to conversion to nonsymbolic series, see http://trac.sagemath.org/ticket/16203

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

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

Thanks tmonteil. But when I write p = taylor(g, g.variables()[0], 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()[0] , 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.