ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 11 May 2017 02:16:47 -0500What is equivalent to applyrule in Maplehttps://ask.sagemath.org/question/37567/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?
Wed, 10 May 2017 20:01:56 -0500https://ask.sagemath.org/question/37567/what-is-equivalent-to-applyrule-in-maple/Answer by B r u n o for <p>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 </p>
<p>applyrule([e2=e3,1/e2=1/e3],e1);</p>
<p>Is there a similar command in Sage?</p>
https://ask.sagemath.org/question/37567/what-is-equivalent-to-applyrule-in-maple/?answer=37570#post-id-37570You 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](http://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html#sage.symbolic.expression.Expression.subs).Thu, 11 May 2017 02:13:21 -0500https://ask.sagemath.org/question/37567/what-is-equivalent-to-applyrule-in-maple/?answer=37570#post-id-37570Answer by tmonteil for <p>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 </p>
<p>applyrule([e2=e3,1/e2=1/e3],e1);</p>
<p>Is there a similar command in Sage?</p>
https://ask.sagemath.org/question/37567/what-is-equivalent-to-applyrule-in-maple/?answer=37571#post-id-37571You 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.Thu, 11 May 2017 02:16:47 -0500https://ask.sagemath.org/question/37567/what-is-equivalent-to-applyrule-in-maple/?answer=37571#post-id-37571