Ask Your Question
1

Substitution of several variables

asked 2016-03-05 05:36:51 -0500

mforets gravatar image

updated 2016-03-05 05:37:39 -0500

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))$ ).

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-03-05 06:58:22 -0500

tmonteil gravatar image

updated 2016-03-05 07:09:32 -0500

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.

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
edit flag offensive delete link more

Comments

1

Thanks! 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]}).

mforets gravatar imagemforets ( 2016-03-06 03:32:48 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-03-05 05:36:51 -0500

Seen: 113 times

Last updated: Mar 05 '16