Ticket #4859: trac_4859-covering-designs2.patch

File trac_4859-covering-designs2.patch, 14.2 KB (added by dgordon, 11 years ago)

fix to problems in first patch pointed out by the reviewers

  • sage/combinat/designs/all.py

    # HG changeset patch
    # User Daniel Gordon <gordon@ccrwest.org>
    # Date 1233097073 28800
    # Node ID add14567dd7e714288df0027415dbac01c3363dd
    # Parent  a65f968c0f2e9eff0022377d8d5cb9ba200f2017
    modifications to answer the comments
    
    diff -r a65f968c0f2e -r add14567dd7e sage/combinat/designs/all.py
    a b  
    1010                             schonheim,
    1111                             trivial_covering_design,
    1212                             best_known_covering_design_www)
     13
  • sage/combinat/designs/covering_design.py

    diff -r a65f968c0f2e -r add14567dd7e sage/combinat/designs/covering_design.py
    a b  
    4949from sage.misc.sage_eval import sage_eval
    5050from sage.structure.sage_object import SageObject
    5151from sage.rings.integer import Integer
     52from sage.rings.rational import Rational
    5253from sage.rings.integer_ring import ZZ
    5354from sage.rings.arith import binomial
    5455from sage.combinat.combination import Combinations
    55 from sage.rings.real_double import RDF
    5656from sage.combinat.designs.incidence_structures import IncidenceStructure
    5757
    5858###################### covering design functions ##############################
     
    7979        sage: schonheim(32,16,8)
    8080        930
    8181    """
    82     bound = 1.0
     82    bound = 1
    8383    for i in range(t-1,-1,-1):
    84         bound = (bound*RDF(v-i)/RDF(k-i)).ceiling()
     84        bound = Rational((bound*(v-i),k-i)).ceil()
    8585
    8686    return bound
    8787
     
    100100
    101101    EXAMPLE:
    102102        sage: C = trivial_covering_design(8,3,1)
    103         sage: C.show()
     103        sage: print C
    104104        C(8,3,1) = 3
    105105        Method: Trivial
    106106        0   1   2   
    107107        0   6   7   
    108108        3   4   5   
    109109        sage: C = trivial_covering_design(5,3,2)
    110         sage: C.show()
     110        sage: print C
    111111        4 <= C(5,3,2) <= 10
    112112        Method: Trivial
    113113        0   1   2   
     
    140140        return CoveringDesign(v,k,t,1,range(v),[blk],1,"Trivial")
    141141
    142142    if t==1:     #blocks [0,...,k-1],[k,...,2k-1],...
    143         size = (RDF(v)/RDF(k)).ceiling()
     143        size = Rational((v,k)).ceil()
    144144        blocks=[]
    145145        for i in range(size-1):
    146146            blk=[]
     
    173173              database information: creator, method, timestamp
    174174    """
    175175
    176     def __init__(self, v=0, k=0, t=0, size=0, points=[], blocks=[], low_bd=0, method='', created_by ='',timestamp=''):
     176    def __init__(self, v=0, k=0, t=0, size=0, points=[], blocks=[], low_bd=0, method='', creator ='',timestamp=''):
    177177        """
    178178        EXAMPLES:
    179             sage: C=CoveringDesign(7,3,2,7,range(7),[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]],0, 'Projective Plane')
    180             sage: C.show()
    181             C(7,3,2) = 7
    182             Method: Projective Plane
    183             0   1   2   
    184             0   3   4   
    185             0   5   6   
    186             1   3   5   
    187             1   4   6   
    188             2   3   6   
    189             2   4   5   
    190         """   
    191         self.v = v
    192         self.k = k
    193         self.t = t
    194         self.size = size
    195         if low_bd > 0:
    196             self.low_bd = low_bd
    197         else:
    198             self.low_bd = schonheim(v,k,t)
    199         self.method = method
    200         self.created_by = created_by
    201         self.timestamp = timestamp
    202         self.incidence_structure = IncidenceStructure(points, blocks)
    203 
    204 
    205     def show(self, max_size=100):
    206         """
    207         Displays a covering design.
    208 
    209         INPUT:
    210             max_size -- maximum number of blocks (to avoid trying to show huge ones)
    211 
    212         OUTPUT:
    213             covering design parameters and blocks, in a readable form
    214            
    215         EXAMPLES:
    216179            sage: C=CoveringDesign(5,4,3,4,range(5),[[0,1,2,3],[0,1,2,4],[0,1,3,4],[0,2,3,4]],4, 'Lexicographic Covering')
    217             sage: C.show()
     180            sage: print C
    218181            C(5,4,3) = 4
    219182            Method: Lexicographic Covering
    220183            0   1   2   3   
    221184            0   1   2   4   
    222185            0   1   3   4   
    223186            0   2   3   4   
     187        """   
     188        self.__v = v
     189        self.__k = k
     190        self.__t = t
     191        self.__size = size
     192        if low_bd > 0:
     193            self.__low_bd = low_bd
     194        else:
     195            self.__low_bd = schonheim(v,k,t)
     196        self.__method = method
     197        self.__creator = creator
     198        self.__timestamp = timestamp
     199        self.__incidence_structure = IncidenceStructure(points, blocks)
     200
     201
     202    def __repr__(self):
    224203        """
    225         if self.size == self.low_bd:    # check if the covering is known to be optimal
    226             print 'C(%d,%d,%d) = %d'%(self.v,self.k,self.t,self.size)
     204        A print method, giving the parameters and any other information about the covering (but not the blocks).
     205
     206        EXAMPLES:
     207            sage: C=CoveringDesign(7,3,2,7,range(7),[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]],0, 'Projective Plane')
     208            sage: C
     209            (7,3,2)-covering design of size 7
     210            Lower bound: 7
     211            Method: Projective Plane
     212        """
     213        repr =  '(%d,%d,%d)-covering design of size %d\n'%(self.__v,self.__k,self.__t,self.__size)
     214        repr +=  'Lower bound: %d\n'%(self.__low_bd)
     215        if self.__creator != '':
     216            repr += 'Created by: %s\n'%(self.__creator)
     217        if self.__method != '':
     218            repr += 'Method: %s\n'%(self.__method)
     219        if self.__timestamp != '':
     220            repr += 'Submitted on: %s\n'%(self.__timestamp)
     221
     222        return repr
     223
     224                   
     225    def __str__(self):
     226        """
     227        A print method, displaying a covering design's parameters and blocks.
     228
     229        OUTPUT:
     230            covering design parameters and blocks, in a readable form
     231           
     232        EXAMPLES:
     233            sage: C=CoveringDesign(7,3,2,7,range(7),[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]],0, 'Projective Plane')
     234            sage: print C
     235            C(7,3,2) = 7
     236            Method: Projective Plane
     237            0   1   2   
     238            0   3   4   
     239            0   5   6   
     240            1   3   5
     241            1   4   6
     242            2   3   6
     243            2   4   5
     244        """
     245        if self.__size == self.__low_bd:    # check if the covering is known to be optimal
     246            repr =  'C(%d,%d,%d) = %d\n'%(self.__v,self.__k,self.__t,self.__size)
    227247        else:
    228             print '%d <= C(%d,%d,%d) <= %d'%(self.low_bd,self.v,self.k,self.t,self.size);
    229         if self.created_by != '':
    230             print 'Created by: %s'%(self.created_by)
    231         if self.method != '':
    232             print 'Method: %s'%(self.method)
    233         if self.timestamp != '':
    234             print 'Submitted on: %s'%(self.timestamp)
    235         for i in range(min(max_size, self.size)):
    236             for j in range(self.k):
    237                 print self.incidence_structure.blocks()[i][j]," ",
    238             print "\n",
    239         if(max_size < self.size):   # if not showing all blocks, indicate it with ellipsis
    240             print "..."
     248            repr = '%d <= C(%d,%d,%d) <= %d\n'%(self.__low_bd,self.__v,self.__k,self.__t,self.__size);
     249        if self.__creator != '':
     250            repr += 'Created by: %s\n'%(self.__creator)
     251        if self.__method != '':
     252            repr += 'Method: %s\n'%(self.__method)
     253        if self.__timestamp != '':
     254            repr += 'Submitted on: %s\n'%(self.__timestamp)
     255        for i in range(self.__size):
     256            for j in range(self.__k):
     257                repr = repr + str(self.__incidence_structure.blocks()[i][j]) + '  '
     258            repr += '\n'
    241259
     260        return repr
    242261
    243262                   
    244263    def is_covering(self):
     
    264283            sage: C.is_covering()
    265284            False
    266285        """
    267         v = self.v
    268         k = self.k
    269         t = self.t
     286        v = self.__v
     287        k = self.__k
     288        t = self.__t
    270289        Svt = Combinations(range(v),t)
    271290        Skt = Combinations(range(k),t)
    272291        tset = {}       # tables of t-sets: False = uncovered, True = covered
     
    274293            tset[tuple(i)] = False
    275294
    276295        # mark all t-sets covered by each block
    277         for a in self.incidence_structure.blocks():
     296        for a in self.__incidence_structure.blocks():
    278297            for z in Skt:
    279298                y = [a[x] for x in z]
    280299                tset[tuple(y)] = True
     
    286305        return True                  # everything was covered
    287306
    288307
     308    def v(self):
     309        """
     310        Return v, the number of points in the covering design
     311
     312        EXAMPLES:
     313            sage: from sage.combinat.designs.covering_design import CoveringDesign
     314            sage: C=CoveringDesign(7,3,2,7,range(7),[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]],0, 'Projective Plane')
     315            sage: C.v()
     316            7
     317        """
     318        return self.__v
     319
     320
     321    def k(self):
     322        """
     323        Return k, the size of blocks of the covering design
     324
     325        EXAMPLES:
     326            sage: from sage.combinat.designs.covering_design import CoveringDesign
     327            sage: C=CoveringDesign(7,3,2,7,range(7),[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]],0, 'Projective Plane')
     328            sage: C.k()
     329            3
     330        """
     331        return self.__k
     332
     333
     334    def t(self):
     335        """
     336        Return t, the size of sets which must be covered by the blocks of the covering design
     337
     338        EXAMPLES:
     339            sage: from sage.combinat.designs.covering_design import CoveringDesign
     340            sage: C=CoveringDesign(7,3,2,7,range(7),[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]],0, 'Projective Plane')
     341            sage: C.t()
     342            2
     343        """
     344        return self.__t
     345
     346    def size(self):
     347        """
     348        Return the number of blocks in the covering design
     349
     350        EXAMPLES:
     351            sage: from sage.combinat.designs.covering_design import CoveringDesign
     352            sage: C=CoveringDesign(7,3,2,7,range(7),[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]],0, 'Projective Plane')
     353            sage: C.size()
     354            7
     355        """
     356        return self.__size
     357
     358
     359    def low_bd(self):
     360        """
     361        Return a lower bound for the number of blocks a covering design with these parameters could have.
     362        Typically this is the Schonheim bound, but for some parameters better bounds have been shown.
     363
     364        EXAMPLES:
     365            sage: from sage.combinat.designs.covering_design import CoveringDesign
     366            sage: C=CoveringDesign(7,3,2,7,range(7),[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]],0, 'Projective Plane')
     367            sage: C.low_bd()
     368            7
     369        """
     370        return self.__low_bd
     371
     372
     373    def method(self):
     374        """
     375        Return the method used to create the covering design
     376        This field is optional, and is used in a database to give information about how coverings were constructed
     377
     378        EXAMPLES:
     379            sage: from sage.combinat.designs.covering_design import CoveringDesign
     380            sage: C=CoveringDesign(7,3,2,7,range(7),[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]],0, 'Projective Plane')
     381            sage: C.method()
     382            'Projective Plane'
     383        """
     384        return self.__method
     385
     386
     387    def creator(self):
     388        """
     389        Return the creator of the covering design
     390        This field is optional, and is used in a database to give attribution for the covering design
     391        It can refer to the person who submitted it, or who originally gave a construction
     392
     393        EXAMPLES:
     394            sage: from sage.combinat.designs.covering_design import CoveringDesign
     395            sage: C=CoveringDesign(7,3,2,7,range(7),[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]],0, 'Projective Plane','Gino Fano')
     396            sage: C.creator()
     397            'Gino Fano'
     398        """
     399        return self.__creator
     400
     401
     402    def timestamp(self):
     403        """
     404        Return the time that the covering was submitted to the database
     405
     406        EXAMPLES:
     407            sage: from sage.combinat.designs.covering_design import CoveringDesign
     408            sage: C=CoveringDesign(7,3,2,7,range(7),[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]],0, 'Projective Plane','Gino Fano','1892-01-01 00:00:00')
     409            sage: C.timestamp()  #Fano had an article in 1892, I don't know the date it appeared
     410            '1892-01-01 00:00:00'
     411        """
     412        return self.__timestamp
     413
     414
     415    def incidence_structure(self):
     416        """
     417        Return the incidence structure of a covering design, without all the extra parameters.
     418
     419        EXAMPLES:
     420            sage: from sage.combinat.designs.covering_design import CoveringDesign
     421            sage: C=CoveringDesign(7,3,2,7,range(7),[[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]],0, 'Projective Plane')
     422            sage: D = C.incidence_structure()
     423            sage: D.points()
     424            [0, 1, 2, 3, 4, 5, 6]
     425            sage: D.blocks()
     426            [[0, 1, 2], [0, 3, 4], [0, 5, 6], [1, 3, 5], [1, 4, 6], [2, 3, 6], [2, 4, 5]]
     427
     428        """
     429        return self.__incidence_structure
    289430
    290431
    291432   
     
    305446        CoveringDesign -- (v,k,t) covering design with smallest number of blocks
    306447
    307448    EXAMPLES:
    308         sage: C = best_known_covering_design_www(7, 3, 2)   # requires internet, optional
    309         sage: C.show()                                      # requires internet, optional
     449        sage: C = best_known_covering_design_www(7, 3, 2)   # optional -- requires internet
     450        sage: print C                                       # optional -- requires internet
    310451        C(7,3,2) = 7
    311452        Method: lex covering
    312453        Submitted on: 1996-12-01 00:00:00
    313         0   1   2   
    314         0   3   4   
    315         0   5   6   
    316         1   3   5   
    317         1   4   6   
    318         2   3   6   
    319         2   4   5   
     454        0  1  2   
     455        0  3  4   
     456        0  5  6   
     457        1  3  5   
     458        1  4  6   
     459        2  3  6   
     460        2  4  5   
    320461       
    321462    This function raises a ValueError if the (v,k,t) parameters are
    322463    not found in the database.