Ask Your Question
1

How to handle integers in the symbolic ring?

asked 2014-12-20 13:22:42 +0200

Peter Luschny gravatar image

Consider the script:

from sage.calculus.calculus import symbolic_sum
x, j = SR.var('x, j')
assume(abs(x)<1)
M = [SR(1), x, x^2 + 1, x^3 + 3*x, x^4 + 6*x^2 + 2, x^5 + 10*x^3 + 10*x]
for k in range(len(M)):
    p = symbolic_sum(x^j*M[k](x=j), j, 0, oo)
    F = p.partial_fraction()
    print [k], [numerator(f) for f in F.operands()]

What I get is:

[0] [1, -1]
[1] [1, 1]
[2] [-2, -3, -2]
[3] [4, 10, 12, 6]
[4] [-9, -33, -62, -60, -24]
[5] [21, 111, 300, 450, 360, 120]

What I expect in the first line is

[0] [-1]

But more generally: Is there a way to get around this darned SR(1) in the first place? This problem appears over and over again and forces to be handled as a separate case. The result is often a mess, as it is the case when the above array M is created by a procedure.

Moreover this workaround does not fit well with other functions as can be seen for example from the bug above.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2014-12-20 21:02:44 +0200

tmonteil gravatar image

updated 2014-12-20 21:16:48 +0200

Your unexpectedd result is not about SR(1), it comes from the fact that, when k=0, F equals -1/(x - 1), which is not a sum, but a product of -1 with 1/(x - 1), hence you got the two operands of the mul operator. If you want to see -1/(x - 1) as the sum of a single fraction, a possible trick could be to add an independent variable y to ensure that you do not have a trivial sum, and then remove it from the list of operands:

from sage.calculus.calculus import symbolic_sum
x, j = SR.var('x, j')
y = SR.var('y')
assume(abs(x)<1)
M = [SR(1), x, x^2 + 1, x^3 + 3*x, x^4 + 6*x^2 + 2, x^5 + 10*x^3 + 10*x]
for k in range(len(M)):
    p = symbolic_sum(x^j*M[k](x=j), j, 0, oo)
    F = p.partial_fraction() + y
    print [k], [numerator(f) for f in F.operands() if f != y]

You should get what you expected:

[0] [-1]
[1] [1, 1]
[2] [-2, -3, -2]
[3] [4, 10, 12, 6]
[4] [-9, -33, -62, -60, -24]
[5] [21, 111, 300, 450, 360, 120]

That said, if you want to avoid to have to substitute x with j in 1 (which requires using SR(1) and not ZZ(1) for which the substitution is not defined), why not using j directly in M ?

from sage.calculus.calculus import symbolic_sum
x, j = SR.var('x, j')
y = SR.var('y')
assume(abs(x)<1)
M = [1, j, j^2 + 1, j^3 + 3*j, j^4 + 6*j^2 + 2, j^5 + 10*j^3 + 10*j]
for k in range(len(M)):
    p = symbolic_sum(x^j*M[k], j, 0, oo)
    F = p.partial_fraction() + y
    print [k], [numerator(f) for f in F.operands() if f != y]

While we are at it, instead of counting elements of M, iterate over the integer k, and use the k-th element of M, note that you can enumerate elements of M directly:

from sage.calculus.calculus import symbolic_sum
x, j = SR.var('x, j')
y = SR.var('y')
assume(abs(x)<1)
M = [1, j, j^2 + 1, j^3 + 3*j, j^4 + 6*j^2 + 2, j^5 + 10*j^3 + 10*j]
for k, m in enumerate(M):
    p = symbolic_sum(x^j*m, j, 0, oo)
    F = p.partial_fraction() + y
    print [k], [numerator(f) for f in F.operands() if f != y]
edit flag offensive delete link more

Comments

Adding y is a nice trick!

slelievre gravatar imageslelievre ( 2014-12-20 22:46:43 +0200 )edit
1

answered 2014-12-20 16:13:03 +0200

rws gravatar image

updated 2014-12-20 16:15:21 +0200

You expect ex.operands() to give the sum terms, which works because they are mostly sums, but the first expression is a fraction (1/(1-x) = Sum(x^j,0,inf)). I don't think there is a function that always gives you sum terms, you would have to write one.

Also, since operands() expects an expression you must convert the Python 1 via SR. Treating everything to have a symbolic meaning will not work with Sage.

edit flag offensive delete link more

Comments

The way I see it: trick 1: SR(1), trick 2: assume(abs(x)<1), trick 3: +y. I do not feel this is the right way to do things. I had to consult three experts on ask.sagemath, slelievre, rws, tmonteil (thanks to you all) to get things straight which worked right out of the box with Maple without such tricks.

Peter Luschny gravatar imagePeter Luschny ( 2014-12-22 18:44:43 +0200 )edit

I think the problem is simply that 1. only a few people manage to contribute to Sage development, and 2. most of those that do have no interest in symbolics. Everything follows from that.

rws gravatar imagerws ( 2014-12-23 08:19:53 +0200 )edit

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-12-20 13:22:42 +0200

Seen: 578 times

Last updated: Dec 20 '14