MathGroup Archive 2012

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

Search the Archive

Re: Fine control of evaluation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg126481] Re: Fine control of evaluation
  • From: Ingolf Dahl <ingolf.dahl at telia.com>
  • Date: Sun, 13 May 2012 03:03:42 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201205110413.AAA23689@smc.vnet.net>

I have found that the following simple construction can be useful

EvaluateIf[test_ /; test, expression_] := expression
SetAttributes[EvaluateIf, HoldAll]

If test is True, it acts like If, but if the test is not true, the
expression, with the EvaluateIf as a wrapper, is returned unevaluated. It
can be evaluated later, when the value of test has changed to True. Example:

In[19]:= p=EvaluateIf[NumberQ[x],x+x]
Out[19]= EvaluateIf[NumberQ[x],x+x]

In[20]:= p
Out[20]= EvaluateIf[NumberQ[x],x+x]

In[21]:= x=4
Out[21]= 4

In[22]:= p
Out[22]= 8

The function EvaluateIf can also be used in pure functions to check the type
of the variable before the function is applied, and also as a wrapper when
one wants to avoid a symbolic evaluation of a numeric function.

Best regards

Ingolf Dahl


> -----Original Message-----
> From: Brentt [mailto:brenttnewman at gmail.com]
> Sent: den 11 maj 2012 06:14
> To: mathgroup at smc.vnet.net
> Subject: Fine control of evaluation
> 
> Hello, I am struggling with evaluation control and was hoping someone
might point me in
> the right direction. I vaguely remember seeing something in the manual or
the tutorials
> addressing this issue, but I can't find the section I saw it in.
> 
> How does one get fine control over things wrapped in Hold and HoldForm.
Say I have
> some functions
> 
> func1[x_]:= f[x];
> func2[x_] := g[x];
> step1 = HoldForm[ func1[x]+ func2[x] ];
> 
> Now say I want the statements wrapped in HoldForm,  func1[x]+ func2[x]  to
evaluate to
> f[x]+ func2[x]
> 
> I thought naturally it would be something like
> 
> step2 = HoldForm[ Evaluate[func1[x]]+ func2[x] ] ;
> 
>  But then HoldForm seems a bit overzealous about it's task, and outputs
> Evaluate[func1[x]]+ func2[x]---it leaves Evaluate unevaluated.  (I tried
wrapping it func1[x]
> in ReleaseHold to no avail.)
> 
> The above is just pseudocode, Maybe there is some specifics about what I
was actually
> doing that was causing the unintuitive behavior (i.e. HoldForm holding
Evaluate, instead of,
> well, evaluating.) What I was doing was more along these lines:
> ----------------
> ubounds= {u,0, pi};
> vbounds={v,0,2pi};
> t[dir_]:= D[s[u,v],dir_];    (*where dir_ is going to be either u or v*)
> 
> (*here is the difficult part:*)
> formattedOutput= Integrate[HoldForm[t[u]] , ubounds, vbounds ]
//TraditionalForm;
> ----------------
> The issue is that I want the t[u] wrapped in the HoldForm to evaluate to
D[s[u,v],dir_], and
> hold that. If I wrap it in Evaluate, HoldForm holds Evaluate (puzzling),
and tries to
> integrate Evaluate as if it is the integrand. I've tried all kinds of
permutations of HoldForm,
> Defer, ReleaseHold, Evaluate, and I haven't had anything to work.
> 
> Thank you for any help or pointing me in the right direction




  • Prev by Date: Is Mathematica v8 slower than v7 ?
  • Next by Date: Re: Speed of Mathematica on AMD machines
  • Previous by thread: Re: Fine control of evaluation
  • Next by thread: Re: Fine control of evaluation