Вы можете получить его, используя функцию powerset
из пакета Combinatorics.jl, например ::
julia> using Combinatorics
julia> x = [1:5;]
5-element Array{Int64,1}:
1
2
3
4
5
julia> powerset(x)
Base.Iterators.Flatten{Array{Combinatorics.Combinations{Array{Int64,1}},1}}(Combinatorics.Combinations{Array{Int64,1}}[Combinations{Array{Int64,1}}([1, 2, 3, 4, 5], 0), Combinations{Array{Int64,1}}([1, 2, 3, 4, 5], 1), Combinations{Array{Int64,1}}([1, 2, 3, 4, 5], 2), Combinations{Array{Int64,1}}([1, 2, 3, 4, 5], 3), Combinations{Array{Int64,1}}([1, 2, 3, 4, 5], 4), Combinations{Array{Int64,1}}([1, 2, 3, 4, 5], 5)])
julia> collect(powerset(x))
32-element Array{Array{Int64,1},1}:
[]
[1]
[2]
[3]
[4]
[5]
[1, 2]
[1, 3]
[1, 4]
[1, 5]
[2, 3]
[2, 4]
[2, 5]
[3, 4]
[3, 5]
[4, 5]
[1, 2, 3]
[1, 2, 4]
[1, 2, 5]
[1, 3, 4]
[1, 3, 5]
[1, 4, 5]
[2, 3, 4]
[2, 3, 5]
[2, 4, 5]
[3, 4, 5]
[1, 2, 3, 4]
[1, 2, 3, 5]
[1, 2, 4, 5]
[1, 3, 4, 5]
[2, 3, 4, 5]
[1, 2, 3, 4, 5]
Обратите внимание, что по умолчанию powerset
возвращает итератор, чтобы избежать выделения всех подмножеств.
Также вы можете передать второй и третий позиционный аргумент в powerset
, чтобы ограничить минимальный и максимальный размер возвращаемого подмножества, например ::
julia> collect(powerset(x, 2, 3))
20-element Array{Array{Int64,1},1}:
[1, 2]
[1, 3]
[1, 4]
[1, 5]
[2, 3]
[2, 4]
[2, 5]
[3, 4]
[3, 5]
[4, 5]
[1, 2, 3]
[1, 2, 4]
[1, 2, 5]
[1, 3, 4]
[1, 3, 5]
[1, 4, 5]
[2, 3, 4]
[2, 3, 5]
[2, 4, 5]
[3, 4, 5]