Prev: ArrayPlot with inverted ColorFunction depending on MaxPlotPoints
Next: Converting the integral of a sum into a sum of integrals
From: AK on 12 Apr 2010 06:48 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.
From: David Park on 12 Apr 2010 22:57 Map maps the function onto to parts of the expression at Level 1, or at the levels you specify. But a is just a Symbol and has no Parts or Level 1. Therefore nothing happens. But the second expression does have parts at Level 1: a + b + c // FullForm Plus[a, b, c] So Map does operate on the items at Level 1, i.e., a, b and c. You could also write Map in shortcut form using "/@" to stand for Map, and #^2& to stand for the squaring Function: #^2 & /@ (a + b + c) a^2 + b^2 + c^2 David Park djmpark(a)comcast.net http://home.comcast.net/~djmpark/ From: AK [mailto:aaarbk(a)googlemail.com] 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.
From: dr DanW on 12 Apr 2010 22:57 AK, Whenever Mathematica seems to be doing something nonsensical (based on what you are seeing on the screen,) then FullForm[] is your friend. What you see in your notebook are expressions formatted to be human- readable. FullForm[] lets you see the structure that Mathematica is actually working with. In[14]:= FullForm[a] Out[14]//FullForm= a In[15]:= FullForm[a + b + c] Out[15]//FullForm= Plus[a, b, c] Mathematica expressions are always in the form of head[elem1, elem2, ...]. Of course, you may not see that structure explicitly unless you use FullForm[]. The function Map[f, expr] applies f to each element on the first level in expr. Looking at the FullForm of a, you see that a has no first level (it is all head), thus there is nothing to Map f to. Plus[a,b,c] has the head Plus and three elements (a, b, and c) at the first level, so f gets mapped to them. Hope this helps, Daniel
From: Mike Bryniarski on 12 Apr 2010 22:57 For map to work you must be mapping over the elements of an expression, be it a list like {a,b,c} or a generic expression like f[a,b,c] in your first example : Map[Function[x, x^2], a] a is an atomic symbol, Map normally tries to apply the function to level one of the expression but "a" has no level 1 this fails, what you would want to use is: Function[x, x^2][a] In your second example what is going on is that a+b+c is really seen by Mathematica as Plus[a,b,c], so Map[Function[x, x^2], a + b + c] is really Map[Function[x, x^2],Plus[a,b,c]] and the function is applies to each element of the Plus statement while keeping Plus as the "head" of the expression an intermediate step might look something like: Plus[Function[x, x^2][a],Function[x, x^2][b],Function[x, x^2][c]] which becoms: Plus[a^2,b^2,c^2] or: a^2+b^2+c^2 -Mike On Apr 12, 6:48 am, AK <aaa...(a)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.
From: Sseziwa Mukasa on 12 Apr 2010 22:58
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 |