[Matrix-SIG] generate set S^g

Pearu Peterson pearu@ioc.ee
Thu, 26 Nov 1998 09:47:58 +0200 (EET)


Hi!
For those how are interested in genereting a set S^g
find below one of many ways how to do it in python.
For example,
{1,2}^2 is {{1,1}, {1,2}, {2,1}, {2,2}}

------------> combination.py <------------
#!/usr/bin/env python
"""
Generate combinations.

Pearu Peterson, <pearu@ioc.ee>
November 25, 1998
"""
def comb(S,g):
    """
    comb(S,g) returns a set S^g as a list of lists.
    s is list,
    g is positive integer.
    """
    a=b=t=''
    p=[]
    if g<1: return p
    for j in range(g):
        a='%s\n%sfor i%d in %s:'%(a,t,j,`S`)
        b='%s,i%d'%(b,j)
        t='%s\t'%t
    exec '%s\n%s p.append([%s])\n'%(a,t,b[1:])
    return p
    
if __name__ == "__main__":
    print comb([1,2,3],2)
    print comb([1,2],3)

--------> end of combination.py <------------


Pearu Peterson <pearu@ioc.ee>, MSc, Researcher
Department of Mechanics and Applied Mathematics          http://koer.ioc.ee/~pearu/
Institute of Cybernetics at Tallinn Technical University Phone: (+3722) 527907
Akadeemia rd. 21, 12618 Tallinn ESTONIA                  Fax:   (+372) 6397039
*** the nonvalidity of rigorous causality is necessary 
    and not just consistently possible (Heisenberg, 1925) ***