26.4 Applying Functions to Parts of Expressions
If you have a list of elements, it is often important to be able to apply a function separately to each of the elements. You can do this in Mathematica TE using Map.
This applies f separately to each element in a list.
In[1]:= Map[f, {a, b, c}]
Out[1]= 
You can use Map to find the length of each of the sublists of a list.
In[2]:= Map[Length, {{1, 3, 4}, {5, 6, 7}, {2, 1, 6, 6}}]
Out[2]= 
Applying a function to each element in a list.
What Map[f, expr] effectively does is to "wrap" the function f around each element of the expression expr. You can use Map on any expression, not just a list.
This applies f to each element in the sum.
In[3]:= Map[f, a + b + c]
Out[3]= 
This applies Log to both sides of an equation.
In[4]:= Map[Log, x == y]
Out[4]= 
This applies the function Sqrt to each argument of g.
In[5]:= Map[Sqrt, g[x^2, x^3]]
Out[5]= 
Map[f, expr] applies f to the first level of parts in expr. You can use MapAll[f, expr] to apply f to all the parts of expr.
This defines a
matrix m.
In[6]:= m = {{a, b}, {c, d}}
Out[6]= 
Map applies f to the first level of m, in this case the rows of the matrix.
In[7]:= Map[f, m]
Out[7]= 
MapAll applies f at all levels in m. If you look carefully at this expression, you will see an f wrapped around every part.
In[8]:= MapAll[f, m]
Out[8]= 
Ways to apply a function to different parts of expressions.