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, 06 Mar 2016 10:32:48 +0100Substitution of several variableshttps://ask.sagemath.org/question/32720/substitution-of-several-variables/
Let $f=f(x_1(t),x_2(t))$ be defined as follows:
sage: var('t mu')
sage: x=list()
sage: x.append( function('x1')(t) )
sage: x.append( function('x2')(t) )
sage: f = x[0] - mu*x[0]^2*x[1]
sage: f
$f = x_1(t) - \mu x_1(t)^2 x_2(t)$.
Now, I need to to substitute the term containing $x_1^2(t) x_2(t)$ in $f$ by some new auxiliary variable $x_3(t)$, that is, to obtain $f = x_1(t) - \mu x_3(t) $. However, this code doesn't work:
sage: x.append( function('x3')(t) )
sage: f.subs({x[0]^2*x[1] : x[2]})
Some ideas? Thanks.
This code is motivated by symbolic manipulation with ODEs (think of $f$ as being the right-hand side term in the autonomous ODE system $\dot{x}(t) = f(x(t))$ ).Sat, 05 Mar 2016 12:36:51 +0100https://ask.sagemath.org/question/32720/substitution-of-several-variables/Answer by tmonteil for <p>Let $f=f(x_1(t),x_2(t))$ be defined as follows:</p>
<pre><code>sage: var('t mu')
sage: x=list()
sage: x.append( function('x1')(t) )
sage: x.append( function('x2')(t) )
sage: f = x[0] - mu*x[0]^2*x[1]
sage: f
</code></pre>
<p>$f = x_1(t) - \mu x_1(t)^2 x_2(t)$. </p>
<p>Now, I need to to substitute the term containing $x_1^2(t) x_2(t)$ in $f$ by some new auxiliary variable $x_3(t)$, that is, to obtain $f = x_1(t) - \mu x_3(t) $. However, this code doesn't work: </p>
<pre><code>sage: x.append( function('x3')(t) )
sage: f.subs({x[0]^2*x[1] : x[2]})
</code></pre>
<p>Some ideas? Thanks.</p>
<p>This code is motivated by symbolic manipulation with ODEs (think of $f$ as being the right-hand side term in the autonomous ODE system $\dot{x}(t) = f(x(t))$ ).</p>
https://ask.sagemath.org/question/32720/substitution-of-several-variables/?answer=32721#post-id-32721You should think of a symbolic expression as a tree. Unfortunately, the substitution only works for substituing pieces of expression that are subtrees of the given expression.
In the present case:
sage: f.operator()
<function add_vararg at 0x7f52b4cdaf50>
sage: f.operands()
[-mu*x1(t)^2*x2(t), x1(t)]
sage: f.operands()[0].operator()
<function mul_vararg at 0x7f52b4d00050>
sage: f.operands()[0].operands()
[mu, x1(t)^2, x2(t), -1]
So you can think of `f` as the tree:
__ mu
/
/ __ x1(t)^2
| /
* ____ x2(t)
/ \
+ \__ -1
\
\
\__ x1(t)
Somehow, it is like `mu, x1(t)^2, x2(t), -1` are tied together by a single `*` (to benefit from the associativity of the product). So, the smallest subtree that contains `x1(t)^2*x2(t)` is actually `-mu*x1(t)^2*x2(t)`, and you can see that the following works:
sage: f.subs({-mu*x[0]^2*x[1] : x[2]})
x1(t) + x3(t)
So, what you can do is to substitute the subtree by taking into account what you had to add to get the entire subtree (in this case, you have to multiply by `-mu`):
sage: f.subs({-mu*x[0]^2*x[1] : -mu*x[2]})
-mu*x3(t) + x1(t)
Remark: the tree i pictured above can of course be further developped, for example `x1(t)^2` is actually:
x1 __ t
/
^
\
2
so that the complete tree representing `f` is actually:
___ mu
/ x1 __ t
/ /
| ______ ^
| / \
| / 2
| /
* ____ x2 __ t
/ \
+ \__ -1
\
\
\__ x1 __ t
Sat, 05 Mar 2016 13:58:22 +0100https://ask.sagemath.org/question/32720/substitution-of-several-variables/?answer=32721#post-id-32721Comment by mforets for <p>You should think of a symbolic expression as a tree. Unfortunately, the substitution only works for substituing pieces of expression that are subtrees of the given expression.</p>
<p>In the present case:</p>
<pre><code>sage: f.operator()
<function add_vararg at 0x7f52b4cdaf50>
sage: f.operands()
[-mu*x1(t)^2*x2(t), x1(t)]
sage: f.operands()[0].operator()
<function mul_vararg at 0x7f52b4d00050>
sage: f.operands()[0].operands()
[mu, x1(t)^2, x2(t), -1]
</code></pre>
<p>So you can think of <code>f</code> as the tree:</p>
<pre><code> __ mu
/
/ __ x1(t)^2
| /
* ____ x2(t)
/ \
+ \__ -1
\
\
\__ x1(t)
</code></pre>
<p>Somehow, it is like <code>mu, x1(t)^2, x2(t), -1</code> are tied together by a single <code>*</code> (to benefit from the associativity of the product). So, the smallest subtree that contains <code>x1(t)^2*x2(t)</code> is actually <code>-mu*x1(t)^2*x2(t)</code>, and you can see that the following works:</p>
<pre><code>sage: f.subs({-mu*x[0]^2*x[1] : x[2]})
x1(t) + x3(t)
</code></pre>
<p>So, what you can do is to substitute the subtree by taking into account what you had to add to get the entire subtree (in this case, you have to multiply by <code>-mu</code>):</p>
<pre><code>sage: f.subs({-mu*x[0]^2*x[1] : -mu*x[2]})
-mu*x3(t) + x1(t)
</code></pre>
<p>Remark: the tree i pictured above can of course be further developped, for example <code>x1(t)^2</code> is actually:</p>
<pre><code> x1 __ t
/
^
\
2
</code></pre>
<p>so that the complete tree representing <code>f</code> is actually:</p>
<pre><code> ___ mu
/ x1 __ t
/ /
| ______ ^
| / \
| / 2
| /
* ____ x2 __ t
/ \
+ \__ -1
\
\
\__ x1 __ t
</code></pre>
https://ask.sagemath.org/question/32720/substitution-of-several-variables/?comment=32724#post-id-32724Thanks! I have benefited from your answer. Also in this site I found that the "wild cards" can be used to avoid defining the $\mu$ (more precisely, what if we had instead $2\mu$?), doing `w0=SR.wild(0)` and then `f.subs({w0*x[0]^2*x[1] : w0*x[2]})`.Sun, 06 Mar 2016 10:32:48 +0100https://ask.sagemath.org/question/32720/substitution-of-several-variables/?comment=32724#post-id-32724