Problem in defining a function

asked 2024-07-13 13:55:51 +0200

hamouda gravatar image

I want to compute the sum $$\sum_{k=0}^m\sum_{i=k}^m\sum_{j=rk}^{rk+r-1}(j-rk)+\sum_{k=0}^m\sum_{i=k}^k \sum_{j=rk+r}^{rk+tk}(j-rk)$$ Using Sage, one can use the code

i,j,k,r,m,t,s=var('i j k r m t s') 
x1=sum(sum(sum(j-r*k,j,r*k,r*k+r-1),i,k,m),k,0,m)
y1=sum(sum(sum(j-r*k,j,r*k+r,r*k+t*k),i,k,k),k,1,m)
nY=(x1+y1).expand().collect(m);

Note that the case $k=0$ in the second sum is exclused. To verify the sum is exact, I have defined the following function which also compute nY.

def Ver(m,t,r):
    s=0
    for k in srange(m+1):
        for i in srange(k,m+1):
            for j in srange(r*k,r*k+r):
                s=s+j-r*k
    for k in srange(m+1):
        for i in srange(k,k+1):
            for j in srange(r*k+r,r*k+t*k+1):
                s=s+j-r*k
    return s

Normally the two methods must give the same vaule, but in the case (m,t,r)=(2,1,3) we have nY.subs(m=2,t=1,r=3)=16 and Ver(2,1,3)=18, I want to know where is the problem which gives the error ?.

edit retag flag offensive close merge delete

Comments

2

This happens when r is much larger than t*k, because the inner summation in the second half of Ver implies bounds for range in the wrong order. By polynomial interpolation, these sums should not be empty, but negative, hence the discrepancy.

FrédéricC gravatar imageFrédéricC ( 2024-07-13 20:42:27 +0200 )edit

@FrédéricC : could you amplify ?

I'm working on this one, and find (yet) unexplained dscreancies between Sage, Sympy and the Wolfram engine...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2024-07-13 21:00:34 +0200 )edit

Another way is to say that the equality is only valid when all the bounds in the range are in the correct order.

FrédéricC gravatar imageFrédéricC ( 2024-07-13 21:03:14 +0200 )edit

@FrédéricC : If I understand you, this means that $\displaystyle{\sum_{i=5}^1 x_i \neq \sum_{i\in 1,\cdots,5} x_i}$.

In other words the $\sum$ symbol is not unambiguous :

  • The "bounded" form implies that the index belongs to an ordered set, and the $\sum$ symbol does not denote a single quantity but an algorithm using said order and some conventions for computing it.

  • Conversively, the "set" form is unambiguous (as long as the addition is commutative over the set under consideration).

Is this correct ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2024-07-14 08:22:25 +0200 )edit

think of the analog situation for integrals. There $\int_{1}^{0} f(x) dx$ is just the opposite.

FrédéricC gravatar imageFrédéricC ( 2024-07-14 08:51:15 +0200 )edit