Ask Your Question

Difference between sum and for loop

asked 2013-10-28 07:04:05 -0600

tommytricks gravatar image

updated 2014-11-10 09:31:24 -0600

tmonteil gravatar image


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 2x+2n+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,


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)


edit retag flag offensive close merge delete


Ok 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.

tommytricks gravatar imagetommytricks ( 2013-10-29 09:14:08 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-10-29 14:51:51 -0600

tmonteil gravatar image

There 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)
sage: h(4,4,2,1,0)

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, they somehow play the role of the identity function x |--> x and are not aimed at receiving a value:

sage: var('x')
sage: x.derivative()

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)


You can read more about symbolic expressions here.

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.

edit flag offensive delete link more


Hi, 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 ...(more)

tommytricks gravatar imagetommytricks ( 2013-10-29 21:33:58 -0600 )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



Asked: 2013-10-28 07:04:05 -0600

Seen: 670 times

Last updated: Oct 29 '13