ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 26 Mar 2019 00:22:57 +0100SUBSTITUTIONS IN SYMBOLIC EXPRESSIONShttps://ask.sagemath.org/question/45894/substitutions-in-symbolic-expressions/ Hi,
I would like to know if it is possible to substitute expressions inside some equation or expression.
Consider the following expression:
$$(r+1)\cdot(r+2)+g+a$$
Then, I would like to say that I want this same expression for $c+d = 2\cdot r + a$.
Is this possible?
Can I implement something like this in SAGE?Sun, 24 Mar 2019 19:43:13 +0100https://ask.sagemath.org/question/45894/substitutions-in-symbolic-expressions/Comment by Juanjo for <p>Hi,</p>
<p>I would like to know if it is possible to substitute expressions inside some equation or expression.</p>
<p>Consider the following expression:</p>
<p>$$(r+1)\cdot(r+2)+g+a$$</p>
<p>Then, I would like to say that I want this same expression for $c+d = 2\cdot r + a$.</p>
<p>Is this possible? </p>
<p>Can I implement something like this in SAGE?</p>
https://ask.sagemath.org/question/45894/substitutions-in-symbolic-expressions/?comment=45900#post-id-45900I think you should explain a bit more what you are looking for. If, for example, you only want to replace $a$ in the expression by its value from the equality, you can simply do this:
var("a,c,d,g,r")
expression = (r+1)*(r+2) + g + a
expression.subs(a=c+d-2*r)Mon, 25 Mar 2019 19:30:45 +0100https://ask.sagemath.org/question/45894/substitutions-in-symbolic-expressions/?comment=45900#post-id-45900Answer by Emmanuel Charpentier for <p>Hi,</p>
<p>I would like to know if it is possible to substitute expressions inside some equation or expression.</p>
<p>Consider the following expression:</p>
<p>$$(r+1)\cdot(r+2)+g+a$$</p>
<p>Then, I would like to say that I want this same expression for $c+d = 2\cdot r + a$.</p>
<p>Is this possible? </p>
<p>Can I implement something like this in SAGE?</p>
https://ask.sagemath.org/question/45894/substitutions-in-symbolic-expressions/?answer=45909#post-id-45909Do you mean:
sage: var("r,g,a,c,d")
(r, g, a, c, d)
sage: Exp=(r+1)*(r+2)+g+a; Exp
(r + 2)*(r + 1) + a + g
sage: Exp.expand()-(2*r+a)+(c+d)
r^2 + c + d + g + r + 2
?Tue, 26 Mar 2019 00:22:57 +0100https://ask.sagemath.org/question/45894/substitutions-in-symbolic-expressions/?answer=45909#post-id-45909