Re: Re: What does & mean?
- To: mathgroup at smc.vnet.net
- Subject: [mg107131] Re: [mg107059] Re: What does & mean?
- From: Canopus56 <canopus56 at yahoo.com>
- Date: Wed, 3 Feb 2010 06:12:45 -0500 (EST)
- References: <hk6d22$m61$1@smc.vnet.net> <201002020824.DAA08531@smc.vnet.net>
On Feb 1, 4:14 am, Michael Knudsen <micknud... at gmail.com> wrote:
> However, there is no explanation of how & works . . .
I'm a beginner too. This is how I interpreted the ampersand syntax. I also could not find an acceptable definition in the software documentation. I got it from a separate dummies
guide.
The ampersand is a way to force evaluation of a function dynamically on the fly. It is used to execute one - time dynamic functions on-the-fly.
Let' s look at a sine function applied to these data points. (Copy and paste each formula into your copy of Mathematica and evaluate.)
data = Table[x, {x, (\[Pi]/8), \[Pi], (\[Pi]/8)}]
A conventional method would be to declare a traditional static called function. The := creates - what you might term in a scripting language like Javascript or VBA - a static called function. This function is stored in memory and reused each time it is called. It returns a computed result each time it is called.
g[x_] := x*Sin[x]
A conventional method to call this static function stored in memory and to apply it to a set of points would be -
Table[g[data[[k]]], {k, 1, Length[data]}]
The Map function is another conventional way to apply the static function to the data points -
Map[g, data]
Instead of a static call, the ampersand can be used to invoke evaluation of a function dynamically - the so called pure function -
Map[g[#] &, data]
We also can evaluate the desired root function dynamically using ampersand, thus dispensing with the need to create a separate static called function in the first place -
Map[#*Sin[#] &, data]
The above syntax can be condensed further into a highly symbolic form by using the symbolic invocation of Map function /@ -
#*Sin[#] & /@ data
The above says -
1) Take the dynamic function #*Sin[#] that I just defined on-the-fly;
2) Apply that dynamic function by mapping to each element in the list "data"; and,
3) When we are done, trash dynamic function #*Sin[#] because we do not need it anymore.
The difference is the static stored function g[x_] := x*Sin[x] sits in memory taking up space.
The dynamic pure function #*Sin[#] & is created dynamically and is automatically destroyed after it is used.
This frees up some memory for other tasks.
Hope that helps.
- Kurt
- References:
- Re: What does & mean?
- From: Noqsi <jpd@noqsi.com>
- Re: What does & mean?