Re: Using "=" vs ":="
- To: mathgroup at smc.vnet.net
- Subject: [mg70688] Re: Using "=" vs ":="
- From: Bill Rowe <readnewsciv at sbcglobal.net>
- Date: Mon, 23 Oct 2006 02:50:26 -0400 (EDT)
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[1]:= 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[2]:= f[x,y]
>Out[2]:=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[3]:=
g[x_,y_]:=1-Sin(x^2+y^2);
g[x,y]//FullForm
Out[4]//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[3]:=f[1,2]
>Out[3]:=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[1]:= f[x_, y_]=1-Sin(x^2+y^2)
>....
>In[3]:=f[1,2]
>Out[3]:=1-Sin[5]
I am not sure what you did here but I am certain what you have
posted is incomplete. That is
In[1]:=
f[x_,y_]=1-Sin(x^2+y^2)
Out[1]=
1 - Sin*(x^2 + y^2)
In[2]:=
f[1,2]
Out[2]=
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