Re: Confused about replacement rules
- To: mathgroup at smc.vnet.net
- Subject: [mg7183] Re: Confused about replacement rules
- From: "Xah Lee" <xah at best.com>
- Date: Wed, 14 May 1997 01:11:17 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
In article <5l8tjg$cs7$1 at dragonfly.wolfram.com>, bleile at fli.sh.bosch.de (Thomas Bleile) wrote: > In[24]:= rules = {x_ ^ 2->a,expr_:>Expand[expr]} > 2 > Out[24]= {(x_) -> a, expr_ :> Expand[expr]} > > Now I apply these rules on a simple expression: > > In[25]:= x (x+b)/.rules > > 2 > Out[25]= b x + x > >Why isn't x^2 substituted by a ? The reason x (x+b)/.{x_ ^ 2->a,expr_:>Expand[expr]} returns b x + x^2 instead of expected b x + a is well explain in the Appendix A.10 of ReplaceAll entry in the Mma book. It says: "The first rule that applies to a particular part is used; no further rules are tried on that part, or any of its subparts". What you might want to do is Expand at expr /. {x_?AtomQ^2->a} Xah xah at best.com, http://www.best.com/~xah/ Symantec Visual Cafe. Windows95/NT. CD-ROM. Shrinkwrapped. $50. email me.