From 2bb4741bb4c5a97ebadf1eceafe0d5e2dae1fd29 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Simon=20D=C3=A9saulniers?= Date: Wed, 14 Feb 2018 17:00:36 -0500 Subject: [PATCH 1/1] 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}}. --- helpers.lua | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/helpers.lua b/helpers.lua index ab87f76..ce65576 100644 --- a/helpers.lua +++ b/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 -- 2.39.5