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
Parameters
Description
Thread Safety
Examples
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, ...)
fcn
-
expression of type appliable; typically, a procedure or name
expr
any expression
argN
(optional) further arguments to fcn
n
positive integer
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
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
A≔Array⁡1,2,3,4,datatype=float8
A≔1.2.3.4.
mapevalhf,inplace,2⁡a,b↦ab,2,A
2.4.8.16.
A1
2.
See Also
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
What kind of issue would you like to report? (Optional)