Processing math: 100%

First time here? Check out the FAQ!

Ask Your Question
3

Substitution of several variables

asked 8 years ago

mforets gravatar image

updated 8 years ago

Let f=f(x1(t),x2(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=x1(t)μx1(t)2x2(t).

Now, I need to to substitute the term containing x21(t)x2(t) in f by some new auxiliary variable x3(t), that is, to obtain f=x1(t)μx3(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 ˙x(t)=f(x(t)) ).

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
5

answered 8 years ago

tmonteil gravatar image

updated 8 years ago

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
Preview: (hide)
link

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 μ (more precisely, what if we had instead 2μ?), doing w0=SR.wild(0) and then f.subs({w0*x[0]^2*x[1] : w0*x[2]}).

mforets gravatar imagemforets ( 8 years ago )

Your Answer

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

Add Answer

Question Tools

2 followers

Stats

Asked: 8 years ago

Seen: 1,582 times

Last updated: Mar 05 '16