Ask Your Question

tommytricks's profile - activity

2018-06-29 11:06:30 -0500 received badge  Famous Question (source)
2014-11-10 09:30:29 -0500 received badge  Notable Question (source)
2014-04-13 06:44:40 -0500 received badge  Popular Question (source)
2013-10-29 21:33:58 -0500 commented answer Difference between sum and for loop

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)

2013-10-29 09:14:08 -0500 commented question Difference between sum and for loop

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.

2013-10-28 07:04:05 -0500 asked a question Difference between sum and for loop


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)