Prev: Problem with a diagram
Next: First nonzero in list
From: Sebastian on 8 Jul 2010 03:12 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
From: Bob Hanlon on 8 Jul 2010 06:50 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(a)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
From: Leonid Shifrin on 8 Jul 2010 07:40 Hi Sebastian, the simplest in such cases is to use a first rule that does nothing when expression is what you don't want to change, Exp[x] in this case: In[1]:= {Sin[x + Exp[x]], Cos[x], x^2} /. {Exp[x] -> Exp[x], x -> y} Out[1]= {Sin[E^x + y], Cos[y], y^2} Be aware though that if you use Replace, this will not work since Replace works from the bottom of the expression: In[2]:= Replace[{Sin[x + Exp[x]], Cos[x], x^2}, {Exp[x] -> Exp[x], x -> y}, Infinity] Out[2]= {Sin[E^y + y], Cos[y], y^2} Here is a more complicated pattern which will work both with ReplaceAll and with Replace: In[3]:= {Sin[x + Exp[x]], Cos[x], x^2} /. h_[left___, x, right___] /; ! (h === Exp && {left} === {} && {right === {}}) :> h[left, y, right] Out[3]= {Sin[E^x + y], Cos[y], y^2} In[4]:= Replace[{Sin[x + Exp[x]], Cos[x], x^2}, h_[left___, x, right___] /; ! ((h === Power) && ({left} === {E}) && ({right} === {})) :> h[left, y, right], Infinity] Out[4]= {Sin[E^x + y], Cos[y], y^2} Regards, Leonid On Thu, Jul 8, 2010 at 11:12 AM, Sebastian <sebhofer(a)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 > >
From: AES on 8 Jul 2010 20:34 In article <i14aij$g0p$1(a)smc.vnet.net>, Bob Hanlon <hanlonr(a)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(a)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.)
From: Andrzej Kozlowski on 9 Jul 2010 07:03
On 9 Jul 2010, at 09:34, AES wrote: > In article <i14aij$g0p$1(a)smc.vnet.net>, Bob Hanlon <hanlonr(a)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(a)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. |