Re: MapAt changes its response towards different expressions
- To: mathgroup at smc.vnet.net
- Subject: [mg55523] Re: MapAt changes its response towards different expressions
- From: Maxim <ab_def at prontomail.com>
- Date: Sun, 27 Mar 2005 02:43:24 -0500 (EST)
- References: <d20qme$b7f$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Fri, 25 Mar 2005 10:55:10 +0000 (UTC), Jacco van Beek <jabe at nmr.phys.chem.ethz.ch> wrote: > I have noticed that when I use MapAt to evaluate similar expressions of > form MapAt[Evaluate, Hold[Expression], Coordinates] that Mathematica > (version 5.1, Solaris) somehow executes these differently. The following > example from below demonstrates the problem: > > I have defined a function MXP that produces an output statement and > furthermore returns the input expression. I also define three similar > expressions TRS which are all on Hold and which are of form > Hold[a*MXP[b]+c], where a, b and c are either numbers or expressions. I > replace all occurences of Times in the TRS expressions by a temporary > function TempTimes, which is not defined. Then I locate the position of > the TempTimes within the TRS expressions, and use this as coordinates > for the MapAt function. The following example shows a different > execution in the second example even though the expression is very > similar to the other two. Somehow, in the second example the call to MXP > is evaluated, thus causing the output "Hello", and an error in my real > application of a similar construct. > > Is this normal behaviour or is this a bug? It's easily solved by adding > a 0 to all terms but I'd like to know what's causing this. > > Jacco van Beek > ETH Zuerich > > > Sample code: > > > $Pre=.; > MXP[xxx_] := Module[{}, Print["Hello"]; Return[xxx]]; > > TRS = Hold[MXP[SPR[1,x]SPR[2,y]]]//.{Times :> TempTimes} > Position[TRS, _TempTimes] > MapAt[Evaluate, TRS, Position[TRS, _TempTimes]] > > Out[47]= Hold[MXP[TempTimes[SPR[1,x],SPR[2,y]]]] > Out[48]= {{1,1}} > Out[49]= Hold[MXP[Evaluate[TempTimes[SPR[1,x],SPR[2,y]]]]] > > > > TRS = Hold[3*MXP[SPR[1,x]SPR[2,y]]]//.{Times :> TempTimes} > Position[TRS, _TempTimes] > MapAt[Evaluate, TRS, Position[TRS, _TempTimes]] > > Out[50]= Hold[TempTimes[3,MXP[TempTimes[SPR[1,x],SPR[2,y]]]]] > Out[51]= {{1,2,1},{1}} > From In[50]:= Hello > Out[52]= Hold[TempTimes[3,TempTimes[SPR[1,x],SPR[2,y]]]] > > > > TRS = Hold[3*MXP[SPR[1,x]SPR[2,y]]+0]//.{Times :> TempTimes} > Position[TRS, _TempTimes] > MapAt[Evaluate, TRS, Position[TRS, _TempTimes]] > > Out[53]= Hold[TempTimes[3,MXP[TempTimes[SPR[1,x],SPR[2,y]]]]+0] > Out[54]= {{1,1,2,1},{1,1}} > Out[55]= > Hold[Evaluate[TempTimes[3,MXP[Evaluate[TempTimes[SPR[1,x],SPR[2,y]]]]]]+0] > Essentially your question is why Evaluate in Hold[Evaluate[f[x]]] and Hold[f[Evaluate[x]]] works differently. See http://forums.wolfram.com/mathgroup/archive/2005/Feb/msg00404.html for an explanation. And when you change TempTimes[a, b] to TempTimes[a, b] + 0 you push TempTimes one level deeper. Maxim Rytin m.r at inbox.ru