ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 29 Oct 2013 21:33:58 -0500Difference between sum and for loophttp://ask.sagemath.org/question/10669/difference-between-sum-and-for-loop/Hi,
I'm currently using sage to calculate some double sums over two variables, where s runs from 1 to t-1, and t runs from 2 to 2*x+2*n+1. I initially used two nested for loops inside the following function:
def qentr(i,j,k,x,n):
s,t,sm = var('s,t,sm')
sm = 0
for t in range(2,2*x+2*n+2):
for s in range(1,t):
sm = sm + h(i,s,k,x,n)*h(j,t,k,x,n) - h(j,s,k,x,n)*h(i,t,k,x,n)
return sm
where i, j, k, x, n are all nonnegative integers. This gives me the correct values for what I am enumerating, for instance qentr(1,3,3,1,2) = 96, which is right. For some reason when I replace these for loops with two nested sums, say,
sum(sum(h(i,s,k,x,n)*h(j,t,k,x,n)-h(i,t,k,x,n)*h(j,s,k,x,n),s,1,t-1),t,2,2*x+2*n+1)
this no longer gives me the correct values. If I replace the two nested loops with this sum I get qentr(1,3,3,1,2)=196....
I wanted to replace the loops with sum in order to return an expression in x, the only way I can think to do this is with sum but this does not yield the right expression. Does anyone know why this happens? Does anyone know an alternative way I can get the function qentr() to return a polynomial in x?
The function h from above is the following:
def h(i,j,k,x,n):
r = var('r')
return binomial(j-1,i-1)-2*sum(binomial(r+i-k,i-k)*binomial(j-i-r+k-2,k-2),r,x+n+(k+1)/2+1-i,j-i)
Cheers!Mon, 28 Oct 2013 07:04:05 -0500http://ask.sagemath.org/question/10669/difference-between-sum-and-for-loop/Comment by tommytricks for <p>Hi,</p>
<p>I'm currently using sage to calculate some double sums over two variables, where s runs from 1 to t-1, and t runs from 2 to 2<em>x+2</em>n+1. I initially used two nested for loops inside the following function:</p>
<pre><code>def qentr(i,j,k,x,n):
s,t,sm = var('s,t,sm')
sm = 0
for t in range(2,2*x+2*n+2):
for s in range(1,t):
sm = sm + h(i,s,k,x,n)*h(j,t,k,x,n) - h(j,s,k,x,n)*h(i,t,k,x,n)
return sm
</code></pre>
<p>where i, j, k, x, n are all nonnegative integers. This gives me the correct values for what I am enumerating, for instance qentr(1,3,3,1,2) = 96, which is right. For some reason when I replace these for loops with two nested sums, say,</p>
<pre><code>sum(sum(h(i,s,k,x,n)*h(j,t,k,x,n)-h(i,t,k,x,n)*h(j,s,k,x,n),s,1,t-1),t,2,2*x+2*n+1)
</code></pre>
<p>this no longer gives me the correct values. If I replace the two nested loops with this sum I get qentr(1,3,3,1,2)=196....</p>
<p>I wanted to replace the loops with sum in order to return an expression in x, the only way I can think to do this is with sum but this does not yield the right expression. Does anyone know why this happens? Does anyone know an alternative way I can get the function qentr() to return a polynomial in x? </p>
<p>The function h from above is the following:</p>
<pre><code>def h(i,j,k,x,n):
r = var('r')
return binomial(j-1,i-1)-2*sum(binomial(r+i-k,i-k)*binomial(j-i-r+k-2,k-2),r,x+n+(k+1)/2+1-i,j-i)
</code></pre>
<p>Cheers!</p>
http://ask.sagemath.org/question/10669/difference-between-sum-and-for-loop/?comment=16832#post-id-16832Ok so I've had a look at it and the problem arises for some specific values in the double sum. For example, with the functions defined above, h(3,2,3,1,2) = 0 which is correct. However, when I try to compute the same value with sum, I get instead sum(h(3,t,3,1,2),t,2,2) = -6. I think this has something to do with the way in which sum evaluates its arguments. I can't work out how to deal with this, if anyone has any ideas I would really appreciate it.Tue, 29 Oct 2013 09:14:08 -0500http://ask.sagemath.org/question/10669/difference-between-sum-and-for-loop/?comment=16832#post-id-16832Answer by tmonteil for <p>Hi,</p>
<p>I'm currently using sage to calculate some double sums over two variables, where s runs from 1 to t-1, and t runs from 2 to 2<em>x+2</em>n+1. I initially used two nested for loops inside the following function:</p>
<pre><code>def qentr(i,j,k,x,n):
s,t,sm = var('s,t,sm')
sm = 0
for t in range(2,2*x+2*n+2):
for s in range(1,t):
sm = sm + h(i,s,k,x,n)*h(j,t,k,x,n) - h(j,s,k,x,n)*h(i,t,k,x,n)
return sm
</code></pre>
<p>where i, j, k, x, n are all nonnegative integers. This gives me the correct values for what I am enumerating, for instance qentr(1,3,3,1,2) = 96, which is right. For some reason when I replace these for loops with two nested sums, say,</p>
<pre><code>sum(sum(h(i,s,k,x,n)*h(j,t,k,x,n)-h(i,t,k,x,n)*h(j,s,k,x,n),s,1,t-1),t,2,2*x+2*n+1)
</code></pre>
<p>this no longer gives me the correct values. If I replace the two nested loops with this sum I get qentr(1,3,3,1,2)=196....</p>
<p>I wanted to replace the loops with sum in order to return an expression in x, the only way I can think to do this is with sum but this does not yield the right expression. Does anyone know why this happens? Does anyone know an alternative way I can get the function qentr() to return a polynomial in x? </p>
<p>The function h from above is the following:</p>
<pre><code>def h(i,j,k,x,n):
r = var('r')
return binomial(j-1,i-1)-2*sum(binomial(r+i-k,i-k)*binomial(j-i-r+k-2,k-2),r,x+n+(k+1)/2+1-i,j-i)
</code></pre>
<p>Cheers!</p>
http://ask.sagemath.org/question/10669/difference-between-sum-and-for-loop/?answer=15628#post-id-15628There is already a problem in the `h()` function:
sage: h(4,4,2,x,0)
1/3*x^3 - 1/2*x^2 - 1/12*x - 7/8
sage: h(4,4,2,x,0)(x=1)
-9/8
sage: h(4,4,2,1,0)
1/4
First, your sum is indexed on non-integer entries as `x+n+(k+1)/2+1-i` may not be an integer. You should fix this if possible (i am not sure how the symbolic `sum()` handles this, and this might be a reason for this weird behaviour).
Then, you should make a difference between symbolic variables and python variables.
A symbolic variable is defined with the `var()` function ; these are elements of the [Symbolic Ring](http://www.sagemath.org/doc/reference/calculus/sage/symbolic/ring.html), they somehow play the role of the identity function `x |--> x` and are not aimed at receiving a value:
sage: var('x')
x
sage: x.derivative()
1
But then writing:
sage: x = 2
just overwrites the previous value of `x` (which was a kind of identity map), and make x an integer. If you want to evaluate the symbolic variable `x` at the point 2, you will have to write
sage: x(x=2)
instead.
You can read more about symbolic expressions [here](http://www.sagemath.org/doc/reference/calculus/sage/calculus/calculus.html).
When you write:
t = var('t')
and later:
for t in range(2,2*x+2*n+2):
Then the `t` stops being a symbolic variable and becomes a python integer, and the first line was useless.
All variables that aim to recieve a value (e.g. i=2) should be python variables, not symbolic ones (though, you could call `h(4,4,2,var('y'),0)` but in this case the symbolic variable `y` is the value recieved by the parameter `x`).
When you write:
def h(i,j,k,x,n):
`i,j,k,x,n` are python parameters. They will eventually get a value. If some of them (`x`?) has to remains a symbolic varialbe until the end, it should not be a parameter of your function. So the question is: which variable in your expression will remain an unknown at the end ?
This is somehow the same difference between the function `sum()` and making a loop: the first one works symbolically and can take symbolic variables as endpoints (like for computing an integral with formulas), the second cannot since it does the computation by effectively adding the elements one by one.
That said, it is still possible that there is a bug with symbolic summations of binomals, but you should first try to clean the two mentionned issues.
I hope i am not too confusing.
Tue, 29 Oct 2013 14:51:51 -0500http://ask.sagemath.org/question/10669/difference-between-sum-and-for-loop/?answer=15628#post-id-15628Comment by tommytricks for <p>There is already a problem in the <code>h()</code> function:</p>
<pre><code>sage: h(4,4,2,x,0)
1/3*x^3 - 1/2*x^2 - 1/12*x - 7/8
sage: h(4,4,2,x,0)(x=1)
-9/8
sage: h(4,4,2,1,0)
1/4
</code></pre>
<p>First, your sum is indexed on non-integer entries as <code>x+n+(k+1)/2+1-i</code> may not be an integer. You should fix this if possible (i am not sure how the symbolic <code>sum()</code> handles this, and this might be a reason for this weird behaviour).</p>
<p>Then, you should make a difference between symbolic variables and python variables.</p>
<p>A symbolic variable is defined with the <code>var()</code> function ; these are elements of the <a href="http://www.sagemath.org/doc/reference/calculus/sage/symbolic/ring.html">Symbolic Ring</a>, they somehow play the role of the identity function <code>x |--> x</code> and are not aimed at receiving a value:</p>
<pre><code>sage: var('x')
x
sage: x.derivative()
1
</code></pre>
<p>But then writing:</p>
<pre><code>sage: x = 2
</code></pre>
<p>just overwrites the previous value of <code>x</code> (which was a kind of identity map), and make x an integer. If you want to evaluate the symbolic variable <code>x</code> at the point 2, you will have to write</p>
<pre><code>sage: x(x=2)
</code></pre>
<p>instead.</p>
<p>You can read more about symbolic expressions <a href="http://www.sagemath.org/doc/reference/calculus/sage/calculus/calculus.html">here</a>.</p>
<p>When you write:</p>
<pre><code>t = var('t')
</code></pre>
<p>and later:</p>
<pre><code>for t in range(2,2*x+2*n+2):
</code></pre>
<p>Then the <code>t</code> stops being a symbolic variable and becomes a python integer, and the first line was useless.</p>
<p>All variables that aim to recieve a value (e.g. i=2) should be python variables, not symbolic ones (though, you could call <code>h(4,4,2,var('y'),0)</code> but in this case the symbolic variable <code>y</code> is the value recieved by the parameter <code>x</code>).</p>
<p>When you write:</p>
<pre><code>def h(i,j,k,x,n):
</code></pre>
<p><code>i,j,k,x,n</code> are python parameters. They will eventually get a value. If some of them (<code>x</code>?) has to remains a symbolic varialbe until the end, it should not be a parameter of your function. So the question is: which variable in your expression will remain an unknown at the end ?</p>
<p>This is somehow the same difference between the function <code>sum()</code> and making a loop: the first one works symbolically and can take symbolic variables as endpoints (like for computing an integral with formulas), the second cannot since it does the computation by effectively adding the elements one by one.</p>
<p>That said, it is still possible that there is a bug with symbolic summations of binomals, but you should first try to clean the two mentionned issues.</p>
<p>I hope i am not too confusing.</p>
http://ask.sagemath.org/question/10669/difference-between-sum-and-for-loop/?comment=16831#post-id-16831Hi, thank you for your reply. On the first point, k is always an odd integer, and so this is not a problem. I see what you mean about distinguishing between python variables and symbolic variables. The variable that will remain unknown at the end is x, and x is also a parameter for t and s. However I have been testing this out with various values of x for which I know what the answer should be.
I seem to have isolated where the problem may lie. I'm not sure if you'd read my comment above: with the functions I've defined before
h(3,2,3,1,2) = 0,
which is right. However if I do the same calculation with sum I get
sum(h(3,t,3,1,2),t,2,2) = -6.
This happens for a number of values and is the reason why my code with the double sum doesn't seem to work.Tue, 29 Oct 2013 21:33:58 -0500http://ask.sagemath.org/question/10669/difference-between-sum-and-for-loop/?comment=16831#post-id-16831