Re: ReplaceList and //.
- To: mathgroup at smc.vnet.net
- Subject: [mg73616] Re: ReplaceList and //.
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Fri, 23 Feb 2007 04:32:45 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <erjo1u$nn5$1@smc.vnet.net>
Mr Ajit Sen wrote:
> Dear Mathgroup,
>
> Could someone please enlighten me as to when I should
> use ReplaceList and when to use //. in pattern
> matching.
>
> Thus, if Y = a x^2/b^3, then
>
> ReplaceList[Y, a^m_. x^n_. b^p_. :> z
> b^(p + 1) a^(m - 1) x^(n - 1)][[1]]
>
> as well as
>
> Y //. a^m_. x^n_. b^p_. :> z b^(p + 1) a^(m - 1)
> x^(n - 1)
>
> give the same result.
>
> However,
>
> ReplaceList[a*b*c, {(x_)*(y_) -> x^y, (x_)*(y_)
> -> x + y}]
>
> and
> a*b*c //. {(x_)*(y_) :> x^y, (x_y)*_ :> x + y}
>
> yield different outputs.
>
> What am I missing here?
//. applies the transformation rule(s) once, as /. does. When done, //.
applies the transformation rule(s) on the resulting expression. If any
modifications have been made, //. applies again the rules the new
expression; otherwise it stops.
Also, keep in mind that /. and //. attempt to match the most general
patterns. For instance, a rule such as x_*y_, when applied to a*b*c,
will match only a*(b*c) (that is x -> a and y -> b*c) although there are
some other interpretations.
The built-in function Trace should help you to understand what is going on:
In[1]:=
Trace[a*b*c /. {(x_)*(y_) -> x^y, (x_)*(y_) -> x + y}]
Out[1]=
y
{a b c /. {(x_) (y_) -> x , (x_) (y_) -> x + y},
b c b c
Times[a ], a }
In[2]:=
Trace[a*b*c //. {(x_)*(y_) -> x^y, (x_y)*_ -> x + y}]
Out[2]=
{{{{(x_y) _, _ (x_y)}, _ (x_y) -> x + y,
_ (x_y) -> x + y},
y
{(x_) (y_) -> x , _ (x_y) -> x + y}},
y
a b c //. {(x_) (y_) -> x , _ (x_y) -> x + y},
c
b c b c b
{Times[a ], a }, a }
Regards,
Jean-Marc