Ask Your Question
0

How to convert a Taylor polynomial to a power series?

asked 2014-11-04 13:09:26 +0100

Peter Luschny gravatar image

updated 2015-10-16 19:15:30 +0100

FrédéricC gravatar image

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 ...
(more)
edit retag flag offensive close merge delete

Comments

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

rws gravatar imagerws ( 2014-11-04 16:36:29 +0100 )edit

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

Peter Luschny gravatar imagePeter Luschny ( 2014-11-04 18:06:01 +0100 )edit

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

tmonteil gravatar imagetmonteil ( 2014-11-05 13:31:54 +0100 )edit

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.

Peter Luschny gravatar imagePeter Luschny ( 2014-11-06 22:06:46 +0100 )edit

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.

tmonteil gravatar imagetmonteil ( 2014-11-10 13:45:25 +0100 )edit

2 Answers

Sort by » oldest newest most voted
4

answered 2014-11-04 20:35:09 +0100

tmonteil gravatar image

updated 2014-11-05 13:30:23 +0100

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()
....:         return P.padded_list(n)
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()[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]]
edit flag offensive delete link more

Comments

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

Peter Luschny gravatar imagePeter Luschny ( 2014-11-04 23:20:52 +0100 )edit

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

Peter Luschny gravatar imagePeter Luschny ( 2014-11-04 23:21:27 +0100 )edit
2

answered 2014-11-04 14:19:44 +0100

calc314 gravatar image

updated 2014-11-04 14:25:26 +0100

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-11-04 13:09:26 +0100

Seen: 2,034 times

Last updated: Nov 05 '14