ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 25 May 2018 18:53:56 -0500Sum using coefficients of a expansionhttp://ask.sagemath.org/question/42416/sum-using-coefficients-of-a-expansion/I'm trying to sum over the coefficients of an expansion but the sum gives zero. I use the taylor method to expand a function f:
f=sin(x)
ft=f.taylor(x,0,6)
then I need to use the coefficients in a sum, but if I for example do the following:
sum(x^(i)*ft.coefficient(x,i),i,0,5)
I get zero. Any idea why this is happening?Wed, 23 May 2018 19:40:44 -0500http://ask.sagemath.org/question/42416/sum-using-coefficients-of-a-expansion/Answer by eric_g for <p>I'm trying to sum over the coefficients of an expansion but the sum gives zero. I use the taylor method to expand a function f:</p>
<pre><code>f=sin(x)
ft=f.taylor(x,0,6)
</code></pre>
<p>then I need to use the coefficients in a sum, but if I for example do the following:</p>
<pre><code>sum(x^(i)*ft.coefficient(x,i),i,0,5)
</code></pre>
<p>I get zero. Any idea why this is happening?</p>
http://ask.sagemath.org/question/42416/sum-using-coefficients-of-a-expansion/?answer=42430#post-id-42430You should use instead:
sage: sum(x^i*ft.coefficient(x, i) for i in [0..5])
1/120*x^5 - 1/6*x^3 + x
*Explanation:* your original code involves **Sage's symbolic sum** and `i` has to be a symbolic variable (you probably declared it as such via `var('i')`). Now, the method `coefficient` is expecting an integer, not a symbolic variable. For some reason, it returns always zero for a symbolic variable (IMHO an error message would be more appropriate here):
sage: i = var('i')
sage: ft.coefficient(x, i)
0
Hence the result that you get. On the contrary, the command
sum(x^i*ft.coefficient(x, i) for i in [0..5])
involves **Python's sum**, for which `i` is an integer, so that `ft.coefficient` behaves correctly. For more details, see the documentation returned by `sum?` (in particular the warning in it).
Thu, 24 May 2018 17:14:44 -0500http://ask.sagemath.org/question/42416/sum-using-coefficients-of-a-expansion/?answer=42430#post-id-42430Comment by Dave for <p>You should use instead:</p>
<pre><code>sage: sum(x^i*ft.coefficient(x, i) for i in [0..5])
1/120*x^5 - 1/6*x^3 + x
</code></pre>
<p><em>Explanation:</em> your original code involves <strong>Sage's symbolic sum</strong> and <code>i</code> has to be a symbolic variable (you probably declared it as such via <code>var('i')</code>). Now, the method <code>coefficient</code> is expecting an integer, not a symbolic variable. For some reason, it returns always zero for a symbolic variable (IMHO an error message would be more appropriate here):</p>
<pre><code>sage: i = var('i')
sage: ft.coefficient(x, i)
0
</code></pre>
<p>Hence the result that you get. On the contrary, the command</p>
<pre><code>sum(x^i*ft.coefficient(x, i) for i in [0..5])
</code></pre>
<p>involves <strong>Python's sum</strong>, for which <code>i</code> is an integer, so that <code>ft.coefficient</code> behaves correctly. For more details, see the documentation returned by <code>sum?</code> (in particular the warning in it).</p>
http://ask.sagemath.org/question/42416/sum-using-coefficients-of-a-expansion/?comment=42440#post-id-42440Thank you! I ended up doing a for loop and it worked so your explanation makes sense, I will implement it. Big fan of your work on SageManifolds by the way, thank you for that too.Fri, 25 May 2018 18:53:56 -0500http://ask.sagemath.org/question/42416/sum-using-coefficients-of-a-expansion/?comment=42440#post-id-42440