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: [mg63740] Re: [mg63727] Confusing behaviour with caching of evaluated expressions ?
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Fri, 13 Jan 2006 04:48:17 -0500 (EST)
  • Reply-to: hanlonr at cox.net
  • Sender: owner-wri-mathgroup at wolfram.com

Presumably it is done to make evaluations more efficient.

SuperFullActuallyEvaluate[expr_]:=
    expr//ToString//ToExpression;

Clear[F,flag];

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

expr1=F[z1];
expr2:=F[z2];

flag=0;
{F[z1],F[z2],expr1,expr2}//
  SuperFullActuallyEvaluate

{F[z1],F[z2],F[z1],F[z2]}

flag=1;
{F[z1],F[z2],expr1,expr2}//
  SuperFullActuallyEvaluate

{G,G,G,G}

flag=0;
{F[z1],F[z2],expr1,expr2}//
  SuperFullActuallyEvaluate

{F[z1],F[z2],F[z1],F[z2]}


Bob Hanlon

> 
> From: Chris Rodgers <rodgers at physchem.NOSPAMox.aREMOVEc.uk>
To: mathgroup at smc.vnet.net
> Subject: [mg63740] [mg63727] Confusing behaviour with caching of evaluated 
expressions ?
> 
> 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.
> 
> 


  • Prev by Date: Re: A question about pattern-matching
  • Next by Date: Re: A question about pattern-matching
  • Previous by thread: Re: Confusing behaviour with caching of evaluated expressions ?
  • Next by thread: problem with numerical values in Solve/NSolve