Using Map and Apply The two functions Map and Apply are special to mathematical/``functional'' programming languages like Mathematica. You won't find anything like them in procedural languages like C, C++, Fortran, Matlab, IDL, etc.
If you ``get'' these functions -- understand what they do and how to use them -- then you are on your way to becoming a Mathematica guru. Or maybe you're simply a mathematician.
In[1]:= A = { 2, 3, 5, 8, 11.0, 13 };
In[2]:= B = { {x11, x12}, {x21, x22}, {x31, x32} };
In[3]:= Map[ Sqrt, A ]
Out[3]= {Sqrt[2], Sqrt[3], Sqrt[5], 2 Sqrt[2], 3.31662, Sqrt[13]}
In[4]:= Map[ (#/5)&, A ] (* an "anonymous" function! *)
2 3 8 13
Out[4]= {-, -, 1, -, 2.2, --}
5 5 5 5
In[5]:= Map[ f, B ] (* function `f' applied to the rows of B *)
Out[5]= {f[{x11, x12}], f[{x21, x22}], f[{x31, x32}]}
In[6]:= Map[ f, B, {2} ] (* f of matrix elements, not matrix rows *)
Out[6]= {{f[x11], f[x12]}, {f[x21], f[x22]}, {f[x31], f[x32]}}
In[7]:= Map[ (#[[1]]-#[[2]])&, B ] (* another anonymous function, applied to the rows of B *)
Out[7]= {x11 - x12, x21 - x22, x31 - x32}
To perform a complicated operation on the elements of a list, you can either (1) name and define a function which performs the operation, and use that function name, or (2) use an ``anonymous'' function, with Mathematica's ``# &'' notation. So, suppose you want to take the natural logarithm of the square root of half of (x+1) for each element x of a list. (Re-use the list A defined above as {2,3,5,8,11.0,13}.) The two methods would look like this.
NAMED FUNCTION:
In[8]:= g[x_] := Log[Sqrt[(x+1)/2]] (* call the function ``g'' *)
In[9]:= Map[g, A]
3
Log[-]
2 Log[2] Log[3] 3 Log[7]
Out[9]= {------, ------, ------, Log[-------], 0.89588, ------}
2 2 2 Sqrt[2] 2
ANONYMOUS FUNCTION:
In[10]:= Map[(Log[Sqrt[(#+1)/2]])&, A] (* unnamed -- anonymous -- function *)
3
Log[-]
2 Log[2] Log[3] 3 Log[7]
Out[9]= {------, ------, ------, Log[-------], 0.89588, ------}
2 2 2 Sqrt[2] 2
In[11]:= FullForm[x]
Out[11]//FullForm= x
In[12]:= Head[x]
Out[12]= Symbol
In[13]:= FullForm[13]
Out[13]//FullForm= 13
In[14]:= Head[13]
Out[14]= Integer
In[15]:= FullForm[{a,b,c}]
Out[15]//FullForm= List[a, b, c]
In[16]:= Head[{a,b,c}]
Out[16]= List
|
In[17]:= FullForm[Sqrt[x/y]] Out[17]//FullForm= Power[Times[x, Power[y, -1]], Rational[1, 2]] In[18]:= Head[Sqrt[x/y]] Out[18]= Power In[19]:= FullForm[x+y] Out[19]//FullForm= Plus[x, y] In[20]:= Head[x+y] Out[20]= Plus In[21]:= FullForm[x-y] Out[21]//FullForm= Plus[x, Times[-1, y]] In[22]:= Head[x-y] Out[22]= Plus |
Given the above, Apply can be described simply: it replaces the Head of an object. Study the three examples blow. Explain, for example, the value of Apply[Power,moo+woof], appearing as output #33 below.
Example One
In[23]:= objectA = {x,y}
Out[23]= {x, y}
In[24]:= FullForm[objectA]
Out[24]//FullForm= List[x, y]
In[25]:= objectB = Apply[Plus,objectA]
Out[25]= x + y
In[26]:= FullForm[objectB]
Out[26]//FullForm= Plus[x, y]
| Example TwoIn[27]:= objectC = w * x * y * z Out[27]= w x y z In[28]:= FullForm[objectC] Out[28]//FullForm= Times[w, x, y, z] In[29]:= objectD = Apply[Plus,objectC] Out[29]= w + x + y + z In[30]:= FullForm[objectD] Out[30]//FullForm= Plus[w, x, y, z] |
Example Three
In[31]:= objectE = moo + woof
Out[31]= moo + woof
In[32]:= FullForm[objectE]
Out[32]//FullForm= Plus[moo, woof]
In[33]:= objectF = Apply[Power,objectE]
woof
Out[33]= moo
In[34]:= FullForm[objectF]
Out[34]//FullForm= Power[moo, woof]
|