ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 18 Jul 2021 16:49:44 +0200Sage refuses to evaluate summations...https://ask.sagemath.org/question/58045/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?Sun, 18 Jul 2021 00:10:31 +0200https://ask.sagemath.org/question/58045/sage-refuses-to-evaluate-summations/Answer by slelievre for <p>I'm trying to define a function</p>
<pre><code>g(x) = sum(frac(j*p/q), j, 0, x)
</code></pre>
<p>where <code>p</code>, <code>q</code> are pre-defined constants (say <code>(p, q)=(33, 21)</code>).
Sage did not give any errors, but when I type <code>g(10)</code>,
I would get output symbolic expression <code>sum(frac(11/7*j), j, 0, 10)</code>,
which is different from the expected value <code>31/7</code>.</p>
<p>How do I fix this?</p>
https://ask.sagemath.org/question/58045/sage-refuses-to-evaluate-summations/?answer=58046#post-id-58046There 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/7Sun, 18 Jul 2021 10:37:21 +0200https://ask.sagemath.org/question/58045/sage-refuses-to-evaluate-summations/?answer=58046#post-id-58046Comment by Hyacinth for <p>There are two ways to get the value you want.</p>
<p>One is to define a Python function rather than a symbolic function.</p>
<p>Define p and q:</p>
<pre><code>p, q = 33, 21
</code></pre>
<p>Define the function:</p>
<pre><code>def g(x):
return sum(frac(j*p/q) for j in range(x+1))
</code></pre>
<p>Use it:</p>
<pre><code>sage: g(10)
31/7
</code></pre>
<p>The other way, with <code>g</code> defined as in the question
(after declaring <code>j</code> as a symbolic variable)
is to factor the result.</p>
<pre><code>sage: j = SR.var('j')
sage: g(x) = sum(frac(j*p/q), j, 0, x)
sage: g(10).factor()
31/7
</code></pre>
https://ask.sagemath.org/question/58045/sage-refuses-to-evaluate-summations/?comment=58048#post-id-58048Thanks! I want to integrate this function, so a python function is probably not the best choice... Nonetheless, how is factor turning up here?Sun, 18 Jul 2021 16:49:44 +0200https://ask.sagemath.org/question/58045/sage-refuses-to-evaluate-summations/?comment=58048#post-id-58048