 map - Maple Help

map

apply a procedure to each operand of an expression

map2

apply a procedure with a specified first argument to each operand of an expression

map[n]

apply a procedure with n-1 specified initial arguments to each operand of an expression Calling Sequence map(fcn, expr, arg2, ..., argN) map2(fcn, arg1, expr, arg3, ..., argN) map[n](fcn, arg1, ..., argn-1, expr, argn+1, ..., argN) map[evalhf](fcn, expr, ...) map[inplace](fcn, expr, ...) map[indices](fcn, expr, arg1, ..., argN) map[fold=(rfcn,zero)](fcn, expr, arg1, ..., argN) map[reduce=rfcn](fcn, expr, arg1, ..., argN) map[scan=sfcn](fcn, expr, arg1, ..., argN) Parameters

 fcn - expression of type appliable; typically, a procedure or name expr - any expression argN - (optional) further arguments to fcn n - positive integer rfcn - function to apply to the result of map to reduce it to a single value sfcn - function to apply to prefixes of the result of map to compute a reduction of the prefix zero - starting value when computing the reduction Description

 • The map commands apply fcn to the operands or elements of expr.
 • map(fcn, expr, arg2, ..., argN) executes fcn(elem, arg2, ..., argN) for each operand or element of expr.
 Similarly, map2(fcn, arg1, expr, arg3, ..., argN) executes fcn(arg1, elem, arg3, ..., argN) for each operand or element of expr.
 In general, map[n](fcn, arg1, ..., argn-1, expr, argn+1, ..., argN), where n is a positive integer, passes the element of expr as the n-th argument of fcn. Thus map and map are equivalent to map and map2 respectively.
 • The result of a call to map (unless the fold or reduce option described below is used) is a copy of expr with the ith operand of expr replaced by the result of applying fcn to the ith operand. This is done for all the operands of expr. For expressions of type atomic, map(fcn, expr) is identical to fcn(expr). For a table or rtable (Array, Matrix, or Vector), fcn is applied to each element instead of each operand.
 • Since strings are atomic expressions in Maple, you cannot map a procedure over a string by using map. However, the StringTools package provides a Map export that delivers this functionality. It is also possible to use seq to construct a sequence based on the characters in a string.
 • If expr has no elements (or operands) then it is returned as is. For example, map(f, [ ]) returns the empty list [ ].
 • Some options can be specified in square brackets as an index to the map command. These options can be used by themselves or in combination with other options. Furthermore, any option shown for map can also be used with map2 and map[n] (the latter by including the option within the same square brackets as n).
 • The evalhf option only applies when mapping over an rtable, or when using the fold or reduce option. Prior to applying the map function, a copy of the original rtable is made with the datatype=float storage option, and the result of each call to fcn is evaluated using evalhf.
 • The inplace option only applies when mapping over a rectangular storage rtable with no indexing function. When inplace is specified, the input rtable is updated in-place.  The result is the updated original expr rtable, rather than a copy.
 • The indices option applies only when mapping over an rtable, list, set, or function, and causes fcn to be passed the index of each entry instead of the value. Note that if an rtable has more than one dimension, multiple arguments are passed for the index, one per dimension.
 • The fold option specifies a function, rfcn, and an initial value, zero, used to reduce the result of map to a single value instead of returning a modified copy of expr. The value is initialized to that specified by zero. Then, for each non-NULL element of the result, rfcn is applied to the value and that element, and the value is updated.
 • The reduce option is similar to fold, except that the value is initialized to the first non-NULL element of the result. Application of rfcn then begins with the value and the next element.
 • Both fold and reduce compute the single result without actually creating the modified copy of expr. Instead, the result value is updated as each individual is computed. This prevents the allocation of a potentially large amount of memory for an intermediate result that would be discarded.
 • When the evalhf option is used with fold or reduce, the arguments to each call to fcn, as well as the result returned, are evaluated using evalhf. The resulting hardware floating point values are passed to rfcn, and its result is also evaluated with evalhf. The value returned by map is a hardware float.
 • Note that fold and reduce are equivalent when the result of mapping fcn over expr is not empty, and the specified zero is the natural zero (or identity) of the reduction function. For example, the natural zero of addition is 0, and of multiplication is 1.
 If the result of mapping fcn over expr is empty (that is, fcn returned NULL for all elements of expr), reduce returns undefined, whereas fold returns zero.
 • Sometimes it is useful to map a function over an expression only for its side effects, without actually constructing a new modified copy of it, nor returning a value. The option reduce=NULL provides this functionality, discarding each returned value of fcn.
 • The scan option is related to reduce, except that instead reducing the result otherwise produced by map to a single value, each prefix of that result is reduced to a single result. It is equivalent to mapping map[reduce] over each prefix of the result produced by a simple call to map, without having to compute that intermediate prefix separately.
 • The fold, reduce, and scan options are mutually exclusive. • The map and map2 commands are thread safe, provided that evaluating the expression fcn is thread safe. Examples

 > $\mathrm{map}\left(f,x+yz\right)$
 ${f}{}\left({y}{}{z}\right){+}{f}{}\left({x}\right)$ (1)
 > $\mathrm{map}\left(f,yz\right)$
 ${f}{}\left({y}\right){}{f}{}\left({z}\right)$ (2)
 > $\mathrm{map}\left(f,\left\{a,b,c\right\}\right)$
 $\left\{{f}{}\left({a}\right){,}{f}{}\left({b}\right){,}{f}{}\left({c}\right)\right\}$ (3)
 > $\mathrm{map}\left(x↦{x}^{2},x+y\right)$
 ${{x}}^{{2}}{+}{{y}}^{{2}}$ (4)
 > map(proc(x,y) x^2+y end proc, [1,2,3,4], 2);
 $\left[{3}{,}{6}{,}{11}{,}{18}\right]$ (5)
 > $\mathrm{map2}\left(f,g,\left\{a,b,c\right\}\right)$
 $\left\{{f}{}\left({g}{,}{a}\right){,}{f}{}\left({g}{,}{b}\right){,}{f}{}\left({g}{,}{c}\right)\right\}$ (6)
 > $\mathrm{map2}\left(\mathrm{op},1,\left[a+b,c+d,e+f\right]\right)$
 $\left[{a}{,}{c}{,}{e}\right]$ (7)

