The standard R expression outer(X, Y, f)
evaluates to a matrix whose (i, j)-th entry has the value f(X[i], Y[j])
.
I would like to implement the function multi.outer
, an n-dimensional generalization of outer
: multi.outer(f, X_1, ..., X_n)
, where f is some n-ary function, would produce a (length(X_1) * ... * length(X_n)) array whose (i_1,...,i_n)-th entry has the value f(X_1[i_1], ..., X_n[i_n])
for all valid index sets (i_1,...,i_n). Clearly, for each i in {1, ..., n}, all the elements of X_i
in multi.outer(f, X_1,...,X_i,..., X_n)
must be allowable i-th arguments for the function f
. For the case n=2, multi.outer
would do the same thing as outer
, although it would have a different signature (IOW, multi.outer(f, X, Y)
would be equivalent to outer(X, Y, f)
).
It is important to note that, although the arguments X_1, ..., X_n of multi.outer
are all vectors, they don't necessarily all have the same mode. E.g. X_1 and X_2 could be c(1, 2, 3)
and LETTERS[10:20]
, respectively.
Thanks!
See Question&Answers more detail:os