
Calling Sequence


Map[filter](f, expr, arg2, ...)
Map2[filter](f, arg1, expr, arg2, ...)


Parameters


filter



(optional) booleanvalued procedure; selects locations at which the function f is applied

f



procedure or name; procedure to apply to the operands of expr

expr



expression; expression being mapped

arg1



first argument to pass to f (Map2 only)

arg2, ...



(optional) further arguments to f





Description


•

The Map(f, expr) calling sequence applies f to the operands of expr.


The ith operand of expr is replaced by the result of applying f to the ith operand. This is done for all the operands of expr.


If f takes more than one argument, they are included in the calling sequence as additional arguments arg2, arg3, ..., argn. They are passed as the second, third, ..., nth arguments to f.

•

The Map2(f, arg1, expr) calling sequence is similar to Map, except that for each operand of expr, arg1 is passed as the first argument to f, the operand of expr is passed as the second argument, and arg3, ..., argn are passed as the third, ..., nth arguments.

•

The Map and Map2 commands are identical to the map and map2 commands, respectively, except that if expr is a Matrix or Vector, then f is applied to the elements of that Matrix or Vector, and the application is done inplace.


Additionally, if any such application of f to an element of the Matrix or Vector fails to store (for example, in the case where the shape indicates that the corresponding location is not mutable), then that element is left unchanged. Thus, Map( ) and Map2( ) can be used to update the entries of an upper triangular Matrix. The diagonal entries of a unit triangular Matrix, for example, are not updated by Map( ) or Map2( ).

•

The optional filter parameter, passed as the index to the Map or Map2 command, restricts the application of $f$ to the entries of expr for which the filter returns true. The filter is passed the index of the entry: for a Vector, it is passed one argument; for a Matrix, two arguments.


The filter parameter is ignored if expr is not a Matrix or Vector.

•

These functions are part of the LinearAlgebra package, and so they can be used in the form Map(..) or Map2(..) only after executing the command with(LinearAlgebra). However, they can always be accessed through the long form of the command by using LinearAlgebra[Map](..) or LinearAlgebra[Map2](..).



Examples


>

$\mathrm{with}\left(\mathrm{LinearAlgebra}\right)\:$

>

$A\u2254\mathrm{Matrix}\left(\left[\left[1\,2\,3\right]\,\left[0\,1\,4\right]\right]\,\mathrm{shape}=\mathrm{triangular}\left[\mathrm{upper},\mathrm{unit}\right]\right)$

${A}{\u2254}\left[\begin{array}{ccc}{1}& {2}& {3}\\ {0}& {1}& {4}\end{array}\right]$
 (1) 
>

$M\u2254\mathrm{Map}\left(x\mapsto x+1\,A\right)$

${M}{\u2254}\left[\begin{array}{ccc}{1}& {3}& {4}\\ {0}& {1}& {5}\end{array}\right]$
 (2) 
>

$\mathrm{evalb}\left(\mathrm{addressof}\left(A\right)=\mathrm{addressof}\left(M\right)\right)$

>

$B\u2254\u27e8\u27e81\,2\,3\u27e9\u27e84\,5\,6\u27e9\u27e9$

${B}{\u2254}\left[\begin{array}{cc}{1}& {4}\\ {2}& {5}\\ {3}& {6}\end{array}\right]$
 (4) 
>

$\mathrm{Map2}\left[\left(i\,j\right)\mapsto \mathrm{evalb}\left(i=1\right)\right]\left(\left(x\,a\right)\mapsto a\cdot x,3,B\right)$

$\left[\begin{array}{cc}{3}& {12}\\ {2}& {5}\\ {3}& {6}\end{array}\right]$
 (5) 
>

$\mathrm{Map}\left(x\mapsto x+1\,g\left(3\,A\right)\right)$

${g}{}\left({4}{\,}\left[\begin{array}{ccc}{2}& {3}& {4}\\ {0}& {2}& {5}\end{array}\right]\right)$
 (6) 
By default, these routines act only on the upper triangle of symmetric and skewsymmetric Matrices.
>

$C\u2254\mathrm{Matrix}\left(\left[\left[1\,2\right]\,\left[3\right]\right]\,\mathrm{scan}=\mathrm{triangular}\left[\mathrm{upper}\right]\,\mathrm{shape}=\mathrm{symmetric}\right)$

${C}{\u2254}\left[\begin{array}{cc}{1}& {2}\\ {2}& {3}\end{array}\right]$
 (7) 
>

$\mathrm{Map}\left(x\mapsto x+1\,C\right)$

$\left[\begin{array}{cc}{2}& {3}\\ {3}& {4}\end{array}\right]$
 (8) 
>

Map[proc(i,j) type(i+j,odd) end proc](x>x^2, C);

$\left[\begin{array}{cc}{2}& {9}\\ {9}& {4}\end{array}\right]$
 (9) 


