RE: Using "=" vs ":="
- To: mathgroup at smc.vnet.net
- Subject: [mg70676] RE: [mg70664] Using "=" vs ":="
- From: "Erickson Paul-CPTP18" <Paul.Erickson at Motorola.com>
- Date: Mon, 23 Oct 2006 02:49:43 -0400 (EDT)
":=" is a delayed set meaning the right hand side is unevaluated whereas "=" the right hand side is evaluated and set. For this example since the right hand side before and after evaluation is the same, if won't matter. However it will matter whether or not you use the Sin function "i.e., Sin[..]" or not "i.e., Sin ( .. )". Using the "( .. )" the internal is evaluated as x^2+y^2 to a five and multiplied whereas the "[ .. ]" says that it is an argument to the function Sin. You can check the definition of the function with "?f" anytime after definition in case your not sure. A trivial example where there is at least a visual difference is: a = 2+3 (* results in a set to 5 *) a := 2+3 (* results in a set to 2+3 - now that may not matter in practice, but in some cases it will matter *) -----Original Message----- From: misha [mailto:iamisha1 at comcast.net] To: mathgroup at smc.vnet.net Subject: [mg70676] [mg70664] Using "=" vs ":=" I'm going through Mathematic by Example, 2nd ed., (Abell and Braselton), and have come across something that puzzles me. Chapter 2, Section 2, Example 8 Define f(x,y)=1-sin(x^2+y^2) So I first try, In[1]:= f[x_, y_]:=1-Sin(x^2+y^2) No problem so far... Then, In[2]:= f[x,y] Out[2]:=1-Sin(x^2+y^2) Still no problem... Then, In[3]:=f[1,2] Out[3]:=1-5 Sin Huh? I noticed that rather than using ":=" to "simply define" this function, as opposed to (just) "=" to "define and compute" this function, I get different subsequent behavior. Specifically, doing the above with just "=", works fine. In[1]:= f[x_, y_]=1-Sin(x^2+y^2) .... In[3]:=f[1,2] Out[3]:=1-Sin[5] My question is, Why? What's the difference between ":=" and "=" for defining functions? Thanks! Misha