Map
Map
Map[ x->f(x), list ] applies a given function or mapping to each element of a given list.
Map[ x->expr, list ]
Map[ x->expr, list ] returns a list in which every element of list is substituted for x in expr. The result is not calculated (see the note below). The name of the mapping variable does not matter, and a script variable with the same name is not affected.
| Example | in | out |
|---|---|---|
Substitute each element in x^2. |
Map[ x->x^2, {2,3,4} ] |
{2^2, 3^2, 4^2} |
Use Calc commands to calculate the results. |
Map[ x->CalcPower[x,2], {2,3,4} ] |
{4, 9, 16} |
| Or simplify the result. | Simplify[ Map[ x->x^2, {2,3,4} ] ] |
{4, 9, 16} |
| A script variable can be used in the mapping. | a := 3 |
r = {3*1, 3*2} |
Use indices (for example from SortI) to look up elements of a list. |
L := {3, 1, 2} |
r = {1, 2, 3} |
Map[ {x,y,...}->expr, list ]
If the mapping has a list of variables, each element of list must be a list with a value for each variable.
| Example | in | out |
|---|---|---|
| Add the two numbers in each pair. | Map[ {x,y}->x+y, { {1,2}, {3,4} } ] |
{1+2, 3+4} |
Map[ func, list ]
Map[ func, list ] applies the function or command func to each element of list.
| Example | in | out |
|---|---|---|
Apply Sqrt to each element. |
Map[ Sqrt, {2,3,4} ] |
{Sqrt[2], Sqrt[3], Sqrt[4]} |
Apply Length to each element. |
Map[ Length, { {1,2}, {3} } ] |
{2, 1} |
Apply RandomInt to each range. |
Map[ RandomInt, { {1,5}, {1,10} } ] |
for example {3, 4} |
Map[ func, arg1, arg2, ... ]
With more arguments, func is applied to the first elements of all lists, then to the second elements, and so on. All lists must have the same length. An argument that is not a list is used in every application.
| Example | in | out |
|---|---|---|
| Add the elements of two lists pairwise. | Map[ CalcPlus, {1,2}, {10,20} ] |
{11, 22} |
Multiply each element by 10. |
Map[ CalcTimes, {1,2,3}, 10 ] |
{10, 20, 30} |
Apply Apply to each pair, with times as its first argument. |
Map[ Apply, times, { {-3,1}, {6,1} } ] |
{-3*1, 6*1} |
Map substitutes the values but does not calculate the result: Map[ x->x^2, {2,3,4} ] gives {2^2, 3^2, 4^2}. Use Calc commands such as CalcPower and CalcPlus in the mapping, or wrap the result in Simplify.
Options given to Map are passed on to every application of func. Which options are allowed depends on func.
| Syntax | Example |
|---|---|
Map[ func, list, Option=>value ] |
Map[ RandomInt, { {1,3}, {1,3}, {1,3} }, Exclude=>{2} ] |
- Empty list:
Map[ x->x^2, {} ]returns{}. - No lists: if none of the arguments is a list,
funcis applied once:Map[ CalcPlus, 1, 2 ]returns3.
| Invalid input | Result |
|---|---|
Map[ x->x^2, 5 ] |
Error: when the 1st argument is a mapping, the 2nd argument should be a list |
Map[ x->x^2, {1,2}, {3,4} ] |
Error: when the 1st argument is a mapping, the number of arguments should be two |
Map[ CalcPlus, {1,2}, {10,20,30} ] |
Error: the expressions should have equal number of arguments |
Map[ 5, {1,2} ] |
Error: the 1st argument should be a mapping or function |
Map[ x->x^2 ] |
Error: the number of arguments should be at least 2 |
Map[ {x,y}->x*y, {1,2} ] |
Error: the elements must be lists when the mapping has a list of variables. |
Commands inside a mapping are executed before the values are substituted. For example, Map[ x->RandomInt[{1,x}], {5,10} ] gives an error, because RandomInt is executed with the variable x instead of a number. Wrap the mapping in Hold to execute the commands after substitution: Map[ Hold[x->RandomInt[{1,x}]], {5,10} ] gives, for example, {5, 2}.
See also: Apply, Substitute, FuncApplication, Simplify