The following example counts the number of finite Abelian groups of the order n by using the map and map2 commands.

 > $\mathrm{NumberOfAbelianGroups}≔n::'\mathrm{posint}'↦\mathrm{mul}\left(k,k=\mathrm{map}\left(\mathrm{combinat}:-\mathrm{numbpart},\mathrm{map2}\left(\mathrm{op},2,\mathrm{ifactors}\left(n\right)\left[2\right]\right)\right)\right)$
 ${\mathrm{NumberOfAbelianGroups}}{≔}{n}{::}{\mathrm{Typesetting}}{:-}{\mathrm{_Hold}}{}\left(\left[{'}{\mathrm{posint}}{'}\right]\right){↦}{\mathrm{mul}}{}\left({k}{,}{k}{=}{\mathrm{map}}{}\left({\mathrm{combinat}}{:-}{\mathrm{numbpart}}{,}{\mathrm{map2}}{}\left({\mathrm{op}}{,}{2}{,}{{\mathrm{ifactors}}{}\left({n}\right)}_{{2}}\right)\right)\right)$ (8)
 > $\mathrm{NumberOfAbelianGroups}\left(4\right)$
 ${2}$ (9)
 > $\mathrm{NumberOfAbelianGroups}\left(6\right)$
 ${1}$ (10)
 > $\mathrm{NumberOfAbelianGroups}\left(768\right)$
 ${22}$ (11)

The map command can be used with type atomic.

 > sum_to_product:=proc(expr)   if expr::atomic then     expr;   elif expr::+ then     *(op(expr));   else    map(procname,expr);  end if; end proc:
 > $\mathrm{sum_to_product}\left(3+4{x}^{2}+3\left(x+2\right){\left(x+7\right)}^{2}\right)$
 ${36}{}{{x}}^{{2}}{}\left({x}{+}{2}\right){}{\left({x}{+}{7}\right)}^{{2}}$ (12)

The evalhf option produces a hardware float result Array.

 > $A≔\mathrm{Array}\left(\left[\frac{1}{2},1,2,3\right]\right)$
 ${A}{≔}\left[\begin{array}{cccc}\frac{{1}}{{2}}& {1}& {2}& {3}\end{array}\right]$ (13)
 > $B≔\mathrm{map}\left[\mathrm{evalhf},2\right]\left(\left(a,b\right)↦{a}^{b},2,A\right)$
 ${B}{≔}\left[\begin{array}{cccc}{1.41421356237310}& {2.}& {4.}& {8.}\end{array}\right]$ (14)
 > $A$
 $\left[\begin{array}{cccc}\frac{{1}}{{2}}& {1}& {2}& {3}\end{array}\right]$ (15)

