Ticket #8745: trac_8745.patch

File trac_8745.patch, 4.6 KB (added by ncohen, 11 years ago)
  • sage/combinat/designs/block_design.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1271939620 -7200
    # Node ID 55cebde63cda7cda76aa9b175c88dc33d24b6bee
    # Parent  ef74a6ed21179df343f658c015d7b068bb28c0f3
    trac 8745 : Steiner Triple Systems in BlockDesign
    
    diff -r ef74a6ed2117 -r 55cebde63cda sage/combinat/designs/block_design.py
    a b  
    234234    # A is the incidence matrix of the block design
    235235    return IncidenceStructureFromMatrix(A,name="HadamardDesign")
    236236
     237def steiner_triple_system(n):
     238    r"""
     239    Returns a Steiner Triple System
     240
     241    A Steiner Triple System (STS) of a set `\{0,...,n-1\}`
     242    is a family `S` of 3-sets such that for any `i \not = j`
     243    there exists exactly one set of `S` in which they are
     244    both contained.
     245
     246    It can alternatively be thought of as a factorization of
     247    the complete graph `K_n` with triangles.
     248
     249    A Steiner Triple System of a `n`-set exists if and only if
     250    `n \equiv 1 mod 6` or `n \equiv 3 mod 6`, in which case
     251    one can be found through Bose's and Skolem's constructions,
     252    respectively [AndHon97]_.
     253
     254    INPUT:
     255
     256    - ``n`` returns a Steiner Triple System of `\{0,...,n-1\}`
     257
     258    EXAMPLE:
     259
     260    A Steiner Triple System on `9` elements ::
     261
     262        sage: from sage.combinat.designs.block_design import steiner_triple_system
     263        sage: sts = steiner_triple_system(9)
     264        sage: sts
     265        Incidence structure with 9 points and 12 blocks
     266        sage: list(sts)
     267        [[0, 1, 5], [0, 2, 4], [0, 3, 6], [0, 7, 8], [1, 2, 3], [1, 4, 7], [1, 6, 8], [2, 5, 8], [2, 6, 7], [3, 4, 8], [3, 5, 7], [4, 5, 6]]
     268
     269    As any pair of vertices is covered once, its parameters are ::
     270
     271        sage: sts.parameters()
     272        (2, 9, 3, 1)
     273
     274    An exception is raised for invalid values of ``n`` ::
     275
     276        sage: steiner_triple_system(10)
     277        Traceback (most recent call last):
     278        ...
     279        ValueError: Steiner triple systems only exist for n = 1 mod 6 or n = 3 mod 6
     280
     281    REFERENCE:
     282
     283    .. [AndHon97] A short course in Combinatorial Designs,
     284      Ian Anderson, Iiro Honkala,
     285      Internet Editions, Spring 1997,
     286      http://www.utu.fi/~honkala/designs.ps
     287    """
     288   
     289    name = "Steiner Triple System on "+str(n)+" elements"
     290
     291    if n%6 == 3:
     292        t = (n-3)/6
     293        Z = range(2*t+1)
     294
     295        T = lambda (x,y) : x + (2*t+1)*y
     296
     297        sts = [[(i,0),(i,1),(i,2)] for i in Z] + \
     298            [[(i,k),(j,k),(((t+1)*(i+j)) % (2*t+1),(k+1)%3)] for k in range(3) for i in Z for j in Z if i != j]
     299
     300    elif n%6 == 1:
     301
     302        t = (n-1)/6
     303        N = range(2*t)
     304        T = lambda (x,y) : x+y*t*2 if (x,y) != (-1,-1) else n-1
     305
     306        L1 = lambda i,j : (i+j) % (int((n-1)/3))
     307        L = lambda i,j : L1(i,j)/2 if L1(i,j)%2 == 0 else t+(L1(i,j)-1)/2
     308
     309        sts = [[(i,0),(i,1),(i,2)] for i in range(t)] + \
     310            [[(-1,-1),(i,k),(i-t,(k+1) % 3)] for i in range(t,2*t) for k in [0,1,2]] + \
     311            [[(i,k),(j,k),(L(i,j),(k+1) % 3)] for k in [0,1,2] for i in N for j in N if i < j]           
     312
     313    else:
     314        raise ValueError("Steiner triple systems only exist for n = 1 mod 6 or n = 3 mod 6")
     315
     316    from sage.sets.set import Set
     317    sts = Set(map(lambda x: Set(map(T,x)),sts))
     318
     319    return BlockDesign(n, sts, name=name)
     320
    237321def BlockDesign(max_pt, blks, name=None, test=True):
    238322    """
    239323    Returns an instance of the IncidenceStructure class. Requires each
  • sage/combinat/designs/incidence_structures.py

    diff -r ef74a6ed2117 -r 55cebde63cda sage/combinat/designs/incidence_structures.py
    a b  
    126126        self.name = name
    127127        self._incidence_matrix = inc_mat
    128128
     129    def __iter__(self):
     130        """
     131        Iterator over the blocks.
     132
     133        EXAMPLE::
     134
     135            sage: sts = sage.combinat.designs.block_design.steiner_triple_system(9)
     136            sage: list(sts)
     137            [[0, 1, 5], [0, 2, 4], [0, 3, 6], [0, 7, 8], [1, 2, 3], [1, 4, 7], [1, 6, 8], [2, 5, 8], [2, 6, 7], [3, 4, 8], [3, 5, 7], [4, 5, 6]]
     138        """
     139
     140        return iter(self.blcks)
     141       
     142
    129143    def __repr__(self):
    130144        """
    131145        A print method.
     
    324338            sage: BD.block_sizes()
    325339            [3, 3, 3, 3, 3, 3, 3]
    326340        """
    327         bs = []
    328         for b in self.blocks():
    329             bs.append(len(b))
    330         self._block_sizes = bs
    331         return bs
     341        self._block_sizes = map(len,self.blocks())
     342        return self._block_sizes
    332343
    333344    def _gap_(self):
    334345        """