Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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?

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]

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?

SolutionSolution ??: 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

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, n).truncate()
    print p, p.parent()
    R.<x> = QQ[[]]
    P = R(p)
    print P, P.parent()
    return P.padded_list()

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

If I open a fresh Sage session, I have, like tmontile, no problem. However I cannot always open a fresh Sage session when I want to execute GF(gf, n). Therefore consider

n = 7
gf = 2/(1+x+sqrt((1+x)*(1-3*x)))
print GF(gf, n)
p = taylor(gf, x, 0, n).truncate()
print p, p.parent()
R.<x> = QQ[[]]
P = R(p)
print P, P.parent()
P.padded_list()
print GF(gf, n)

If I open a fresh Sage session this will run OK. But when this piece of code is rerun my old friend will reappear:

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

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, n).truncate()
    print p, p.parent()
    R.<x> = QQ[[]]
    P = R(p)
    print P, P.parent()
    return P.padded_list()

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

If I open a fresh Sage session, I have, like tmontile, no problem. However I cannot always open a fresh Sage session when I want to execute GF(gf, n). Therefore consider

n = 7
gf = 2/(1+x+sqrt((1+x)*(1-3*x)))
print GF(gf, n)
p = taylor(gf, x, 0, n).truncate()
print p, p.parent()
R.<x> = QQ[[]]
P = R(p)
print P, P.parent()
P.padded_list()
print GF(gf, n)

If I open a fresh Sage session this will run OK. But when this piece of code is rerun my old friend will reappear:

"TypeError: unable to convert O(x^20) to a rational"
click to hide/show revision 6
retagged

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, n).truncate()
    print p, p.parent()
    R.<x> = QQ[[]]
    P = R(p)
    print P, P.parent()
    return P.padded_list()

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

If I open a fresh Sage session, I have, like tmontile, no problem. However I cannot always open a fresh Sage session when I want to execute GF(gf, n). Therefore consider

n = 7
gf = 2/(1+x+sqrt((1+x)*(1-3*x)))
print GF(gf, n)
p = taylor(gf, x, 0, n).truncate()
print p, p.parent()
R.<x> = QQ[[]]
P = R(p)
print P, P.parent()
P.padded_list()
print GF(gf, n)

If I open a fresh Sage session this will run OK. But when this piece of code is rerun my old friend will reappear:

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