What is equivalent to applyrule in Maple

I have an expression e1 and in e1 I would like to replace e2 (a sub-expression of e1) with e3 (a different expression). In Maple I use

applyrule([e2=e3,1/e2=1/e3],e1);

Is there a similar command in Sage?

edit retag close merge delete

Sort by ยป oldest newest most voted

You can use the method subs:

sage: e1 = cos(x) + sin(x) - exp(x) + x^2/3
sage: e2 = sin(x)
sage: e3 = 1/x + 1/x^2
sage: e1.subs({e2:e3})
1/3*x^2 + 1/x + 1/x^2 + cos(x) - e^x


Note though that it won't work with too complicated expressions for e2. For more on this, you may have a look to the documentation of subs in the reference manual.

more

You can try:

sage: e1.substitute({e2:e3})


or even

sage: e1.substitute({e2:e3, 1/e2:1/e3})


if it can help.

If this does not work, please do not hesitate to provide a way to construct e1, e2 and e3 so that we can think about it.

more

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

Stats

Asked: 2017-05-11 03:01:56 +0200

Seen: 295 times

Last updated: May 11 '17