From ce51c8748de317c489bf8f422deb954331dc5f04 Mon Sep 17 00:00:00 2001 From: Maxime Vorwerk Date: Fri, 25 Aug 2023 15:16:02 +0200 Subject: [PATCH] created powerset functions --- julia/Powerset.jl | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 julia/Powerset.jl diff --git a/julia/Powerset.jl b/julia/Powerset.jl new file mode 100644 index 0000000..132df21 --- /dev/null +++ b/julia/Powerset.jl @@ -0,0 +1,38 @@ + +module Powerset + +global _calculatedPowerSets = Matrix{Int64}(undef, 1, 0) +global _grownUntil = 0 + +function getPowerSet(setSize::Integer) + wantedPowerSetSize = 2^setSize + if setSize <= _grownUntil + return _calculatedPowerSets[1:wantedPowerSetSize, 1:setSize] + else + currentPowerSetSize = 2^_grownUntil + newPowerSets = zeros(Int64, wantedPowerSetSize, setSize) + indices = CartesianIndices(_calculatedPowerSets) + copyto!(newPowerSets, indices, _calculatedPowerSets, indices) + for i ∈ (_grownUntil+1):setSize + indicesTarget = CartesianIndices((2^(i-1)+1:2^i, 1:(i-1))) + indicesSource = CartesianIndices((1:2^(i-1), 1:(i-1))) + copyto!(newPowerSets, indicesTarget, newPowerSets, indicesSource) + for j ∈ 1:currentPowerSetSize + newPowerSets[currentPowerSetSize + j, i] = 1; + end + currentPowerSetSize *= 2 + end + _calculatedPowerSets = newPowerSets + _grownUntil = setSize + return _calculatedPowerSets + end +end + +function getPowerSubSet(setSize::Integer, subSetSize::Integer) + PowerSet = getPowerSet(setSize) + setSizes = dropdim(sum(Powerset, dims = 2), dims = 2) + + return +end + +end