Re: set versus set-delay
- To: mathgroup at smc.vnet.net
- Subject: [mg75218] Re: set versus set-delay
- From: dimitris <dimmechan at yahoo.com>
- Date: Sat, 21 Apr 2007 23:03:04 -0400 (EDT)
- References: <f09h44$19r$1@smc.vnet.net>
You should also have added that there some cases that Set is preferable. For example compare the timings... In[1]:= g2[a_] := Integrate[(x^2 + 4*x + 6)^3/(x^2 + 2*x^2 + 6)^7, {x, 0, a}] In[2]:= Timing[Plot[g2[a], {a, 1, 10}]] (*graphics to be displayed*) Out[2]= {6.766*Second, Graphics[]} In[3]:= g3[a_] = Integrate[(x^2 + 4*x + 6)^3/(x^2 + 2*x^2 + 6)^7, {x, 0, a}] Out[3]= (20800 + (-1331200 + 2291520*a - 675840*a^2 + 5202400*a^3 - 46080*a^4 + 4115232*a^5 + 1693296*a^7 + 363460*a^9 + 32070*a^11 + 16035*Sqrt[2]*(2 + a^2)^6*ArcTan[a/Sqrt[2]])/(2 + a^2)^6)/67184640 In[4]:= Timing[Plot[g3[a], {a, 1, 10}]] (*graphics to be displayed*) Out[4]= {0.014999999999998792*Second, Graphics[]} Dimitris =CF/=C7 siewsk at bp.com =DD=E3=F1=E1=F8=E5: > Informative Article > =================== > > SET versus SETDELAY > > > Newbie are always told to use SETDELAY instead of SET when they create > a mathematica function. > > The objective of this article is to explain the differnce between SET > and SETDELAY using ordinary language (what linguistics call vernacular > language) > > The main differences between those two are: > > SET means CALCULATE FIRST THEN CHANGE > SETDELAY means CHANGE FIRST THEN CALCULATE > > > To explain, consider the following two functions f ang g > > f[x_] = Expand[(x+7)^2] (* Using SET *) > g[x_]:= Expand[(x+7)^2] (* Using SETDELAY *) > > The two functions are identical except f uses SET while g uses > SETDELAY. In normal usage there is no difference in the two functions. > However they produce their results using different steps. > > ================ > SET > ================ > > Consider f[4] > > (Step 1. CALCULATE FIRST ) > > f[x] = Expand[(x+7)^2] > > f[x] = x^2 + 14 x + 49 > > (Step 2. THEN CHANGE ) > > f[x->4] = x^2 + 14 x + 49 > > f[4] <- 4^2 + 14 * 4 + 49 > > f[4] <- 121 > > > ================ > SETDELAY > ================ > > Consider g[4] > > (Step 1. CHANGE FIRST ) > > g[x] := Expand[(x+7)^2] > > g[x->4] := Expand[(x+7)^2] > > g[4] <- Expand[(4+7)^2] > > (Step 2. THEN CALCULATE ) > > g[4] <- Expand[(11)^2] > > g[4] <- Expand[121] > > g[4] <- 121