Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110851] Re: replacement x->y except in Exp[x]
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Thu, 8 Jul 2010 07:41:02 -0400 (EDT)

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 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
>
>


  • Prev by Date: Re: Pattern: x_List with conditions on elements
  • Next by Date: Re: More flexibility in placement of ChartLegend
  • Previous by thread: Re: replacement x->y except in Exp[x]
  • Next by thread: Re: replacement x->y except in Exp[x]