Re: Map
- To: mathgroup at smc.vnet.net
- Subject: [mg130918] Re: Map
- From: Szabolcs HorvÃt <szhorvat at gmail.com>
- Date: Sat, 25 May 2013 05:39:59 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-outx@smc.vnet.net
- Delivered-to: mathgroup-newsendx@smc.vnet.net
- References: <knnboj$qqi$1@smc.vnet.net>
On 2013-05-24 09:26:43 +0000, Jon Morris said: > I'm new to Mathematica and I've been given some code to help me analyse > some data. I'm trying to understand what the Map function does, > specifically what the {2,2} means? > > qbarlist = Map[(# - bragg) &, hkllist,{2, 2}]; > > hklist is a 3 column list, bragg is a three element vector. > > When I try the same line with {2} or {1} I seem to get the same answer. > The online explanation of this term does not make that much sense to > me. I'd be very grateful if someone could explain the purpose of the > last term of the Map syntax. Hello Jon, Did you take a look at the examples in the documentation? Those will make it much clearer how this works. Choose an undefined function so you get back the result in the form f[x]. I assume you understand what Map does without a level specification. Now let's take an example from the docs and try different valus of the levels specification: In[26]:= Map[f, {{a, b}, {c, d, e}}, {0}] Out[26]= f[{{a, b}, {c, d, e}}] In[27]:= Map[f, {{a, b}, {c, d, e}}, {1}] Out[27]= {f[{a, b}], f[{c, d, e}]} In[28]:= Map[f, {{a, b}, {c, d, e}}, {2}] Out[28]= {{f[a], f[b]}, {f[c], f[d], f[e]}} Notice how the function f goes deeper and deeper into the expression as you incerase the level. In[29]:= Map[f, {{a, b}, {c, d, e}}, {3}] Out[29]= {{a, b}, {c, d, e}} There's no level 3 in the expression {{a, b}, {c, d, e}} so this last one does nothing. {2,2} is exactly the same as {2}. This will insert f into levels 0 through 2, i.e. all levels of this expression: In[30]:= Map[f, {{a, b}, {c, d, e}}, {0, 2}] Out[30]= f[{f[{f[a], f[b]}], f[{f[c], f[d], f[e]}]}] A naive way to think of level n of a nested list is this: move to deeper and deeper lists until you have passed exactly n braces. (But note that Map works on any expression, not just lists.)