apply a procedure to each operand of an expression
apply a procedure with a specified first argument to each operand of an expression
apply a procedure with n-1 specified initial arguments to each operand of an expression
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)
expression of type appliable; typically, a procedure or name
(optional) further arguments to fcn
function to apply to the result of map to reduce it to a single value
function to apply to prefixes of the result of map to compute a reduction of the prefix
starting value when computing the reduction
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.
For more information on thread safety, see index/threadsafe.
map(proc(x,y) x^2+y end proc, [1,2,3,4], 2);
The following example counts the number of finite Abelian groups of the order n by using the map and map2 commands.
NumberOfAbelianGroups ≔ n::posint→mul⁡k,k=map⁡combinat:-numbpart,map2⁡op,2,ifactors⁡n2
The map command can be used with type atomic.
if expr::atomic then
elif expr::`+` then
map(procname,expr); end if;
The evalhf option produces a hardware float result Array.
A ≔ Array⁡12,1,2,3
B ≔ mapevalhf,2⁡a,b→ab,2,A
The inplace option modifies an Array in-place.
The indices option pass the index of each entry to the mapping function.
The fold or reduce options reduce the result of map to a single value without producing an intermediate list.
NULL values produced by fcn are ignored during reduction.
The scan option is equivalent to mapping map[reduce] over each prefix of the data produced by a simple call to map:
L ≔ 1,2,3,4,5
prefixes ≔ seq⁡L1..n,n=1..numelems⁡L
map⁡y→mapreduce=`+`⁡x→2*x − 1,y,prefixes
The map command was updated in Maple 2021.
The indices, fold, reduce and scan options were introduced in Maple 2021.
For more information on Maple 2021 changes, see Updates in Maple 2021.
Download Help Document