Re: replacement x->y except in Exp[x]

*To*: mathgroup at smc.vnet.net*Subject*: [mg110871] Re: replacement x->y except in Exp[x]*From*: Andrzej Kozlowski <akozlowski at gmail.com>*Date*: Fri, 9 Jul 2010 07:03:38 -0400 (EDT)*References*: <i14aij$g0p$1@smc.vnet.net> <201007090034.UAA21449@smc.vnet.net>

On 9 Jul 2010, at 09:34, AES wrote: > In article <i14aij$g0p$1 at smc.vnet.net>, Bob Hanlon <hanlonr at cox.net> > wrote: > >> expr = a*x + b*x^2 - c*Exp[x]; >> >> expr /. {Exp[x] -> z, x -> y} /. z -> Exp[x] >> >> a*y + b*y^2 - c*E^x >> >> >> Bob Hanlon >> >> ---- Sebastian <sebhofer at gmail.com> wrote: >> >> ============= >> Is it possible to exclude certain patterns from replacements? >> For example I want to replace y for x everywhere except in Exp[x]. >> >> TIA Sebastian > > > Is it ever a good idea to use ReplaceAll to do word-processing-type > "global find and replace" changes like this? > > Lengthy threads on this NG have pointed out the havoc that this can > wreak if the symbol I is among those replaced, particularly if one > attempts to do I -> -I. > > Or is I the _only_ symbol in the entire Mathematica vocabulary that > encounters this difficulty? > > (I really would like to know the answer to this question, even if this > is entirely out of curiosity, not any real need. I've asked it > previously, and never gotten an answer.) > You have asked this many times and the reason why you have not received a satisfactory answer is probably that you are asking the wrong question. Many people have tried to explain to you about FullForm of expressions but seemingly without any effect. Certain expressions in Mathematica change their FullForm as a result of evaluation. For example, the FullForm of the symbol I becomes converted to Complex[0,1]. While, I think, this is the only symbol to which this happens, other expressions also undergo this sort of conversion, notably 2/3 will be converted to Rational[2,3]. If you are unwilling to remember this you can always avoid this problem at the const of making your pattern matching take up more characters, as you will need to insert Unevaluated and HoldPattern as follows. Suppose in the expression 2/3 I + x/y I you wish to replace all fractions (that is 2/3 and x/y) by r and I by d. Without worrying about evaluation you can do this as follows: Unevaluated[Unevaluated[2/3 I + x/y I] /. HoldPattern[x_/y_] -> r] /. HoldPattern[I] -> d 2 d r If you allow the expression to evaluate the patterns will no longer match. For example, with only one Unevaluated you will get Unevaluated[(2/3)*I + (x/y)*I] /. HoldPattern[(x_)/(y_)] -> r /. HoldPattern[I] -> d 2*I*r Without any you will get: (2/3)*I + (x/y)*I /. HoldPattern[(x_)/(y_)] -> r /. HoldPattern[I] -> d r + (2*I)/3 All this is perfectly reasonable, logical and a great deal easier than almost anything in an undergraduate math syllabus at a reasonable university.

**References**:**Re: replacement x->y except in Exp[x]***From:*AES <siegman@stanford.edu>