mirror of https://github.com/lcpz/lain.git
helpers: powerset and trivial_partition_set
- the powerset is the set of all combinations of a given set; - the trivial partition set is the simplest partition of a set. For e.g., the trivial partition set of {a, b, c}, is simply {{a}, {b}, {c}}.
This commit is contained in:
parent
b94e701ff2
commit
2bb4741bb4
23
helpers.lua
23
helpers.lua
|
@ -170,6 +170,29 @@ function helpers.spairs(t)
|
|||
end
|
||||
end
|
||||
|
||||
-- create trivial partition of a set. The trivial partition set is the simplest
|
||||
-- partition of a set. For e.g., the trivial partition set of {a, b, c}, is
|
||||
-- simply {{a}, {b}, {c}}.
|
||||
function helpers.trivial_partition_set(set)
|
||||
local ss = {}
|
||||
for _,e in pairs(set) do
|
||||
ss[#ss+1] = {e}
|
||||
end
|
||||
return ss
|
||||
end
|
||||
|
||||
-- creates the powerset of a given set
|
||||
function helpers.powerset(s)
|
||||
if not s then return {} end
|
||||
local t = {{}}
|
||||
for i = 1, #s do
|
||||
for j = 1, #t do
|
||||
t[#t+1] = {s[i],unpack(t[j])}
|
||||
end
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
-- }}}
|
||||
|
||||
return helpers
|
||||
|
|
Loading…
Reference in New Issue