Prev: ArrayPlot with inverted ColorFunction depending on MaxPlotPoints
Next: Converting the integral of a sum into a sum of integrals
From: Murray Eisenberg on 12 Apr 2010 23:00 There are two keys here. First, the official definition of Map, said briefly (in response to evaluating ?Map) is, "Map[f, expr] ... applies f to each element on the first level in expr." Second, the FullForm of expressions, which can show you what the "elements" are at various "levels". Look, first at a + b + c: FullForm[a + b + c] Plus[a, b, c] {Part[a + b + c, 1], Part[a + b + c, 2], Part[a + b + c, 3]} {a, b, c} So when you Map Function[x,x^2] onto a + b + c, what it's doing is to evaluate that expression at each of the three parts a, b, c, respectively and stick the results back in place of a, b, c, respectively, in the whole expression: Plus[a^2, b^2, c2] a^2 + b^2 + c^2 Which is what you obtained. Now look at the object a: FullForm[a] a Part[a, 1] (* you get an error message along with the input you gave returned unevaluated -- but in the abbreviated form: *) a[[1]] Thus the object a does not have any parts at level 1, so Map does nothing to it, and the result is just the original object a. Map[Function[x,x^2], a] a On the other hand: FullForm[{a}] List[a] Part[{a}, 1] a Map[Function[x,x^2], {a}] {a^2} Hope this helps. On 4/12/2010 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. > -- Murray Eisenberg murray(a)math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305
From: telefunkenvf14 on 12 Apr 2010 23:01 On Apr 12, 5: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. 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
From: Bill Rowe on 12 Apr 2010 23:02 On 4/12/10 at 6:53 AM, aaarbk(a)googlemail.com (AK) wrote: >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 By default, Map works at level 1. A symbol with nothing assigned to it has no level 1. You could have gotten the output you were expecting by telling Map to work at level 0 which refers to the entire expression. That is: In[11]:= Map[Function[x, x^2], a, {0}] Out[11]= a^2 >In[2]:=Map[Function[x, x^2], a + b + c] >Out[2]:= a^2 + b^2 + c^2 This works since the expression a + b + c has a level 1. >If I enclose the second argument of Map[] inside a list, By doing that you create a level 1. >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... For more detail see tutorial/LevelsInExpressions
From: Bob Hanlon on 12 Apr 2010 23:02 a is an atom there is nothing to Map onto. Use Function[x, x^2][a] a^2 Map applies the function to each element on the first level of expression: {a} // FullForm List[a] Map[Function[x, x^2], {a}] == List[Function[x, x^2][a]] True a + b + c // FullForm Plus[a,b,c] Map[Function[x, x^2], a + b + c] == Plus[ Function[x, x^2][a], Function[x, x^2][b], Function[x, x^2][c]] True Bob Hanlon ---- AK <aaarbk(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: Albert Retey on 12 Apr 2010 23:03
Am 12.04.2010 12:48, schrieb AK: > 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. I think this is probably one of the remarkable differences between mathematica and most other systems: "Everything is an expression" and there is no difference between a List and a sum of terms, one is an expression with head List and a variable number of arguments, the other an expression with head Plus and also a variable number of arguments. Map will work on both. On the other hand the symbols a is an "Atom", its Head is symbol and it cannot have arguments, so Map will not work for it. This is also working for user defined symbols: Map[f,a[1,2,3]] If you have experience with other systems, but are new to mathematica and are primarily interested in the difference to other comparable systems I think reading the tutorials at the bottom of the page guide/ExpressionStructure in the documentation center will be a good starting point. For the next step I would suggest everything in howto/WorkWithVariablesAndFunctions especially tutorial/PatternsAndTransformationRules which will reveal the next important difference IMO: the whole evaluation process is just rewritting an expression by applying rules that have been defined with = (Set) or := (SetDelayed). hth, albert |