map - Maple Programming 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

 Calling Sequence map(fcn, expr, arg1, ..., argN) map2(fcn, arg1, expr, arg2, ..., argN) map[n](fcn, arg1, ..., argn-1, expr, argn, ..., argN) map[evalhf](fcn, expr, ...) map[inplace](fcn, expr, ...)

Parameters

 fcn - procedure or name expr - any expression argN - (optional) further arguments to fcn n - positive integer

Description

 • The map commands apply fcn to the operands or elements of expr.
 • map(fcn, expr, arg1, ..., argN) executes fcn(elem, arg1, ..., argN) for each operand or element of expr.  Similarly, map2(fcn, arg1, expr, arg2, ..., argN) executes fcn(arg1, elem, arg2, ..., argN) for each operand or element of expr, and map[3](fcn, arg1, arg2, expr, arg3, ..., argN) executes fcn(arg1, arg2, elem, arg3, ..., argN) for each operand or element of expr.
 • The result of a call to map 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 (such as Array, Matrix, or Vector), fcn is applied to each element instead of each operand.
 • If fcn takes more than one argument, they are to be specified as additional arguments, arg2, arg3, ..., argn, which are simply passed through as the second, third, ..., nth arguments to fcn.
 • 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.
 • The evalhf option only applies when mapping over an rtable.  Prior to applying the map function, a copy of the original rtable is made with datatype=float[8] storage. Each application of fcn is evaluated using evalhf, which works only in the hardware float domain.
 • 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.
 • 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.

 • 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)}_{2}\right)\right)\right)$
 ${\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)}_{{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)
 > $A≔\mathrm{Array}\left(\left[1,2,3,4\right],\mathrm{datatype}=\mathrm{float}[8]\right)$
 ${A}{≔}\left[\begin{array}{cccc}{1.}& {2.}& {3.}& {4.}\end{array}\right]$ (13)
 > $\mathrm{map}[\mathrm{evalhf},\mathrm{inplace},2]\left(\left(a,b\right)→{a}^{b},2,A\right)$
 $\left[\begin{array}{cccc}{2.}& {4.}& {8.}& {16.}\end{array}\right]$ (14)
 > ${A}_{1}$
 ${2.}$ (15)