# How to handle integers in the symbolic ring?

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:

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


What I expect in the first line is

 [-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 close merge delete

Sort by » oldest newest most voted

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:

 [-1]
 [1, 1]
 [-2, -3, -2]
 [4, 10, 12, 6]
 [-9, -33, -62, -60, -24]
 [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]

more

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.

more

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.

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.