On Sat, 16 Jun 2018, Sven Schreiber wrote:

> Am 15.06.2018 um 22:02 schrieb Henrique Andrade:
>> Em 15 de junho de 2018, Periklis Gogas escreveu:
>>
>>     Yes of course, I would love to use it.
>> 
>> 
>> Dear Periklis, attached you will find two files. The AutoMod is the one you 
>> need to execute to get the models with the combined variables. This is a 
>> draft. I have a more polished, but it is not written in Hansl/Gretl (but I 
>> can translate it if anyone is interested).
>
> Hi Henrique,
> thanks for this code. This could be quite easily turned into a nice function 
> package. What would it take to convince you to do it?

Really very nice. The equivalent for vectors, in two varieties. The first 
is a slighly more idiomatic adaptation of Henrique's code, the other one 
uses recursion.

<hansl>
function matrices PSetMat(matrix X)
     if nelem(X) == 0
         return defarray({})
     endif

     matrix Set = msortby(uniq(vec(X)), 1)
     scalar SetSize = rows(Set)

     scalar PowerSetSizeMax = 2^SetSize
     matrices PowerSet = array(PowerSetSizeMax)
     PowerSet[1] = {}
     scalar PowerSetSize = 1

     scalar n = 2

     loop i = 1..SetSize --quiet
         loop j = 1..PowerSetSize --quiet
             PowerSet[n++] = PowerSet[j] ~ Set[i]
             PowerSetSize++
         endloop
     endloop

     return PowerSet
end function

function matrices AltPSetMat(matrix X)

     if rows(X) == 0
         matrices PowerSet = defarray({})
     else
         matrix Set = msortby(uniq(vec(X)), 1)
         n = rows(Set)
         N = 2^n
         matrices PowerSet = array(N)
         if n == 1
             PowerSet[1] = {}
             PowerSet[2] = Set[1]
         else
             tmp = AltPSetMat(Set[1:n-1])
             PowerSet = tmp
             loop i = 1 .. nelem(tmp) --quiet
                 PowerSet += tmp[i] ~ Set[n]
             endloop
         endif
     endif

     return PowerSet
end function

###
### example
###

clear
matrix X = {3, 11, 8, 2}
A = PSetMat(X)

loop i = 1 .. nelem(A) --quiet
     printf "A[%3d] = %5.0f", i, A[i]
     if nelem(A[i]) == 1
         printf "\n"
     endif
endloop

B = AltPSetMat(X)

loop i = 1 .. nelem(B) --quiet
     printf "B[%3d] = %5.0f", i, B[i]
     if nelem(B[i]) == 1
         printf "\n"
     endif
endloop
</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
-------------------------------------------------------

Reply via email to