Ask Your Question

Sum using coefficients of a expansion

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

Dave gravatar image

updated 2018-05-25 19:02:38 -0500

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:


then I need to use the coefficients in a sum, but if I for example do the following:


I get zero. Any idea why this is happening?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-05-24 17:14:44 -0500

eric_g gravatar image

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)

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

edit flag offensive delete link more


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.

Dave gravatar imageDave ( 2018-05-25 18:53:56 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower


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

Seen: 34 times

Last updated: May 25