Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #6427 > unrolled thread

Re: Replace in an elegant way

Started byBob Hanlon <hanlonr357@gmail.com>
First post2011-11-13 12:58 +0000
Last post2011-11-16 09:50 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Replace in an elegant way Bob Hanlon <hanlonr357@gmail.com> - 2011-11-13 12:58 +0000
    Re: Replace in an elegant way Peter Pein <petsie@dordos.net> - 2011-11-14 12:15 +0000
      Re: Replace in an elegant way "Dr. Wolfgang Hintze" <weh@snafu.de> - 2011-11-16 09:50 +0000

#6427 — Re: Replace in an elegant way

FromBob Hanlon <hanlonr357@gmail.com>
Date2011-11-13 12:58 +0000
SubjectRe: Replace in an elegant way
Message-ID<j9oett$1lp$1@smc.vnet.net>
You don't need to use multiple RepaceRepeated just ReplaceAll; and
replacing dd with m does not require multiple special handling cases

expr = ((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[1][theta][m])/lambda));

Your original

expr2 = expr //.
       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];

Streamlined

expr3 = expr /.
    {Derivative[y_][theta][m] -> Derivative[y][theta][dd],
     theta[m] -> theta[dd], m -> qq} /. dd -> m;

More compactly

expr4 = expr /.
    {theta'[m] -> theta'[dd], theta[m] -> theta[dd],
     m -> qq} /.dd -> m;

expr2 == expr3 == expr4

True


Bob Hanlon


On Sun, Nov 13, 2011 at 4:43 AM, Mirko <dashiell@web.de> 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[1][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).
>

[toc] | [next] | [standalone]


#6448

FromPeter Pein <petsie@dordos.net>
Date2011-11-14 12:15 +0000
Message-ID<j9r0pt$bjr$1@smc.vnet.net>
In reply to#6427
Am 13.11.2011 13:58, schrieb Bob Hanlon:
> You don't need to use multiple RepaceRepeated just ReplaceAll; and
> replacing dd with m does not require multiple special handling cases
>
> expr = ((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[1][theta][m])/lambda));
>
> Your original
>
> expr2 = expr //.
>         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];
>
> Streamlined
>
> expr3 = expr /.
>      {Derivative[y_][theta][m] ->  Derivative[y][theta][dd],
>       theta[m] ->  theta[dd], m ->  qq} /. dd ->  m;
>
> More compactly
>
> expr4 = expr /.
>      {theta'[m] ->  theta'[dd], theta[m] ->  theta[dd],
>       m ->  qq} /.dd ->  m;
>
> expr2 == expr3 == expr4
>
> True
>
>
> Bob Hanlon
>
>
> On Sun, Nov 13, 2011 at 4:43 AM, Mirko<dashiell@web.de>  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[1][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).
>>
>

It's a matter of taste.

I like:
expr //. f_[a___, m, b___] /; FreeQ[f, theta] :> f[a, qq, b]

Peter

[toc] | [prev] | [next] | [standalone]


#6475

From"Dr. Wolfgang Hintze" <weh@snafu.de>
Date2011-11-16 09:50 +0000
Message-ID<ja011k$647$1@smc.vnet.net>
In reply to#6448
Peter,

I like this one most.

I wanted to do something with FreeQ but I didn't find the construct.

Regards,
Wolfgang

"Peter Pein" <petsie@dordos.net> schrieb im Newsbeitrag 
news:j9r0pt$bjr$1@smc.vnet.net...
> Am 13.11.2011 13:58, schrieb Bob Hanlon:
>> You don't need to use multiple RepaceRepeated just ReplaceAll; and
>> replacing dd with m does not require multiple special handling cases
>>
>> expr = ((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[1][theta][m])/lambda));
>>
>> Your original
>>
>> expr2 = expr //.
>>         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];
>>
>> Streamlined
>>
>> expr3 = expr /.
>>      {Derivative[y_][theta][m] ->  Derivative[y][theta][dd],
>>       theta[m] ->  theta[dd], m ->  qq} /. dd ->  m;
>>
>> More compactly
>>
>> expr4 = expr /.
>>      {theta'[m] ->  theta'[dd], theta[m] ->  theta[dd],
>>       m ->  qq} /.dd ->  m;
>>
>> expr2 == expr3 == expr4
>>
>> True
>>
>>
>> Bob Hanlon
>>
>>
>> On Sun, Nov 13, 2011 at 4:43 AM, Mirko<dashiell@web.de>  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[1][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).
>>>
>>
>
> It's a matter of taste.
>
> I like:
> expr //. f_[a___, m, b___] /; FreeQ[f, theta] :> f[a, qq, b]
>
> Peter
> 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web