The inplace option modifies an Array in-place.

 > $\mathrm{map}\left[\mathrm{inplace}\right]\left(\mathrm{log}\left[2\right],B\right)$
 $\left[\begin{array}{cccc}{0.500000000000000}& {1.}& {2.}& {3.}\end{array}\right]$ (16)
 > $B$
 $\left[\begin{array}{cccc}{0.500000000000000}& {1.}& {2.}& {3.}\end{array}\right]$ (17)

The indices option pass the index of each entry to the mapping function.

 > $\mathrm{map}\left[\mathrm{indices}\right]\left(i↦\left[i,B\left[i\right]\right],B\right)$
 $\left[\begin{array}{cccc}\left[{1}{,}{0.500000000000000}\right]& \left[{2}{,}{1.}\right]& \left[{3}{,}{2.}\right]& \left[{4}{,}{3.}\right]\end{array}\right]$ (18)
 > $\mathrm{map}\left[\mathrm{indices}\right]\left(i↦\mathbf{if}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}A\left[i\right]::'\mathrm{integer}'\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{then}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}A\left[i\right]\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{else}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}B\left[i\right]\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{if},A\right)$
 $\left[\begin{array}{cccc}{0.500000000000000}& {1}& {2}& {3}\end{array}\right]$ (19)

The fold or reduce options reduce the result of map to a single value without producing an intermediate list.

 > $\mathrm{map}\left[\mathrm{fold}=\left(\mathrm{+},0\right)\right]\left(\mathrm{^},\left[1,2,3,4\right],2\right)$
 ${30}$ (20)
 > $\mathrm{exp}\left(\mathrm{map}\left[\mathrm{reduce}=\mathrm{+}\right]\left(x↦\mathrm{evalf}\left(\mathrm{log}\left(x\right)\right),\left[1,2,3,4\right]\right)\right)$
 ${24.00000002}$ (21)
 > $\mathrm{map}\left[\mathrm{reduce}=\mathrm{and}\right]\left(\mathrm{type},\left[1,2,3,4\right],\mathrm{odd}\right)$
 ${\mathrm{false}}$ (22)
 > $\mathrm{exp}\left(\mathrm{map}\left[\mathrm{reduce}=\mathrm{+},\mathrm{evalhf}\right]\left(\mathrm{log},\left[1,2,3,4\right]\right)\right)$
 ${24.0000000000000}$ (23)

NULL values produced by fcn are ignored during reduction.

 > $\mathrm{map}\left[\mathrm{reduce}=\mathrm{+}\right]\left(x↦\mathbf{if}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathrm{type}\left(x,\mathrm{odd}\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{then}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathrm{NULL}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{else}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}x\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{if},\left[1,2,3,4\right]\right)$
 ${6}$ (24)

The scan option is equivalent to mapping map[reduce] over each prefix of the data produced by a simple call to map:

 > $\mathrm{map}\left[\mathrm{scan}=\mathrm{+}\right]\left(x↦x,\left[a,b,c,d,e\right]\right)$
 $\left[{a}{,}{a}{+}{b}{,}{a}{+}{b}{+}{c}{,}{a}{+}{b}{+}{c}{+}{d}{,}{a}{+}{b}{+}{c}{+}{d}{+}{e}\right]$ (25)
 > $L≔\left[1,2,3,4,5\right]$
 ${L}{≔}\left[{1}{,}{2}{,}{3}{,}{4}{,}{5}\right]$ (26)
 > $\mathrm{prefixes}≔\left[\mathrm{seq}\left(L\left[1..n\right],n=1..\mathrm{numelems}\left(L\right)\right)\right]$
 ${\mathrm{prefixes}}{≔}\left[\left[{1}\right]{,}\left[{1}{,}{2}\right]{,}\left[{1}{,}{2}{,}{3}\right]{,}\left[{1}{,}{2}{,}{3}{,}{4}\right]{,}\left[{1}{,}{2}{,}{3}{,}{4}{,}{5}\right]\right]$ (27)
 > $\mathrm{map}\left(y↦\mathrm{map}\left[\mathrm{reduce}=\mathrm{+}\right]\left(x↦2\cdot x-1,y\right),\mathrm{prefixes}\right)$
 $\left[{1}{,}{4}{,}{9}{,}{16}{,}{25}\right]$ (28)
 > $\mathrm{map}\left[\mathrm{scan}=\mathrm{+}\right]\left(x↦2\cdot x-1,L\right)$
 $\left[{1}{,}{4}{,}{9}{,}{16}{,}{25}\right]$ (29) Compatibility

 • The map command was updated in Maple 2021.
 • The indices, fold, reduce and scan options were introduced in Maple 2021.