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
Parameters
Description
Thread Safety
Examples
Compatibility
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)
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
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[1] and map[2] 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[8] 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⁡f,x+y⁢z
f⁡y⁢z+f⁡x
map⁡f,y⁢z
f⁡y⁢f⁡z
map⁡f,a,b,c
f⁡a,f⁡b,f⁡c
map⁡x↦x2,x+y
x2+y2
map(proc(x,y) x^2+y end proc, [1,2,3,4], 2);
3,6,11,18
map2⁡f,g,a,b,c
f⁡g,a,f⁡g,b,f⁡g,c
map2⁡op,1,a+b,c+d,e+f
a,c,e
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
NumberOfAbelianGroups≔n::ℤ+↦mul⁡k,k=map⁡combinat:−numbpart,map2⁡op,2,ifactors⁡n2
NumberOfAbelianGroups⁡4
2
NumberOfAbelianGroups⁡6
1
NumberOfAbelianGroups⁡768
22
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:
sum_to_product⁡3+4⁢x2+3⁢x+2⁢x+72
36⁢x2⁢x+2⁢x+72
The evalhf option produces a hardware float result Array.
A≔Array⁡12,1,2,3
A≔12123
B≔mapevalhf,2⁡a,b↦ab,2,A
B≔1.414213562373102.4.8.
A
12123
The inplace option modifies an Array in-place.
mapinplace⁡log2,B
0.5000000000000001.2.3.
B
The indices option pass the index of each entry to the mapping function.
mapindices⁡i↦i,Bi,B
1,0.5000000000000002,1.3,2.4,3.
mapindices⁡i↦ifAi::integerthenAielseBiendif,A
0.500000000000000123
The fold or reduce options reduce the result of map to a single value without producing an intermediate list.
mapfold=`+`,0⁡`^`,1,2,3,4,2
30
exp⁡mapreduce=`+`⁡x↦evalf⁡log⁡x,1,2,3,4
24.00000002
mapreduce=`and`⁡type,1,2,3,4,odd
false
exp⁡mapreduce=`+`,evalhf⁡log,1,2,3,4
24.0000000000000
NULL values produced by fcn are ignored during reduction.
mapreduce=`+`⁡x↦iftype⁡x,oddthenNULLelsexendif,1,2,3,4
6
The scan option is equivalent to mapping map[reduce] over each prefix of the data produced by a simple call to map:
mapscan=`+`⁡x↦x,a,b,c,d,e
a,a+b,a+b+c,a+b+c+d,a+b+c+d+e
L≔1,2,3,4,5
prefixes≔seq⁡L1..n,n=1..numelems⁡L
prefixes≔1,1,2,1,2,3,1,2,3,4,1,2,3,4,5
map⁡y↦mapreduce=`+`⁡x↦2⋅x−1,y,prefixes
1,4,9,16,25
mapscan=`+`⁡x↦2⋅x−1,L
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.
See Also
andmap
applyop
combinat[numbpart]
curry
eval
ifactors
LinearAlgebra[Map]
mul
op
operators/elementwise
operators/functional
proc
rcurry
select
spec_eval_rules
StringTools[Map]
type/appliable
type/atomic
zip
Download Help Document