MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: algebraic substitution rules

  • To: mathgroup at
  • Subject: [mg30737] Re: [mg30716] algebraic substitution rules
  • From: Andrzej Kozlowski <andrzej at>
  • Date: Sun, 9 Sep 2001 03:26:26 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

It is obvious why the first case works, so I shall only explain why the 
second does not. First, a kind of digression. Actually the rule that you 
used is in any case probably not the one you meant. In fact it is 
equivalent to:

1+x^2+x^3+x^4 /. {x^2->1+x ,x^3->x(1+x) ,x^4->(1+x)^2,a_->a}

The reason for this is that you are using Rule rather than 
RuleDelayed (:>) so a_->Expand[a] simply evaluates to a_->a. Presumably 
you meant to use :>. There are cases where that would make a difference. 
For example compare:


(1 + x)


              2    3
1 + 3 x + 3 x  + x

But in fact yours is not one of them and the difference between :> 
and -> plays no role here.

To see what goes wrong let's look at two examples, similar to yours.


f[x] /. {x -> a, z_ -> z}



f[x] /. {f[x] -> a, z_ -> z}


Why does the first case fail while the second does not?

In cases like this you first look at the FullForm of your expression:

FullForm[Hold[f[x] /. {f[x] -> a, z_ -> z}]]


Next you read the documentation for ReplaceAll. The relevant passage is:

ReplaceAll looks at each part of expr, tries all the rules on it, and 
then goes on to the next part of expr. The first rule that applies to a 
particular part is used; no further rules are tried on that part, or on 
any of its subparts.

So now everything is clear. The rule z_->z matches everything. When 
f[x] /. {x -> a, z_ -> z}
  was evaluated the Pattern Matcher first tried to match f[x] using the 
rule x->a and failed. Then it tried to match it using z_->z and 
succeeded. At this point it finished its job since as the documantation 
tells you "no further rules are tried on that part, or on any of its 
subparts. "

In the second case f[x] /. {f[x] -> a, z_ -> z} f[x] was matched using 
f[x]->a and that was that. The same thing happened of course in your 
original case.

Andrzej Kozlowski
Toyama International University

On Saturday, September 8, 2001, at 03:55  PM, Cattiaux Isabelle wrote:

> Hi,
>   Could someone tell me why the first substitution rule
>  works and the second doesn't
> In[1]:==
> 1+x^2+x^3+x^4 /. {x^2->1+x ,x^3->x(1+x) ,x^4->(1+x)^2}
> Out[1]==
> 2 + x + x(1 + x)+ (1 + x)^2
> In[78]:==
> 1+x^2+x^3+x^4 /. {x^2->1+x ,x^3->x(1+x) ,x^4->(1+x)^2,a_->Expand[a]}
> Out[78]==
> 1 + x^2 + x^3 + x^4
> --
> Isabelle Cattiaux-Huillard
> Universite de Valenciennes

  • Prev by Date: Re: algebraic substitution rules
  • Next by Date: Re: Seeking Mathematica's faults
  • Previous by thread: Re: algebraic substitution rules
  • Next by thread: Re: Substitutions[rookie]