Group Abstract Group Abstract

Message Boards Message Boards

0
|
13.3K Views
|
11 Replies
|
12 Total Likes
View groups...
Share
Share this post:

[?] Different ways to define a function?

Posted 8 years ago
POSTED BY: Edward Davis
11 Replies
POSTED BY: EDITORIAL BOARD

So this post is for you then: see subchapter "5.2.3 Attributes of Pure Functions" of Maeder's book PIM 3e. Whenever I believe that I understood something, maybe a concept explained in the WLDC/tutorial or so, and feel a bit more confident about the topic, and i then look at any subchapter of that book, I am back at the humblest of myself. Love the book tho, has great side note explanations commenting his code!!

POSTED BY: Raspi Rascal
Anonymous User
Anonymous User
Posted 8 years ago

I will read it. Thanks.

POSTED BY: Anonymous User

One can set attributes on pure functions no problem. Never mind since the op seems gone.

POSTED BY: Raspi Rascal
Anonymous User
Anonymous User
Posted 8 years ago

I appreciate the info.

POSTED BY: Anonymous User
Anonymous User
Anonymous User
Posted 8 years ago

think of this

#1*#2&

as just an abbreviation for this:

Function[{x,y},x*y]

Like all abbreviations it looks odd at first, but once you get used to it, you may appreciate its conciseness.

Pure functions are Wolfram's implementation of Church's concept of lambda function:

https://en.wikipedia.org/wiki/Lambda_calculus

Between Church and Wolfram, McCarthy included lambda functions in LISP.

I don't know if it is possible to set attributes on Mathematica's pure functions or do pattern matching on their arguments, as you can with Mathematica's regular functions.

POSTED BY: Anonymous User
POSTED BY: Raspi Rascal

It's often convenient to use a pure function when you start with a function f of two arguments and want to fix one of the arguments so as to evaluate f for several values of the other argument, and then you would combine the pure function with a Map (or, often, the /@ special input form for Map).

For example:

 cubeRootsUnity = Exp[(2 Range[3] \[Pi] I)/3];
 Graphics[{
   Red, PointSize[Large],
   Point /@ ReIm[cubeRootsUnity],
   Blue, Thick, Arrowheads[Medium],
   Arrow[{{0, 0}, #}] & /@ ReIm[cubeRootsUnity]
   }, Axes -> True, PlotRange -> 1.25]

Plot of cube roots of unity

To be utterly precise here, the expressionf[arg1, arg2] is Arrow[{arg1,arg2}], and the fixed first argument is {0, 0}.

It would be tedious (especially if you considered, say, 8th roots of unity instead of cube roots), to repeat the Arrow expression for each individual root.

(Note that Arrow[{{0, 0},#}]& is not Listable.)

POSTED BY: Murray Eisenberg

In the simplest situations the two paradigms are interchangeable. The & way (cryptic slang for Function) may be a little faster for purely numerical functions, because it skips the pattern matching, but I don't really know. The p[x_]= way is much more customisable with all the pattern-matching capabilities. With the & way you don't need to give the function a name, you simply insert the function code into your expressions.

A general-purpose beginner shouldn't be bothered with the &, I think.

POSTED BY: Gianluca Gorni
POSTED BY: Matthew Sottile
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard