Ask Your Question
1

Sum using coefficients of a expansion

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

Dave gravatar image

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

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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

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

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

edit flag offensive delete link 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.

Dave gravatar imageDave ( 2018-05-25 18:53:56 -0600 )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

Stats

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

Seen: 47 times

Last updated: May 25