This package implements a feature known as "Broadcasting" in Python (see e.g. here) and as "Implicit Expansion" in Matlab (see e.g. here). In operations involving multiple arguments of type array (or vector, matrix, list) with mismatching dimensions, any argument is repeated along its dimensions of size (exactly) 1, as often as necessary to match the other argument(s).
Below are some examples that illustrate possible operations using implicit expansion. For detailed explanations of the behavior, see the corresponding docs for Python and Matlab.
Dimensions of size 0 are ok, as long as all other arrays are also of size 0 or 1 in that dimension.
All arguments to an operation with implicit expansion are coerced to arrays first.
Currently, all arguments to an operation with implicit expansion are expanded to the full size of the output first, resulting in bad performance for very large arrays.
The package rray
(
on GitHub,
documentation,
currently archived on CRAN
) provides similar functionality as part of complete a remodeling of the array object.
x <- c(1,2,3)
y <- t(c(4,5))
x %m+% y
#> [,1] [,2]
#> [1,] 5 6
#> [2,] 6 7
#> [3,] 7 8
mmapply(sum, x, x, t(x))
#> [,1] [,2] [,3]
#> [1,] 3 4 5
#> [2,] 5 6 7
#> [3,] 7 8 9
m <- matrix(3*(1:12)^2, 3, 4)
cm <- t(colMeans(m))
m %m-% cm
#> [,1] [,2] [,3] [,4]
#> [1,] -11 -29 -47 -65
#> [2,] -2 -2 -2 -2
#> [3,] 13 31 49 67
summaries <- list(Max = max, Min = min, avg = mean)
data <- list(a = 1:5, b = 2:3, c = 20:12)
formatStrings <- array(c('%.1f', '%.3f'), c(1,1,2))
mmapply(function(f, d, s) sprintf(s, f(d)), summaries, t(data), formatStrings)
#> , , 1
#>
#> a b c
#> Max "5.0" "3.0" "20.0"
#> Min "1.0" "2.0" "12.0"
#> avg "3.0" "2.5" "16.0"
#>
#> , , 2
#>
#> a b c
#> Max "5.000" "3.000" "20.000"
#> Min "1.000" "2.000" "12.000"
#> avg "3.000" "2.500" "16.000"
#>