# Sage refuses to evaluate summations...

I'm trying to define a function

g(x) = sum(frac(j*p/q), j, 0, x)


where p, q are pre-defined constants (say (p, q)=(33, 21)). Sage did not give any errors, but when I type g(10), I would get output symbolic expression sum(frac(11/7*j), j, 0, 10), which is different from the expected value 31/7.

How do I fix this?

edit retag close merge delete

Sort by » oldest newest most voted

There are two ways to get the value you want.

One is to define a Python function rather than a symbolic function.

Define p and q:

p, q = 33, 21


Define the function:

def g(x):
return sum(frac(j*p/q) for j in range(x+1))


Use it:

sage: g(10)
31/7


The other way, with g defined as in the question (after declaring j as a symbolic variable) is to factor the result.

sage: j = SR.var('j')
sage: g(x) = sum(frac(j*p/q), j, 0, x)
sage: g(10).factor()
31/7

more

Thanks! I want to integrate this function, so a python function is probably not the best choice... Nonetheless, how is factor turning up here?

( 2021-07-18 16:49:44 +0200 )edit