MathGroup Archive 2010

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

Search the Archive

Re: beginner question about syntax

On Apr 12, 5:48 am, AK <aaa... at> 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


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


  • 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