       Re: Replace in an elegant way

• To: mathgroup at smc.vnet.net
• Subject: [mg122887] Re: Replace in an elegant way
• From: Heike Gramberg <heike.gramberg at gmail.com>
• Date: Mon, 14 Nov 2011 07:08:01 -0500 (EST)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• References: <201111130943.EAA29993@smc.vnet.net>

```You can actually do something like this:

((1 + theta[m])^2)/(1 - m) -
lambda*((theta[
m] + ((lambda + m - lambda*m)*(1 + theta[m])*(1 + theta[m] -
lambda*theta[m]))/(1 - m) + (m*Derivative[theta][m])/
lambda)) /. {theta'[m] -> theta'[m], theta[m] -> theta[m], m -> dd}

This works because ReplaceAll compares each part of the original expression with the elements in the replacement list in turn until the first match is found. As soon as a match is found, that part nor any of its subparts are checked further. So for example, if a match for theta[m] is found in the example above, it's replaced with theta[m] and none of its subparts (i.e. m inside the brackets) are checked against the replacement list.

Heike.

On 13 Nov 2011, at 10:43, Mirko wrote:

> Hi all,
> I have following equation:
>
> ((1 + theta[m])^2)/(1 - m) - lambda*((theta[m] + ((lambda + m - lambda*m)*(1 + theta[m])*(1 + theta[m] - lambda*theta[m]))/(1 - m) +
>     (m*Derivative[theta][m])/lambda))
>
> I want to replace all m that are not an argument of theta[m] or the Derivative. However, if I use
> /., it replaces all m. If I use Replace[expr,m->qq] it doesn't replace anything. (qq is the one I want to replace with). If I specify any level, it doesn't change anything, unless I use -1, but then I replace everything.
>
> Do you know how I solve this problem?
> Right now I use:
> //. Derivative[y_][theta][m] -> Derivative[y][theta][dd] //.
>    theta[m] -> theta[dd] /. m -> qq //.
>  Derivative[y_][theta][dd] -> Derivative[y][theta][m] //.
> theta[dd] -> theta[m]
>
> but this is not elegant at all (and takes slightly longer).
>

```

• Prev by Date: Re: Replace in an elegant way
• Next by Date: Re: 2D Plot for a face of a 3D plot box?
• Previous by thread: Re: Replace in an elegant way
• Next by thread: Re: Replace in an elegant way