MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: beginner question about syntax

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109105] Re: beginner question about syntax
  • From: telefunkenvf14 <rgorka at gmail.com>
  • Date: Mon, 12 Apr 2010 23:02:51 -0400 (EDT)
  • References: <hputp3$lh7$1@smc.vnet.net>

On Apr 12, 5:48 am, AK <aaa... at googlemail.com> wrote:
> Hi,
>
> I'm a fairly seasoned user of another system who's just started using
> Mathematica. Although at the moment I'm just playing around with
> Mathematica (without any specific task at hand), trying to figure out
> the Mathematica way of doing things from the documentation
> (particularly the examples) there are some things I can't seem to wrap
> my head around. For example, can anyone explain the outputs for the
> inputs below:
> In[1]:= Map[Function[x, x^2], a]
> Out[1]:= a
> In[2]:=Map[Function[x, x^2], a + b + c]
> Out[2]:= a^2 + b^2 + c^2
>
> If I enclose the second argument of Map[] inside a list, I get the
> expected output, but I don't understand what the operations given in
> the example above represent and why the outputs are what they are.
> Would appreciate an explanation for what's going here... thank you in
> advance.

Pure functions are easier to understand using the following notation,
where the #1 stands for the first variable in your function.
Evaluating it just returns the same thing.

In[1]:= #1^2&
Out[1]= #1^2&

If you want to apply the pure function to 'a', one way to do this is:

In[2]:= #1^2&[a]
Out[2]= a^2

Note that if I add another variable, I get the same thing:

In[3]:= #1^2&[a,b]
Out[3]= a^2

Map[] can also be used to apply the pure function. (Note: (1)
Mathematica applies Map at level 1, by default. (2) Most people use
the /@ notation rather than wrapping with Map[] all the time.)

In[4]:= #1^2&/@{a}
Out[4]= {a^2}

Also note that the following:

In[5]:= #1^2&/@(a)
Out[5]= a

And for your second example, I'm not sure if you wanted:

In[6]:= #^2&/@(a+b+c)
Out[6]= a^2+b^2+c^2

Or,

In[7]:= #^2&/@{a+b+c}
Out[7]= {(a+b+c)^2}

I'm sure someone else will chime in with a better/clearer answer and a
lecture on levels of expressions in Mathematica...

-RG


  • Prev by Date: Re: Root again
  • Next by Date: Re: beginner question about syntax
  • Previous by thread: Re: beginner question about syntax
  • Next by thread: Re: beginner question about syntax