       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:= {Sin[x + Exp[x]], Cos[x], x^2} /. {Exp[x] -> Exp[x], x -> y}

Out= {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:= Replace[{Sin[x + Exp[x]], Cos[x], x^2}, {Exp[x] -> Exp[x],
x -> y}, Infinity]

Out= {Sin[E^y + y], Cos[y], y^2}

Here is a more complicated pattern which will work both with ReplaceAll and
with Replace:

In:= {Sin[x + Exp[x]], Cos[x], x^2} /.
h_[left___, x,
right___] /; ! (h === Exp && {left} === {} && {right === {}}) :>
h[left, y, right]

Out= {Sin[E^x + y], Cos[y], y^2}

In:= Replace[{Sin[x + Exp[x]], Cos[x], x^2},
h_[left___, x,
right___] /; ! ((h ===
Power) && ({left} === {E}) && ({right} === {})) :>
h[left, y, right], Infinity]

Out= {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]