Re: Evaluate/Module
- To: mathgroup at smc.vnet.net
- Subject: [mg16079] Re: Evaluate/Module
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Thu, 25 Feb 1999 08:24:47 -0500
- References: <7atq9s$7kr@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Bernd Brandt wrote in message <7atq9s$7kr at smc.vnet.net>... >Hi, > >I'm having some trouble with Evaluate and i cannot find this in the >documentation. >Does anybody know what and esp. why the following is happening? > > In1:= tmp = x^2 +1; > > In2:= Module[{x=4},tmp=tmp-1;Print[tmp]; Evaluate[tmp]] > > x^2 > x^2 + 1 > >In the above Evaluate[tmp] somehow takes the latest value of tmp and not >the current one after the "tmp=tmp-1". Why? > >Changing "Evaluate[tmp] to "l=Evaluate[tmp]" does give the right answer >(in this case x^2) immediately. Does this assignment force Evaluate to >Evaluate tmp before ending the Module. Does someone know more about >Evaluate and Modelule? > > lM[x0_]:= Module[{lonediff,lMdiff,ldead,lone,search}, > lonediff= 1 + lone/2 - x0/lone - 2 / (1 + Exp[2*ldead - 2*lone]); > lMdiff = (2*(x0/(1+x0) )*E^(-ldead+ lone))/(E^(2*lone-2*ldead)+1)- >0.1; > search=FindRoot[{lMdiff==0, lonediff==0 },{ldead,x0+1} ,{lone,x0}]; > Evaluate[ldead /. search] > ]; > > lM[6] > > ReplaceAll::"reps": > {search$77} is neither a list of replacement rules nor a valid > dispatch table, and so cannot be used for replacing." > > 5.44865 > >In this module Evaluate somehow is looking for an global "search" >variable. >Why does it not just evaluate this local "search"? > >Thanks, > >Bernd ---------------------------------------------------------------------------- --------------------------------------- Neither problem invoves Module; both are due to how expressions like e1;e2;e3;e4 (FullForm CompoundExpression[e1,e2, e3,e4] evaluate Normally, CompoundExpression evaluates its entries in sequence and then gives the value of the last one. However, since it has the attribute HoldAll this evaluation is under the control of the programmer and if some entries are wrapped in Evaluate[ ] they will be evaluated first *and* the the resulting expression will then be evaluated as usual So, writing ei* for the value of ei, we get the following steps CompoundExpression[e1, Evaluate[e2], Evaluate[e3], e4] CompoundExpression[e1, e2*, Evaluate[e3], e4] CompoundExpression[e1, e2*, e3*, e4] (*restart!*) CompoundExpression[e1*, e2*, e3*, e4] CompoundExpression[e1*, e2**, e3*, e4] CompoundExpression[e1*, e2**, e3**, e4] CompoundExpression[e1*, e2**, e3**, e4*] e4* In your examples, simply deleting Evaluate will make them work as expected. Incidentally, in your code tmp = x^2 +1; Module[{x=4}, tmp=tmp-1;Print[tmp]; Evaluate[tmp]] Module has no effect: - the x are renamed x$n (where n is the ModuleNumber); but this is done before the body tmp=tmp-1;Print[tmp]; Evaluate[tmp] is evaluated, so, only the displayed x is renamed. - then the assigment x$n = 4 is made. - after this the body evaluates to x^2+1 but this involves x, not x$n, so the assignment has no effect. However, try wrapping Evaluate around the body.... Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565