R: Outer Product of Arrays (original) (raw)

outer {base} R Documentation

Description

The outer product of the arrays X and Y is the arrayA with dimension c(dim(X), dim(Y)) where elementA[c(arrayindex.x, arrayindex.y)] = FUN(X[arrayindex.x], Y[arrayindex.y], ...).

Usage

outer(X, Y, FUN = "*", ...)
X %o% Y

Arguments

X, Y first and second arguments for function FUN. Typically a vector or array.
FUN a function to use on the outer products, found via match.fun (except for the special case "*").
... optional arguments to be passed to FUN.

Details

X and Y must be suitable arguments for FUN. Each will be extended by [rep](../../base/help/rep.html) to length the products of the lengths of X and Y before FUN is called.

FUN is called with these two extended vectors as arguments (plus any arguments in ...). It must be a vectorized function (or the name of one) expecting at least two arguments and returning a value with the same length as the first (and the second).

Where they exist, the [dim]names of X and Y will be copied to the answer, and a dimension assigned which is the concatenation of the dimensions of X and Y (or lengths if dimensions do not exist).

FUN = "*" is handled as a special case via as.vector(X) %*% t(as.vector(Y)), and is intended only for numeric vectors and arrays.

%o% is binary operator providing a wrapper forouter(x, y, "*").

Author(s)

Jonathan Rougier

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language. Wadsworth & Brooks/Cole.

See Also

[%*%](../../base/help/+25+2A+25.html) for usual (inner) matrix vector multiplication;[kronecker](../../base/help/kronecker.html) which is based on outer;[Vectorize](../../base/help/Vectorize.html) for vectorizing a non-vectorized function.

Examples

x <- 1:9; names(x) <- x
# Multiplication & Power Tables
x %o% x
y <- 2:8; names(y) <- paste(y,":", sep = "")
outer(y, x, `^`)

outer(month.abb, 1999:2003, FUN = paste)

## three way multiplication table:
x %o% x %o% y[1:3]

[Package _base_ version 4.6.0 Index]