Problem in defining a function

asked 0 years ago

hamouda gravatar image

I want to compute the sum mk=0mi=krk+r1j=rk(jrk)+mk=0ki=krk+tkj=rk+r(jrk) 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 ?.

Preview: (hide)

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 ( 0 years ago )

@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 ( 0 years ago )

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 ( 0 years ago )

@FrédéricC : If I understand you, this means that 1i=5xii1,,5xi.

In other words the symbol is not unambiguous :

  • The "bounded" form implies that the index belongs to an ordered set, and the 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 ( 0 years ago )

think of the analog situation for integrals. There 01f(x)dx is just the opposite.

FrédéricC gravatar imageFrédéricC ( 0 years ago )