MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Confusing behaviour with caching of evaluated expressions ?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg63741] Re: Confusing behaviour with caching of evaluated expressions ?
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Fri, 13 Jan 2006 04:48:17 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <dq53va$acd$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Chris Rodgers wrote:
> Hi,
> 
> Can anyone shed any light upon the following unexpected output from 
> Mathematica?
> 
> I have come across some rather strange behaviour in the way Mathematica 
> handles expressions which depend on a "flag" variable e.g.:
> 
> F[x_] := G /; flag==1
> 
> It appears that Mathematica forgets that flag is a variable that might 
> change in the future. Thus, if one defines another expression e.g.:
> 
> flag=0
> expr1= F[z1]
> expr2:=F[z2]
> 
> expr1 and expr2 will both currently evaluate to F[z1] and F[z2] as 
> expected. BUT, if we now change the flag and try to prod them into 
> evaluating to give G, things become tricky:
> 
> flag=1
> expr1
> expr2
> Evaluate[expr1]
> Evaluate[expr2]
> FullSimplify[expr1]
> FullSimplify[expr2]
> 
> Even doing something like
> 
> F[z1]
> expr1
> 
> doesn't work. (The F[z1] returns G correctly, but expr1 doesn't realise 
> that it should change to give G.)
> 
> The only way to make it work again seems to be to "change" z1 or z2. For 
> example,
> 
> z1=z1+0
> expr1
> 
> RandomFunctionIMadeUp[z2]^:=33
> expr2
> 
> These two assignments are enough to make Mathematica update its cache(?).
> 
> Can anyone explain to me why this might be "by design"?
> 
> Can anyone tell me a way to make Mathematica fully evaluate an 
> expression e.g. expr1 without getting stuck in this way? Is there a 
> SuperFullActuallyEvaluate[expr1] commmand?? :-)
> 
> Many thanks in advance,
> 
> Chris.
> 
Hi Chris,

Everything works as expected. Section "2.5.8 Immediate and Delayed 
Definitions" of _The Mathematica Book_ should explain it all 
(http://documents.wolfram.com/mathematica/book/section-2.5.8).

In[1]:=
F[x_] := G /; flag == 1

In[2]:=
flag = 0
expr1 = F[z1]
expr2 := F[z2]

Out[2]=
0

Out[3]=
F[z1]

In[5]:=
flag = 1
expr1
expr2

Out[5]=
1

Out[6]=
F[z1]

Out[7]=
G

In[8]:=
Clear[expr1, expr2]
flag = 0
expr1 := F[z1]
expr2 := F[z2]

Out[9]=
0

In[12]:=
flag = 1
expr1
expr2

Out[12]=
1

Out[13]=
G

Out[14]=
G

Best regards,
/J.M.


  • Prev by Date: Released Biokmod 3.3 (a tool box for biokinetic modeling)
  • Next by Date: Re: problem with numerical values in Solve/NSolve
  • Previous by thread: Re: Confusing behaviour with caching of evaluated expressions ?
  • Next by thread: Re: Confusing behaviour with caching of evaluated expressions ?