[Date Index] [Thread Index] [Author Index]
Re: Using "=" vs ":="
On 10/22/06 at 1:20 AM, iamisha1 at comcast.net (misha) wrote: >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:= f[x_, y_]:=1-Sin(x^2+y^2) >No problem so far... The line of code above is valid Mathematica syntax but almost certainly isn't what you think. It defines f[x,y] as 1 minus the product of something called Sin and the expression x^2+y^2. That is the characters '(' and ')' do not delimit arguments for functions. Instead you need to use square brackets. I am reasonably certain what you wanted is f[x_, y_]:= 1-Sin[x^2+y^2] >Then, >In:= f[x,y] >Out:=1-Sin(x^2+y^2) >Still no problem... Yes, this would not show an issue since x and y don't have defined values. Hence, Mathematica returns a symbolic answer showing an implicit multiplication. If you do In:= g[x_,y_]:=1-Sin(x^2+y^2); g[x,y]//FullForm Out//FullForm= Plus[1,Times[-1,Sin,Plus[Power[x,2],Power[y,2]]]] you can see the result is not how you are interpreting it. >Then, >In:=f[1,2] >Out:=1-5 Sin >Huh? See comments above >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:= f[x_, y_]=1-Sin(x^2+y^2) >.... >In:=f[1,2] >Out:=1-Sin I am not sure what you did here but I am certain what you have posted is incomplete. That is In:= f[x_,y_]=1-Sin(x^2+y^2) Out= 1 - Sin*(x^2 + y^2) In:= f[1,2] Out= 1 - 5*Sin >My question is, Why? What's the difference between ":=" and "=" for >defining functions? The difference is when evaluation of the right hand side occurs. For "=", evaluation occurs before the assignment is made. For ":=" evaluation is delayed and occurs when you evaluate the function at some later point. -- To reply via email subtract one hundred and four