# Revision history [back]

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 gf, and if you need the name of the variable of gf for the computation of the taylor expansion, you can use sage: gf.variables()[0].

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]]


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 gfg, and if you need the name of the variable of gfg for the computation of the taylor expansion, you can use sage: gf.variables()[0].g.variables()[0] as follows : p = taylor(g, g.variables()[0], 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]]