Loading [MathJax]/jax/output/HTML-CSS/jax.js
Ask Your Question
1

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

asked 7 years ago

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: |p(ρϵe(iθϵ)+w)|2=ρ0ρmϵρme(imθϵiθ0+iθm)+ρ0ρmϵρme(imθϵ+iθ0iθm)+ρ2mϵρ2m+ρ20

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 7 years ago

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])
Preview: (hide)
link

Comments

Thanks too ndomes.

ortollj gravatar imageortollj ( 7 years ago )

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

ortollj gravatar imageortollj ( 7 years ago )
2

answered 7 years ago

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)
Preview: (hide)
link

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 ( 7 years ago )

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

tmonteil gravatar imagetmonteil ( 7 years ago )

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: 7 years ago

Seen: 725 times

Last updated: Sep 03 '17