calc: Mapping
10.8.2 Mapping
--------------
The ‘V M’ (‘calc-map’) [‘map’] command applies a given operator
elementwise to one or more vectors. For example, mapping ‘A’ [‘abs’]
produces a vector of the absolute values of the elements in the input
vector. Mapping ‘+’ pops two vectors from the stack, which must be of
equal length, and produces a vector of the pairwise sums of the
elements. If either argument is a non-vector, it is duplicated for each
element of the other vector. For example, ‘[1,2,3] 2 V M ^’ squares the
elements of the specified vector. With the 2 listed first, it would
have computed a vector of powers of two. Mapping a user-defined
function pops as many arguments from the stack as the function requires.
If you give an undefined name, you will be prompted for the number of
arguments to use.
If any argument to ‘V M’ is a matrix, the operator is normally mapped
across all elements of the matrix. For example, given the matrix ‘[[1,
-2, 3], [-4, 5, -6]]’, ‘V M A’ takes six absolute values to produce
another 3x2 matrix, ‘[[1, 2, 3], [4, 5, 6]]’.
The command ‘V M _’ [‘mapr’] (i.e., type an underscore at the
operator prompt) maps by rows instead. For example, ‘V M _ A’ views the
above matrix as a vector of two 3-element row vectors. It produces a
new vector which contains the absolute values of those row vectors,
namely ‘[3.74, 8.77]’. (Recall, the absolute value of a vector is
defined as the square root of the sum of the squares of the elements.)
Some operators accept vectors and return new vectors; for example, ‘v v’
reverses a vector, so ‘V M _ v v’ would reverse each row of the matrix
to get a new matrix, ‘[[3, -2, 1], [-6, 5, -4]]’.
Sometimes a vector of vectors (representing, say, strings, sets, or
lists) happens to look like a matrix. If so, remember to use ‘V M _’ if
you want to map a function across the whole strings or sets rather than
across their individual elements.
The command ‘V M :’ [‘mapc’] maps by columns. Basically, it
transposes the input matrix, maps by rows, and then, if the result is a
matrix, transposes again. For example, ‘V M : A’ takes the absolute
values of the three columns of the matrix, treating each as a 2-vector,
and ‘V M : v v’ reverses the columns to get the matrix ‘[[-4, 5, -6],
[1, -2, 3]]’.
(The symbols ‘_’ and ‘:’ were chosen because they had row-like and
column-like appearances, and were not already taken by useful operators.
Also, they appear shifted on most keyboards so they are easy to type
after ‘V M’.)
The ‘_’ and ‘:’ modifiers have no effect on arguments that are not
matrices (so if none of the arguments are matrices, they have no effect
at all). If some of the arguments are matrices and others are plain
numbers, the plain numbers are held constant for all rows of the matrix
(so that ‘2 V M _ ^’ squares every row of a matrix; squaring a vector
takes a dot product of the vector with itself).
If some of the arguments are vectors with the same lengths as the
rows (for ‘V M _’) or columns (for ‘V M :’) of the matrix arguments,
those vectors are also held constant for every row or column.
Sometimes it is useful to specify another mapping command as the
operator to use with ‘V M’. For example, ‘V M _ V A +’ applies ‘V A +’
to each row of the input matrix, which in turn adds the two values on
that row. If you give another vector-operator command as the operator
for ‘V M’, it automatically uses map-by-rows mode if you don’t specify
otherwise; thus ‘V M V A +’ is equivalent to ‘V M _ V A +’. (If you
really want to map-by-elements another mapping command, you can use a
triple-nested mapping command: ‘V M V M V A +’ means to map ‘V M V A +’
over the rows of the matrix; in turn, ‘V A +’ is mapped over the
elements of each row.)
Previous versions of Calc had “map across” and “map down” modes that
are now considered obsolete; the old “map across” is now simply ‘V M V
A’, and “map down” is now ‘V M : V A’. The algebraic functions ‘mapa’
and ‘mapd’ are still supported, though. Note also that, while the old
mapping modes were persistent (once you set the mode, it would apply to
later mapping commands until you reset it), the new ‘:’ and ‘_’
modifiers apply only to the current mapping command. The default ‘V M’
always means map-by-elements.
Algebraic Manipulation, for the ‘a M’ command, which is like
‘V M’ but for equations and inequalities instead of vectors.
Storing Variables, for the ‘s m’ command which modifies a variable’s
stored value using a ‘V M’-like operator.