Re: symbol replace
- To: mathgroup at smc.vnet.net
- Subject: [mg51244] Re: [mg51241] symbol replace
- From: "Maxim A. Dubinnyi" <maxim at nmr.ru>
- Date: Sun, 10 Oct 2004 01:57:15 -0400 (EDT)
- References: <200410090819.EAA09741@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi, Answer to the first question. You should temporary replace \!\(E\_0\) (Subscript[E,0]) with "E0" or other unused symbol, then perform substitution and finally replace "E0" back: In[1]= E*Subscript[E,0]/. Subscript[E,0]->"E0"/. E->Subscript[E,0]/. "E0"->Subscript[E, 0] Out[1]= Subscript[E, 0]^2 The following is general solution for this problem. Function "SubscriptProtectReplaceAll" is the same as Build-In function "ReplaceAll", but it preserves parts of Subscripted symbols from replacing: In[2]= SubscriptProtectReplaceAll[exprn_, rules_] := Module[{ProtectRules}, ProtectRules = Cases[ exprn, Subscript[A_,b_]:>(Subscript[A,b]->Unique[tmp]), {0, Infinity}, Heads->True ]; exprn/.ProtectRules/.rules/.(Reverse/@ProtectRules) ] In[3]= SubscriptProtectReplaceAll[ E*Subscript[E,0], E->Subscript[E,0] ] Out[3]= Subscript[E,0]^2; More sophisticated example: In[4]= \!\(SubscriptProtectReplaceAll[A+A\_2+A\_A+A\_\(A\_3\)+A\_4[x], A->A\_1]\) Out[4]= \!\(A\_1+A\_2+A\_A+A\_\(A\_3\)+A\_4[x]\) The following code permanently substitutes "ReplaceAll" with "SubscriptProtectReplaceAll": In[5]= Unprotect[ReplaceAll]; SubscriptProtect[ReplaceAll]=True; (exprn_ /. rules_)/;SubscriptProtect[ReplaceAll]:= Block[{SubscriptProtect}, SubscriptProtect[ReplaceAll]=False; SubscriptProtectReplaceAll[exprn, rules] ]; Protect[ReplaceAll]; Now you can use ordinary substitution symbol "/.": In[6]= \!\(A+A\_2+A\_A+A\_\(A\_3\)+A\_4[x]/.A->A\_1\) Out[6]= \!\(A\_1+A\_2+A\_A+A\_\(A\_3\)+A\_4[x]\) Resetting In[7]= SubscriptProtect[ReplaceAll]=False; Will return you to ordinary substitution rules. Sincerely, Maxim A. Dubinnyi symbio wrote: >I am facing 2 problems with Mathematica: > 1st problem) >With replace all command, when I use it with subscripts I get an unintended >effect, this is what I do: >E * (Eo) /. E -> (Eo) >I intend to replace only E with Eo, but Mathematica instead replaces all E's >with (Eo) and I get this instead: >(Eo) * (Eo)o >Please note the 'o' in Eo is supposed to be a subscript here and reads Enot. >Can anyone please help? >here is the Mathematica code: > >In[1]:= >\!\(E\ *\ E\_0\ /. \ E -> \ E\_0\) >Out[1]= >\!\(\[ExponentialE]\_0\ \((\[ExponentialE]\_0)\)\_0\) > >2nd problem) >I like to use descriptive names for my variables with Underscore to separate >the names, it's really easier to read, but of course underscore is a special >character in Mathematica unfortunately, so is there an alternative?? For >example, I like to use variable Dynamic_Energy_of_Stars_With_Sand = 123; Is >there something else equally as obvious as Underscore that maybe used for >purpose of separting words in a variables names?? > >Thanks a lot for your help > > >
- Follow-Ups:
- Re: Re: symbol replace
- From: Delfino Matteo <delfino@studenti.ph.unito.it>
- Re: Re: symbol replace
- References:
- symbol replace
- From: "symbio" <symbio@h0tmail.com>
- symbol replace