An important operation on arrays is the outer product. If a and b are two numeric arrays, their outer product is an array whose dimension vector is got by concatenating their two dimension vectors, (order is important), and whose data vector is got by forming all possible products of elements of the data vector of a with those of b. The outer product is formed by the special operator %o%:
ab <- a %o% b
An alternative is
ab <- outer(a, b, '*')
The multiplication function can be replaced by an arbitrary function of two
variables. For example if we wished to evaluate the function
over a regular grid of values with x- and y- coordinates defined by the
. vectors x and y respectively, we could proceed as
follows:
> f <- function(x,y) cos(y)/(1 + x^2) > z <- outer(x, y, f)In particular the outer product of two ordinary vectors is a doubly subscripted array (that is a matrix, of rank at most 1). Notice that the outer product operator is of course non-commutative. Defining your own . functions will be considered further in Chapter
.