Ask Your Question

# 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?

edit retag close merge delete

## 1 answer

Sort by » oldest newest most voted

You 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).

more

## Comments

Thank 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.

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2018-05-23 19:40:44 -0500

Seen: 57 times

Last updated: May 25 '18