The outer product of two arrays



next up previous contents
Next: An example: Determinants Up: Arrays and matrices Previous: Mixed vector and

The outer product of two arrays

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 and coordinates defined by the S-PLUS 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 (i.e. a matrix, of rank at most 1). Notice that the outer product operator is of course non-commutative.





Erik Moledor
Tue Jan 31 21:02:18 EST 1995