On Thu, 11 Oct 2018, Sven Schreiber wrote:
Am 11.10.2018 um 15:04 schrieb Riccardo (Jack) Lucchetti:
> On Thu, 11 Oct 2018, Sven Schreiber wrote:
>
>> Am 23.02.2016 um 14:02 schrieb Henrique Andrade:
>>> Dear Jack and Allin,
>>>
>>> Thanks a lot for your invaluable advices! I really couldn't figure out
>>> these solutions and code improvements by myself.
>>>
>>>
>>
>> Let me pick up this old thread: Would it make sense to integrate such a
>> reasonably optimized powerset function into the extra.gfn package? (which
>> is now an addon)
>
> Sonds good to me.
>
OK, which one is the final optimized version?
If I'm not mistaken, this is more or less what we came up with in the end
<hansl>
function strings powerset(strings S)
matrix P = {}
scalar l = nelem(S)
scalar N = 2^l
matrix s = seq(0, N-1)'
loop l --quiet
matrix a = s % 2
matrix P ~= a
matrix s = (s-a)/2
endloop
strings PS = array(N)
loop i = 1..N --quiet
string c = ""
loop j = 1..l --quiet
if P[i, j]
c = c ~ " " ~ S[j]
endif
endloop
PS[i] = c
endloop
return PS
end function
# example
strings S = defarray("wage", "lwage", "belavg",
"abvavg", "exper",\
"looks", "union", "goodhlth", "black",
"female", "married", "south",\
"bigcity", "smllcity",
"service","expersq","educ")
set stopwatch
strings PwrSet = powerset(S)
printf "Elapsed time = %g\n", $stopwatch
</hansl>
-------------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Scienze Economiche e Sociali (DiSES)
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------