Re: What does & mean?

• To: mathgroup at smc.vnet.net
• Subject: [mg107155] Re: What does & mean?
• From: Bill Rowe <readnews at sbcglobal.net>
• Date: Thu, 4 Feb 2010 06:26:56 -0500 (EST)

```On 2/3/10 at 6:12 AM, canopus56 at yahoo.com (Canopus56) wrote:

>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.

Details and tutorials on "&" can be found by entering the "&"
character in the search box of the Documentation center and
following the lings

>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.

This is incorrect. Functions defined using the "&" are evaluated
by Mathematica in the same manner as the equivalent function
defined differently. The "&" character is simply a shorthand way
of creating a function definition.

>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]

I could just as easily define g by

g = # Sin[#]&

Both definitions, the more usual definition using SetDelayed
(":=") and the definition above create a thing in memory called
g. So, if you definition of static is being previously defined
and stored in memory, it should be clear using "&" does not
change this characteristic of the function.

Note, when defining g here I used Set rather than SetDelayed. I
could have defined g using SetDelayed instead of Set as follows:

g := # Sin[#]&

The difference between SetDelayed and Set is when the right hand
side gets evaluated by Mathematica. When set is used, the right
hand side is immediately evaluated by Mathematica and the result
of that evaluation is assigned to the symbol on the left hand
side. SetDelayed defers evaluation of the right hand side until
the function is actually used later. In many, perhaps most
cases, this difference between SetDelayed and Set is unimportant.

>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]

While the "&" is used to define a pure function, the example you
give here is not a pure function. In fact

Map[g[#]&, data]

evaluated identically to

Map[g, data]

in every respect. The second definition using Slot (#) and &
simply uses more characters to defined exactly the same operation.

>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]

This is a pure function. The difference here is the expression
#*Sin[#}& has no name. That is what makes it a pure function.

>The above syntax can be condensed further into a highly symbolic
>form by using the symbolic invocation of Map function /@ -

>#*Sin[#] & /@ data

The /@ is simply a convenient shorthand for Map, nothing more.
And in this particular case, the two operations used to define
the pure function have the attribute Listable so the Map
function is not needed and the same result can be obtained using

#*Sin[#]&[data]

or

#*Sin[#]&@data

or

#*Sin@#&@data

All I've done here is use different shorthand notation for doing
exactly the same thing. While the last entry uses the fewest
keystrokes to create, it is also probably the most arcane from
the perspective of someone unfamiliar with Mathematica notation.
And in fact, this version is probably more difficult to quickly
read and understand by those quite familiar with Mathematica notation.

In code I write for myself, the key reason I use various
shorthand notations is to make the resulting code more easily readable.

```

• Prev by Date: Re: Re: Combining InterpolatingFunctions
• Next by Date: Re: position of sequence of numbers in list
• Previous by thread: Re: What does & mean?
• Next by thread: Re: Re: What does & mean?