Re: Evaluation of args in pure functions
- To: mathgroup at smc.vnet.net
- Subject: [mg17001] Re: Evaluation of args in pure functions
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Sat, 10 Apr 1999 02:13:35 -0400
- References: <7ehib1$njm@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Andrea: The comparison is between In[1]:= Unevaluated[ Simplify[1 + 2 x + x^2] ] Out[1]= Unevaluated[Simplify[1 + 2*x + x^2]] Which has simply returned the input unevaluated and In[2]:= Function[Unevaluated[#] [ Simplify[1 + 2 x + x^2] ] Out[2]= Unevaluated[(1 + x)^2] Which follows the standard evaluion process for an expression h[e1,e2,...]; namely, evaluate head, h, then entries, e1, e2, ... in order to give, say, h*[e1*,e2*,...]; then use any rule applicable to this whole expression. In this particular case the steps give Function[Unevaluated[#]][ Simplify[1 + 2 x + x^2] ] (*head,Function[Unevaluated[#]], unchanged because Function has attribute HoldAll *) Function[Unevaluated[#]][ (1+x)^2] (*entry, Simplify[1 + 2 x + x^2] , evaluated*) Now the entry is passed to the slot # in the body, Unevaluated[#]], of Function[..] and the result is returned Unevaluated[(1+x)^2 Because of Unevaluated, nothing more is done. ---------- The fuller form of function, below, seems to be the same but we can give it the attribute HoldFirst and get In[3]:= Function[x, Unevaluated[x], HoldFirst][Simplify[1 + 2 x + x^2]] Out[3]= Unevaluated[Simplify[1 + 2*x + x^2]] This is not possible with a slot function In[4] Function[Unevaluated[#], HoldFirst][ (1+x)^2] Function::"flpar": "Parameter specification \!\(#1\) in \!\(Function[\(#1, HoldFirst\)]\) \ should be a symbol or a list of symbols." Function::"flpar": "Parameter specification \!\(Unevaluated[#1]\) in \ \!\(Function[\(\(Unevaluated[#1]\), HoldFirst\)]\) should be a symbol or a \ list of symbols." Out[4]= Function[Unevaluated[#1], HoldFirst][(1 + x)^2] However we can overide the HoldAll attribute of function in both cases: In[5]:= Evaluate[Simplify[#]]&[1+2x+x^2] Out[5]= 1 + 2*x + x^2 In[6]:= Function[Evaluate[y],Evaluate[Simplify[x]]][1+2x+x^2] Out[6]= 1 + 2*x + x^2 Alan --------------------- 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 Andrea Sosso <sosso at dns.ien.it> wrote in message news:7ehib1$njm at smc.vnet.net... > Hello Group: > > Mathematica seems to evaluate arguments in a different way, when > applying pure functions rather than using the usual function form: > > Here is and example: > > >>> a) Usual form > > In[1]:=Unevaluated[ Simplify[1 + 2 x + x^2] ] > 2 > Out[5]=Unevaluated[ Simplify[1 + 2 x + x ]] > > >>> b) Pure function @ > > In[2]:=Unevaluated[#]& @ Simplify[1 + 2 x + x^2] > 2 > Out[2]=Unevaluated[(1 + x) ] > > > The trace confirms evaluation order is different in these two cases: > > >>> A) Usual form > > In[3]:=Trace[ Unevaluated[ Simplify[1 + 2 x + x^2] ] > 2 2 > Out[3]={Simplify[1 + 2 x + x ], (1 + x) } > > >>> B) Pure function @ > > In[4]:=Unevaluated[#]& @ Simplify[1 + 2 x + x^2] > > Out[4]={{#1 & , Unevaluated[#1] & }, > 2 2 > {Simplify[1 + 2 x + x ], (1 + x) }, > 2 > (Unevaluated[#1] & )[(1 + x) ], > 2 > Unevaluated[(1 + x) ]} > > > Why ? > And how to control evaluation in pure functions ? > > Thanks to anyone giving help. > > > -- > > Andrea Sosso > > > Istituto Elettrotecnico Nazionale "Galileo Ferraris" > Settore Metrologia Elettrica > Strada delle Cacce, 91 > 10135 TORINO > > Phone: +39 11 3919436 > Fax: +39 11 3919436 / 346384 > ---------------------------- > E-mail:sosso at me.ien.it > sand at cstv.to.cnr.it >