Ask Your Question
1

Get the nth term of an equation.rhs() sum

asked 2017-09-03 10:59:01 +0100

ortollj gravatar image

Hi

equ0 = term_0 == term_1 + term_2 + term_3

I want to extract term_1 + term_2 to work with.

I have been looking for a long time without success, I read the whole page: link text

example: $ {\left| p\left(\rho_{\epsilon} e^{\left(i \, \theta_{\epsilon}\right)} + w\right) \right|}^{2} = \rho_{0} \rho_{\epsilon}^{m} \rho_{m} e^{\left(i \, m \theta_{\epsilon} - i \, \theta_{0} + i \, \theta_{m}\right)} + \rho_{0} \rho_{\epsilon}^{m} \rho_{m} e^{\left(-i \, m \theta_{\epsilon} + i \, \theta_{0} - i \, \theta_{m}\right)} + \rho_{\epsilon}^{2 \, m} \rho_{m}^{2} + \rho_{0}^{2} $

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2017-09-03 12:00:55 +0100

ndomes gravatar image
var('term_0 term_1 term_2 term_3')
equ0 = term_0 == term_1 + term_2 + term_3 

# use the operands method of expressions
print equ0.rhs().operands()

# subtract third term (the last in the list of operands)
print equ0.rhs() - equ0.rhs().operands()[-1]
# or build expression from list of operands
print sum(equ0.rhs().operands()[:2])
edit flag offensive delete link more

Comments

Thanks too ndomes.

ortollj gravatar imageortollj ( 2017-09-03 12:47:41 +0100 )edit

moreover operands() appears in the link I put above, but I did not see it ;-(, really sorry.

ortollj gravatar imageortollj ( 2017-09-03 12:53:42 +0100 )edit
2

answered 2017-09-03 12:01:17 +0100

tmonteil gravatar image

You should really give explicit example with Sage code, so that we really understand what do you want to achieve. Let me take my own example:

sage: equ0 = cos(x) == 3*x^2 + log(x) + 42*sin(x)
sage: equ0
cos(x) == 3*x^2 + log(x) + 42*sin(x)

You can first extract the right-hand-side of the equation as follows:

sage: equ0.rhs()
3*x^2 + log(x) + 42*sin(x)

Then, you can check that it is an addition:

sage: equ0.rhs().operator()
<function add_vararg at 0x7f5a30600050>

For which you can get the operands:

sage: equ0.rhs().operands()
[3*x^2, log(x), 42*sin(x)]

You can extract the first two terms of this list:

sage: equ0.rhs().operands()[:2]
[3*x^2, log(x)]

You can them add them together:

sage: sum(equ0.rhs().operands()[:2])
3*x^2 + log(x)

WARNING the order of the operands is not necessrilly the one you entered:

sage: equ0 = cos(x) == log(x) + 3*x^2 + 42*sin(x)
sage: equ0
cos(x) == 3*x^2 + log(x) + 42*sin(x)
edit flag offensive delete link more

Comments

Thanks tmonteil, it is exactly what I would like to do ! you said "WARNING the order of the operands is not necessarily the one you entered"

Yes,but if it is the first term I see when I do a show(equ0.rhs()) I suppose it will have the first indice for operands().

ortollj gravatar imageortollj ( 2017-09-03 12:46:27 +0100 )edit

Correct, the way symbolic operands are reordered by Sage is deterministic.

tmonteil gravatar imagetmonteil ( 2017-09-03 13:20:55 +0100 )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: 2017-09-03 10:59:01 +0100

Seen: 690 times

Last updated: Sep 03 '17