# Revision history [back]

### 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 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,

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!

 2 retagged tmonteil 20583 ●25 ●146 ●381 http://wiki.sagemath.o...

### 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 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,

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!