ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 23 Dec 2014 08:19:53 +0100How to handle integers in the symbolic ring?https://ask.sagemath.org/question/25305/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:
[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.Sat, 20 Dec 2014 13:22:42 +0100https://ask.sagemath.org/question/25305/how-to-handle-integers-in-the-symbolic-ring/Answer by rws for <p>Consider the script:</p>
<pre><code>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()]
</code></pre>
<p>What I get is:</p>
<pre><code>[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]
</code></pre>
<p>What I expect in the first line is </p>
<pre><code>[0] [-1]
</code></pre>
<p>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.</p>
<p>Moreover this workaround does not fit well with other functions
as can be seen for example from the bug above.</p>
https://ask.sagemath.org/question/25305/how-to-handle-integers-in-the-symbolic-ring/?answer=25307#post-id-25307You 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.Sat, 20 Dec 2014 16:13:03 +0100https://ask.sagemath.org/question/25305/how-to-handle-integers-in-the-symbolic-ring/?answer=25307#post-id-25307Comment by Peter Luschny for <p>You expect <code>ex.operands()</code> to give the sum terms, which works because they are mostly sums, but the first expression is a fraction (<code>1/(1-x) = Sum(x^j,0,inf)</code>). I don't think there is a function that always gives you sum terms, you would have to write one. </p>
<p>Also, since <code>operands()</code> expects an expression you must convert the Python <code>1</code> via <code>SR</code>. Treating everything to have a symbolic meaning will not work with Sage.</p>
https://ask.sagemath.org/question/25305/how-to-handle-integers-in-the-symbolic-ring/?comment=25326#post-id-25326The 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.Mon, 22 Dec 2014 18:44:43 +0100https://ask.sagemath.org/question/25305/how-to-handle-integers-in-the-symbolic-ring/?comment=25326#post-id-25326Comment by rws for <p>You expect <code>ex.operands()</code> to give the sum terms, which works because they are mostly sums, but the first expression is a fraction (<code>1/(1-x) = Sum(x^j,0,inf)</code>). I don't think there is a function that always gives you sum terms, you would have to write one. </p>
<p>Also, since <code>operands()</code> expects an expression you must convert the Python <code>1</code> via <code>SR</code>. Treating everything to have a symbolic meaning will not work with Sage.</p>
https://ask.sagemath.org/question/25305/how-to-handle-integers-in-the-symbolic-ring/?comment=25329#post-id-25329I 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.Tue, 23 Dec 2014 08:19:53 +0100https://ask.sagemath.org/question/25305/how-to-handle-integers-in-the-symbolic-ring/?comment=25329#post-id-25329Answer by tmonteil for <p>Consider the script:</p>
<pre><code>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()]
</code></pre>
<p>What I get is:</p>
<pre><code>[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]
</code></pre>
<p>What I expect in the first line is </p>
<pre><code>[0] [-1]
</code></pre>
<p>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.</p>
<p>Moreover this workaround does not fit well with other functions
as can be seen for example from the bug above.</p>
https://ask.sagemath.org/question/25305/how-to-handle-integers-in-the-symbolic-ring/?answer=25310#post-id-25310Your 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]
Sat, 20 Dec 2014 21:02:44 +0100https://ask.sagemath.org/question/25305/how-to-handle-integers-in-the-symbolic-ring/?answer=25310#post-id-25310Comment by slelievre for <p>Your unexpectedd result is not about <code>SR(1)</code>, it comes from the fact that, when <code>k=0</code>, <code>F</code> equals <code>-1/(x - 1)</code>, which is not a sum, but a product of <code>-1</code> with <code>1/(x - 1)</code>, hence you got the two operands of the <code>mul</code> operator. If you want to see <code>-1/(x - 1)</code> as the sum of a single fraction, a possible trick could be to add an independent variable <code>y</code> to ensure that you do not have a trivial sum, and then remove it from the list of operands:</p>
<pre><code>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]
</code></pre>
<p>You should get what you expected:</p>
<pre><code>[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]
</code></pre>
<p>That said, if you want to avoid to have to substitute <code>x</code> with <code>j</code> in <code>1</code> (which requires using <code>SR(1)</code> and not <code>ZZ(1)</code> for which the substitution is not defined), why not using <code>j</code> directly in <code>M</code> ?</p>
<pre><code>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]
</code></pre>
<p>While we are at it, instead of counting elements of <code>M</code>, iterate over the integer <code>k</code>, and use the <code>k</code>-th element of <code>M</code>, note that you can enumerate elements of <code>M</code> directly:</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/25305/how-to-handle-integers-in-the-symbolic-ring/?comment=25311#post-id-25311Adding `y` is a nice trick!Sat, 20 Dec 2014 22:46:43 +0100https://ask.sagemath.org/question/25305/how-to-handle-integers-in-the-symbolic-ring/?comment=25311#post-id-25311