Home > Archive > Mathematica > June 2005 > pure functions vs. functions
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
pure functions vs. functions
|
|
| Marcin Rak 2005-05-27, 8:59 am |
|
Hey everyone, I had a beginner question: what exactly is the difference
between a pure function and just a function?
ie. the difference between declaring functions using
f[arg1_,...,argn_] := "some expression making use of the arguments"
and explicilty calling Function[{arg1_,...,argn_}, "same expression
making use of the arguments"]
They have different heads!!
Thanks
MR
| |
| David Park 2005-05-28, 8:58 am |
| MR,
First, pure functions are very useful in Mathematica and it's great that you
are looking at them.
Pure functions are usually used where we want to carry out some operation on
expressions without actually going to the bother of defining a named
function. Maybe it is something you want to do on the fly and you are only
going to use it once.
Here is a function that takes the square of an expression.
f[x_]:=x^2
We use it...
f[2a]
4*a^2
But maybe we only wanted to do this once. So why bother defining f. (Of
course, in this case we could just use (2a)^2 but I'm trying to use a simple
example where f might actually be more complicated.) We could just use...
#^2 &[2a]
4*a^2
The pure function is just a substitute for 'f'. Instead of using a name of a
function we simply describe the action we want from the function and don't
bother with assigning a name.
Here is an example where we might want to use pure functions. We are going
to show how a simple linear equation is solved step-by-step for x. (Copy the
code and paste it into a Mathematica notebook to evaluate.)
Print["Equation to solve for x"]
a x + b == c
Print["Subtract b from each side"]
# - b & /@ %%
Print["Divide each side by a"]
#/a & /@ %%
We can do the entire derivation in one cell. The interspersed Print
statements annotate the steps. %% refers to the second previous output
(jumping over the Print statements). Each operation is defined by a pure
function, which is mapped to each side of the equation. It would be
cumbersome to define functions to subtract b from an expression, or to
divide an expression by a because we would never use them again.
The Function statement is just a longer method of writing a pure function.
Sometimes I find Function useful because it is a little more explicit and it
is easier to follow if I'm trying to write nested pure functions.
David Park
djmp@earthlink.net
http://home.earthlink.net/~djmp/
From: Marcin Rak [mailto:umrakmm@cc.umanitoba.ca]
Hey everyone, I had a beginner question: what exactly is the difference
between a pure function and just a function?
ie. the difference between declaring functions using
f[arg1_,...,argn_] := "some expression making use of the arguments"
and explicilty calling Function[{arg1_,...,argn_}, "same expression
making use of the arguments"]
They have different heads!!
Thanks
MR
| |
|
| Neither of those is a pure function. This one is:
f = Sin[#]&
Bobby
On Fri, 27 May 2005 04:56:53 -0400 (EDT), Marcin Rak <umrakmm@cc.umanitoba.ca> wrote:
>
> Hey everyone, I had a beginner question: what exactly is the difference
> between a pure function and just a function?
> ie. the difference between declaring functions using
> f[arg1_,...,argn_] := "some expression making use of the arguments"
>
> and explicilty calling Function[{arg1_,...,argn_}, "same expression
> making use of the arguments"]
>
> They have different heads!!
>
> Thanks
> MR
>
>
>
>
>
--
DrBob@bigfoot.com
| |
| Marcin Rak 2005-06-01, 9:10 am |
| Thanks for the thurough description of when one would go about using the
pure function.
I've never programmed in a language of this sort - I'm used to procedural
and object orianted but Mathematica (which seems to simply be a whole bunch
of statements nested one within the other is something completely
different).
Perhaps you could help me understand also the structure of programs in
Mathematica, by answering whether the following is true:
1) Each line of input in Mathematica is simply at most a whole bunch of
nested commands where the output of the inner is used as the input of the
outer?
2) The only way to program proceduraly in Mathematica is to employ the
entire notebook?
Thanks again
MR
----- Original Message -----
From: "David Park" <djmp@earthlink.net>
Subject: Re: pure functions vs. functions
> MR,
>
> First, pure functions are very useful in Mathematica and it's great that
you
> are looking at them.
>
> Pure functions are usually used where we want to carry out some operation
on
> expressions without actually going to the bother of defining a named
> function. Maybe it is something you want to do on the fly and you are only
> going to use it once.
>
> Here is a function that takes the square of an expression.
>
> f[x_]:=x^2
>
> We use it...
>
> f[2a]
> 4*a^2
>
> But maybe we only wanted to do this once. So why bother defining f. (Of
> course, in this case we could just use (2a)^2 but I'm trying to use a
simple
> example where f might actually be more complicated.) We could just use...
>
> #^2 &[2a]
> 4*a^2
>
> The pure function is just a substitute for 'f'. Instead of using a name of
a
> function we simply describe the action we want from the function and don't
> bother with assigning a name.
>
> Here is an example where we might want to use pure functions. We are going
> to show how a simple linear equation is solved step-by-step for x. (Copy
the
> code and paste it into a Mathematica notebook to evaluate.)
>
> Print["Equation to solve for x"]
> a x + b == c
> Print["Subtract b from each side"]
> # - b & /@ %%
> Print["Divide each side by a"]
> #/a & /@ %%
>
> We can do the entire derivation in one cell. The interspersed Print
> statements annotate the steps. %% refers to the second previous output
> (jumping over the Print statements). Each operation is defined by a pure
> function, which is mapped to each side of the equation. It would be
> cumbersome to define functions to subtract b from an expression, or to
> divide an expression by a because we would never use them again.
>
> The Function statement is just a longer method of writing a pure function.
> Sometimes I find Function useful because it is a little more explicit and
it
> is easier to follow if I'm trying to write nested pure functions.
>
> David Park
> djmp@earthlink.net
> http://home.earthlink.net/~djmp/
>
>
>
>
>
> From: Marcin Rak [mailto:umrakmm@cc.umanitoba.ca]
>
>
> Hey everyone, I had a beginner question: what exactly is the difference
> between a pure function and just a function?
> ie. the difference between declaring functions using
> f[arg1_,...,argn_] := "some expression making use of the arguments"
>
> and explicilty calling Function[{arg1_,...,argn_}, "same expression
> making use of the arguments"]
>
> They have different heads!!
>
> Thanks
> MR
>
>
>
>
>
| |
| Chris Chiasson 2005-06-02, 9:06 am |
| What do you mean by procedurally?
On 6/1/05, Marcin Rak <umrakmm@cc.umanitoba.ca> wrote:
> Thanks for the thurough description of when one would go about using the
> pure function.
> I've never programmed in a language of this sort - I'm used to procedural
> and object orianted but Mathematica (which seems to simply be a whole bunch
> of statements nested one within the other is something completely
> different).
>
> Perhaps you could help me understand also the structure of programs in
> Mathematica, by answering whether the following is true:
> 1) Each line of input in Mathematica is simply at most a whole bunch of
> nested commands where the output of the inner is used as the input of the
> outer?
>
> 2) The only way to program proceduraly in Mathematica is to employ the
> entire notebook?
>
> Thanks again
> MR
> ----- Original Message -----
> From: "David Park" <djmp@earthlink.net>
> Subject: pure functions vs. functions
>
>
> you
> on
> simple
> a
> the
> it
>
>
--
Chris Chiasson
http://chrischiasson.com/
1 (810) 265-3161
|
|
|
|
|