map - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


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

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[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.

Thread Safety

• 

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.

Examples

mapf,x+yz

fyz+fx

(1)

mapf,yz

fyfz

(2)

mapf,a,b,c

fa,fb,fc

(3)

mapxx2,x+y

x2+y2

(4)

map(proc(x,y) x^2+y end proc, [1,2,3,4], 2);

3,6,11,18

(5)

map2f,g,a,b,c

fg,a,fg,b,fg,c

(6)

map2op,1,a+b,c+d,e+f

a,c,e

(7)

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

NumberOfAbelianGroupsn::posintmulk,k=mapcombinat:−numbpart,map2op,2,ifactorsn2

NumberOfAbelianGroupsn::+mulk,k=mapcombinat:−numbpart,map2op,2,ifactorsn2

(8)

NumberOfAbelianGroups4

2

(9)

NumberOfAbelianGroups6

1

(10)

NumberOfAbelianGroups768

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:

sum_to_product3+4x2+3x+2x+72

36x2x+2x+72

(12)

The evalhf option produces a hardware float result Array.

AArray12,1,2,3

A12123

(13)

Bmapevalhf,2a,bab,2,A

B1.414213562373102.4.8.

(14)

A

12123

(15)

The inplace option modifies an Array in-place.

mapinplacelog2,B

0.5000000000000001.2.3.

(16)

B

0.5000000000000001.2.3.

(17)

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

mapindicesii,Bi,B

1,0.5000000000000002,1.3,2.4,3.

(18)

mapindicesiifAi::integerthenAielseBiendif,A

0.500000000000000123

(19)

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

(20)

expmapreduce=`+`xevalflogx,1,2,3,4

24.00000002

(21)

mapreduce=`and`type,1,2,3,4,odd

false

(22)

expmapreduce=`+`,evalhflog,1,2,3,4

24.0000000000000

(23)

NULL values produced by fcn are ignored during reduction.

mapreduce=`+`xiftypex,oddthenNULLelsexendif,1,2,3,4

6

(24)

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

mapscan=`+`xx,a,b,c,d,e

a,a+b,a+b+c,a+b+c+d,a+b+c+d+e

(25)

L1,2,3,4,5

L1,2,3,4,5

(26)

prefixesseqL1..n,n=1..numelemsL

prefixes1,1,2,1,2,3,1,2,3,4,1,2,3,4,5

(27)

mapymapreduce=`+`x2x1,y,prefixes

1,4,9,16,25

(28)

mapscan=`+`x2x1,L

1,4,9,16,25

(29)

Compatibility

• 

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