Evaluation of sum function in callable expression

50 views
Skip to first unread message

BDesco

unread,
Dec 17, 2017, 5:24:39 AM12/17/17
to sage-support


Hello,
i'm running Sagemath 8.0 in Jupyter Notebook on Windows 10.
I'm fairly new to Sagemath.
running this:

rrun=2
h2(h2rrunrest)= binomial(h2n-rrun-h2rrunrest-1,h2rrest-h2rrunrest)+h2n
h3(h3rrest)=sum(h2(h3rrunrest,h2rrest=h3rrest,h2n=h3n),h3rrunrest,0,h3rrest)
print(h3(3,h3n=5))

works fine: result: 21

but just adding a multiplication:

rrun=2
h2(h2rrunrest)= 2*binomial(h2n-rrun-h2rrunrest-1,h2rrest-h2rrunrest)+h2n
h3(h3rrest)=sum(h2(h3rrunrest,h2rrest=h3rrest,h2n=h3n),h3rrunrest,0,h3rrest)
print(h3(3,h3n=5))

I get: sum(5, h3rrunrest, 0, 3) instead of the result 22 (5+5+5+7).

But didn't work for me. I'm not sure why.

Thanks!

Ralf Stephan

unread,
Dec 18, 2017, 1:29:44 AM12/18/17
to sage-support
This looks like a bug, you get the sum unevaluated. You can as a workaround evaluate it manually. I get:

print(h3(3,h3n=5).unhold())
20

Thanks for the report.

Ralf Stephan

unread,
Dec 18, 2017, 1:56:05 AM12/18/17
to sage-support
BTW instead of unhold() you can also use n() to get the value of an unevaluated definite sum.

Ralf Stephan

unread,
Dec 18, 2017, 11:18:10 AM12/18/17
to sage-support
Also I think that results other than 20 are wrong for both formulae because the binomial part always amounts to binomial(2-n,3-n), i.e. zero, and you're just adding 5 four times. But I'm not 100 per cent on that.

BDesco

unread,
Dec 26, 2017, 3:03:44 AM12/26/17
to sage-support
for n=3. I get binomial(-1,0) = 1
I don't know why.
Secondly the sum stays 20. But when I calculate the terms I get 5+5+5+7.
At least both results should be the same. Both 20 or both 22.

Thanks. 

BDesco

unread,
Dec 26, 2017, 3:04:59 AM12/26/17
to sage-support
Sorry for my late reply. Thanks for your help.
 
unhold works fine but I could not find any documentation on it.

Bart 

slelievre

unread,
Dec 26, 2017, 6:02:26 PM12/26/17
to sage-support


Tue 2017-12-26 08:03:44 UTC, BDesco:
>
> for n=3. I get binomial(-1,0) = 1
> I don't know why.

You can look at the documentation of binomial:

    sage: binomial?

You will see it indicates that the definition is extended:

    We extend this definition to include cases when [...]

and as a result, for any numerical value of x,

     binomial(x, 0)

returns 1.

slelievre

unread,
Dec 26, 2017, 6:08:59 PM12/26/17
to sage-support


Tue 2017-12-26 08:04:59 UTC, BDesco:


> Sorry for my late reply. Thanks for your help.
> unhold works fine but I could not find any documentation on it.

You can get the documentation of .unhold by using ".unhold?":

    sage: k, n = SR.var('k n')
    sage: s = sum(n^5, n, 0, k, hold=True)
    sage: s
    sum(n^5, n, 0, k)
    sage: s.unhold()
    1/6*k^6 + 1/2*k^5 + 5/12*k^4 - 1/12*k^2
    sage: s.unhold?

Reply all
Reply to author
Forward
0 new messages