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.Sun, 15 Apr 2012 10:46:15 +0200Is there any way I can substitute a combination of variables.https://ask.sagemath.org/question/7750/is-there-any-way-i-can-substitute-a-combination-of-variables/I know this is a long short but I was wondering whether it is possible to substitute a combination of variables by something else. Let me explain it with an example.
Let $f(x,y,z) = f(x) \sqrt{x^2+y^2+z^2} + f(y) \sqrt{x^2+y^2+z^2} + f(z) \sqrt{x^2+y^2+z^2}$
As we can see the combination $\sqrt{x^2+y^2+z^2}$ occurs very often. Is there a way I call this combination some other variable, say, w. I cannot use substitute in this case as I want to leave $x$,$y$ and $z$ alone if they dont come in this specific combination $\sqrt{x^2+y^2+z^2}$.
If it were a string I could have used find and replace. But I want to use the expression as a input later on in the notebook.
Edit: I came across a sympy command which is very close to what I want to do but not quite. There is a command cse in the module simplify. However, it identifies the subexpresssion by itself. As far as I could see it does not offer the flexibility for the users to identify the subexpression. Also for some reason sage expressions cannot be converted to sympy using `f._sympy_()` if there are "i"'s in the expression.
Thanks in advance. Thu, 11 Nov 2010 13:28:57 +0100https://ask.sagemath.org/question/7750/is-there-any-way-i-can-substitute-a-combination-of-variables/Answer by Shashank for <p>I know this is a long short but I was wondering whether it is possible to substitute a combination of variables by something else. Let me explain it with an example.</p>
<p>Let $f(x,y,z) = f(x) \sqrt{x^2+y^2+z^2} + f(y) \sqrt{x^2+y^2+z^2} + f(z) \sqrt{x^2+y^2+z^2}$</p>
<p>As we can see the combination $\sqrt{x^2+y^2+z^2}$ occurs very often. Is there a way I call this combination some other variable, say, w. I cannot use substitute in this case as I want to leave $x$,$y$ and $z$ alone if they dont come in this specific combination $\sqrt{x^2+y^2+z^2}$.
If it were a string I could have used find and replace. But I want to use the expression as a input later on in the notebook.</p>
<p>Edit: I came across a sympy command which is very close to what I want to do but not quite. There is a command cse in the module simplify. However, it identifies the subexpresssion by itself. As far as I could see it does not offer the flexibility for the users to identify the subexpression. Also for some reason sage expressions cannot be converted to sympy using <code>f._sympy_()</code> if there are "i"'s in the expression. </p>
<p>Thanks in advance. </p>
https://ask.sagemath.org/question/7750/is-there-any-way-i-can-substitute-a-combination-of-variables/?answer=11780#post-id-11780I figured out that the "subs" command in sympy is far more powerful than substitute in sage.
So I converted the sage expression into sympy expression using f=f._sympy_() then did the substitution I wanted to do and after the substitution converted to sage again using f=f._sage_(). The problem is that sympy gives a problem because it cannot recognize i. So I declared a variable name i and restored it in the end. Here is an example script. It would be awesome though if the sage substitute command worked like the sympy subs command.
from sympy import Symbol, cos, sympify, pprint
from sympy.abc import x
x=var('x')
z=var('z')
i=var('i')
f=x*x+x*i+1
view(f)
f=f._sympy_()
view(f)
f=f.subs(x*x,z)
f=f._sage_()
view(f)
restore('i')Thu, 11 Nov 2010 21:27:27 +0100https://ask.sagemath.org/question/7750/is-there-any-way-i-can-substitute-a-combination-of-variables/?answer=11780#post-id-11780Comment by d3banjan for <p>I figured out that the "subs" command in sympy is far more powerful than substitute in sage.
So I converted the sage expression into sympy expression using f=f._sympy_() then did the substitution I wanted to do and after the substitution converted to sage again using f=f._sage_(). The problem is that sympy gives a problem because it cannot recognize i. So I declared a variable name i and restored it in the end. Here is an example script. It would be awesome though if the sage substitute command worked like the sympy subs command.</p>
<pre><code>from sympy import Symbol, cos, sympify, pprint
from sympy.abc import x
x=var('x')
z=var('z')
i=var('i')
f=x*x+x*i+1
view(f)
f=f._sympy_()
view(f)
f=f.subs(x*x,z)
f=f._sage_()
view(f)
restore('i')
</code></pre>
https://ask.sagemath.org/question/7750/is-there-any-way-i-can-substitute-a-combination-of-variables/?comment=19939#post-id-19939would someone please format the code? Sun, 15 Apr 2012 10:46:15 +0200https://ask.sagemath.org/question/7750/is-there-any-way-i-can-substitute-a-combination-of-variables/?comment=19939#post-id-19939Answer by DSM for <p>I know this is a long short but I was wondering whether it is possible to substitute a combination of variables by something else. Let me explain it with an example.</p>
<p>Let $f(x,y,z) = f(x) \sqrt{x^2+y^2+z^2} + f(y) \sqrt{x^2+y^2+z^2} + f(z) \sqrt{x^2+y^2+z^2}$</p>
<p>As we can see the combination $\sqrt{x^2+y^2+z^2}$ occurs very often. Is there a way I call this combination some other variable, say, w. I cannot use substitute in this case as I want to leave $x$,$y$ and $z$ alone if they dont come in this specific combination $\sqrt{x^2+y^2+z^2}$.
If it were a string I could have used find and replace. But I want to use the expression as a input later on in the notebook.</p>
<p>Edit: I came across a sympy command which is very close to what I want to do but not quite. There is a command cse in the module simplify. However, it identifies the subexpresssion by itself. As far as I could see it does not offer the flexibility for the users to identify the subexpression. Also for some reason sage expressions cannot be converted to sympy using <code>f._sympy_()</code> if there are "i"'s in the expression. </p>
<p>Thanks in advance. </p>
https://ask.sagemath.org/question/7750/is-there-any-way-i-can-substitute-a-combination-of-variables/?answer=11781#post-id-11781It's occasionally a little flaky, but I think substitute_expression (subs_expr) can do what you want:
<pre><code>sage: var("x y z")
(x, y, z)
sage: var("w")
w
sage: f = function("f")
sage: s = sqrt(x*x+y*y+z*z)
sage: eq = f(x,y,z) == f(x) * s + f(y) * s + f(z) * s
sage: eq
f(x, y, z) == sqrt(x^2 + y^2 + z^2)*f(x) + sqrt(x^2 + y^2 + z^2)*f(y) + sqrt(x^2 + y^2 + z^2)*f(z)
sage: eq.subs_expr(sqrt(x*x+y*y+z*z)== w)
f(x, y, z) == w*f(x) + w*f(y) + w*f(z)
</code>
</pre>
Also, if you want to force a replace if subs_expr is being disobedient, you <i>could</i> treat it as a string and then evaluate it back. It's ugly, but there are times you might want to do something like this:
<pre><code>
sage: # ugly and dangerous! do not use!
sage: tmp = str(eq)
sage: tmp2 = tmp.replace(str(s), 'w')
sage: sage_eval(tmp2,locals=vars())
f(x, y, z) == w*f(x) + w*f(y) + w*f(z)
</code></pre>
but this can lead to problems when the string you're replacing is a substring of something else.Thu, 11 Nov 2010 22:19:11 +0100https://ask.sagemath.org/question/7750/is-there-any-way-i-can-substitute-a-combination-of-variables/?answer=11781#post-id-11781