Processing math: 100%

First time here? Check out the FAQ!

Ask Your Question
0

How to add a condition in the sum function in sage

asked 0 years ago

hamouda gravatar image

updated 0 years ago

FrédéricC gravatar image

I want to implement the following sum nj=0ajxj under the following conditions the coefficient aj=j if j is even, or j+1 otherwise. My problem is how to include these cases in the sum function in sage. Normally the command is sum(a_j*x^j,j,1,n) but how to distinguish between the parity of j ?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 0 years ago

rburing gravatar image

Instead of using the symbolic sum that you mention, you can use Python's built-in sum that takes an iterable such as a generator expression:

sage: n = 5
sage: sum((j if j % 2 == 0 else j + 1)*x^j for j in range(1,n+1))
6*x^5 + 4*x^4 + 4*x^3 + 2*x^2 + 2*x
Preview: (hide)
link

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 0 years ago

Seen: 259 times

Last updated: Jun 05 '24