MathGroup Archive 2010

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

Search the Archive

Re: beginner question about syntax

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109089] Re: beginner question about syntax
  • From: Sseziwa Mukasa <mukasa at jeol.com>
  • Date: Mon, 12 Apr 2010 22:59:53 -0400 (EDT)

On Apr 12, 2010, at 6:53 AM, AK 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.

In the expression:

Map[Function[x, x^2], a]

a is atomic, Map doesn't operate on such objects so it simply returns a unevaluated.

In

Map[Function[x, x^2], a + b + c]

the Head of the expression is Plus (the FullForm of a+b+c is Plus[a,b,c]) so it maps the function over the arguments to Plus giving the result you see.  You can confirm this using Trace

(Debug) In[61]:== Trace[Map[Function[x,x^2],a+b+c]]
(Debug) Out[61]== {Function[x,x^2]/@(a+b+c),Function[x,x^2][a]+Function[x,x^2][b]+Function[x,x^2][c],{Function[x,x^2][a],a^2},{Function[x,x^2][b],b^2},{Function[x,x^2][c],c^2},a^2+b^2+c^2}

Unfortunately the Trace of the first expression is not informative.

Putting the second argument inside of a list makes Map operate over the elements of the list.  For further information study level specifications and expression structure in the help text.

Regards,
	Ssezi


  • Prev by Date: Using package functions in Manipulate/DynamicModule
  • Next by Date: Re: beginner question about syntax
  • Previous by thread: Re: beginner question about syntax
  • Next by thread: Re: beginner question about syntax