Determine all n-tuples using the elements of a set. This is really just a simple wrapper for expand.grid, so it is not optimized.

tuples(set, n = length(set), repeats = FALSE, list = FALSE)

Arguments

set

a set

n

length of each tuple

repeats

if set contains duplicates, should the result?

list

tuples as list?

Value

a matrix whose rows are the n-tuples

Examples

tuples(1:2, 3)
#> [,1] [,2] [,3] #> [1,] 1 1 1 #> [2,] 1 1 2 #> [3,] 1 2 1 #> [4,] 1 2 2 #> [5,] 2 1 1 #> [6,] 2 1 2 #> [7,] 2 2 1 #> [8,] 2 2 2
tuples(1:2, 3, list = TRUE)
#> [[1]] #> [1] 1 1 1 #> #> [[2]] #> [1] 1 1 2 #> #> [[3]] #> [1] 1 2 1 #> #> [[4]] #> [1] 1 2 2 #> #> [[5]] #> [1] 2 1 1 #> #> [[6]] #> [1] 2 1 2 #> #> [[7]] #> [1] 2 2 1 #> #> [[8]] #> [1] 2 2 2 #>
apply(tuples(c("x","y","z"), 3), 1, paste, collapse = "")
#> [1] "xxx" "xxy" "xxz" "xyx" "xyy" "xyz" "xzx" "xzy" "xzz" "yxx" "yxy" "yxz" #> [13] "yyx" "yyy" "yyz" "yzx" "yzy" "yzz" "zxx" "zxy" "zxz" "zyx" "zyy" "zyz" #> [25] "zzx" "zzy" "zzz"
# multinomial coefficients r <- 2 # number of variables, e.g. x, y n <- 2 # power, e.g. (x+y)^2 apply(burst(n,r), 1, function(v) factorial(n)/ prod(factorial(v))) # x, y, xy
#> [1] 1 1 2
mp("x + y")^n
#> x^2 + 2 x y + y^2
r <- 2 # number of variables, e.g. x, y n <- 3 # power, e.g. (x+y)^3 apply(burst(n,r), 1, function(v) factorial(n)/ prod(factorial(v)))
#> [1] 1 1 3 3
mp("x + y")^n
#> x^3 + 3 x^2 y + 3 x y^2 + y^3
r <- 3 # number of variables, e.g. x, y, z n <- 2 # power, e.g. (x+y+z)^2 apply(burst(n,r), 1, function(v) factorial(n)/ prod(factorial(v))) # x, y, z, xy, xz, yz
#> [1] 1 1 1 2 2 2
mp("x + y + z")^n
#> x^2 + 2 x y + 2 x z + y^2 + 2 y z + z^2