map - Maple Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : Flow Control : Iteration or Looping : map

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

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.

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)

mapx→x2,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::posint→mulk,k=mapcombinat:-numbpart,map2op,2,ifactorsn2

NumberOfAbelianGroups:=n::posint→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)

AArray1,2,3,4,datatype=float[8]

A:=1.2.3.4.

(13)

map[evalhf,inplace,2]a,b→ab,2,A

2.4.8.16.

(14)

A1

2.

(15)

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/atomic

 


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam