Ticket #13840: trac_13840-integer_list_speedup-ts.patch

File trac_13840-integer_list_speedup-ts.patch, 7.9 KB (added by tscrim, 9 years ago)

Fixed doctest

  • sage/combinat/integer_list.py

    # HG changeset patch
    # User Travis Scrimshaw <tscrim@ucdavis.edu>
    # Date 1359992227 28800
    # Node ID 032dd7a0e52d118410d1720dad8873b783d9f2ad
    # Parent  605233157dc94826ba55455ac55706081c914c36
    Trac #13840: Speedup of IntegerListsLex by converting to float's infinity.
    
    diff --git a/sage/combinat/integer_list.py b/sage/combinat/integer_list.py
    a b AUTHORS: 
    3030
    3131import generator
    3232from sage.rings.arith import binomial
    33 from sage.rings.infinity import infinity
    3433from sage.rings.integer_ring import ZZ
    3534from sage.misc.lazy_attribute import lazy_attribute
    3635import __builtin__
    def first(n, min_length, max_length, flo 
    5049
    5150    Preconditions:
    5251
    53     - minslope < maxslope
     52    - ``minslope < maxslope``
    5453
    55     - floor and ceiling need to satisfy the slope constraints,
    56       e.g. be obtained fromcomp2floor or comp2ceil
     54    - ``floor`` and ``ceiling`` need to satisfy the slope constraints,
     55      e.g. be obtained ``fromcomp2floor`` or ``comp2ceil``
    5756
    58     - floor must be below ceiling to ensure
     57    - ``floor`` must be below ``ceiling`` to ensure
    5958      the existence a valid composition
    6059
    6160    TESTS::
    def first(n, min_length, max_length, flo 
    118117    if n == 0: # There is nothing more to do
    119118        return result
    120119
    121     if min_slope == -infinity:
     120    if min_slope == float('-inf'):
    122121        for i in range(1, min_length+1):
    123122            if n <= ceiling(i) - result[i-1]: #-1 for indexing
    124123                result[i-1] += n
    def rightmost_pivot(comp, min_length, ma 
    259258
    260259    highX = x
    261260    lowX  = x
    262  
     261
    263262    while not (ceilingx_x >= floorx_x and
    264263               (G >= 0 or
    265264               ( y < max_length +1 and
    def rightmost_pivot(comp, min_length, ma 
    282281
    283282
    284283        #Update G
    285         if max_slope == infinity:
     284        if max_slope == float('+inf'):
    286285            #In this case, we have
    287286            #  -- ceiling_x(i) = ceiling(i) for i > x
    288287            #  --G >= 0 or G = -1
    def rightmost_pivot(comp, min_length, ma 
    303302        #Update F
    304303        if y < max_length+1:
    305304            F += comp[x-1] - floorx_x
    306             if min_slope != -infinity:
     305            if min_slope != float('-inf'):
    307306                F += (lowX - x) * (oldfloorx_x - (floorx_x + min_slope))
    308307                temp = floor(lowX) - (floorx_x + min_slope_lowX)
    309308                while lowX > x and temp >= 0:
    def next(comp, min_length, max_length, f 
    344343##     // Efficiency note: they are not wrapped more than once, since
    345344##     // the method Next calls first, but not the converse.
    346345
    347     if min_slope == -infinity:
     346    if min_slope == float('-inf'):
    348347        new_floor = lambda i: floor(x+(i-1))
    349348    else:
    350349        new_floor = lambda i: max(floor(x+(i-1)), low+(i-1)*min_slope)
    351350
    352     if max_slope == infinity:
     351    if max_slope == float('+inf'):
    353352        new_ceiling = lambda i: comp[x-1] - 1 if i == 1 else ceiling(x+(i-1))
    354353    else:
    355354        new_ceiling = lambda i: min(ceiling(x+(i-1)), high+(i-1)*max_slope)
    def iterator(n, min_length, max_length,  
    388387       
    389388        return generator.concat(iterators)
    390389    else:
    391         f =  first(n, min_length, max_length, floor, ceiling, min_slope, max_slope)
     390        f = first(n, min_length, max_length, floor, ceiling, min_slope, max_slope)
    392391        if f == None:
    393392            return generator.element(None, 0)
    394393        return generator.successor(f, succ)
    def upper_bound(min_length, max_length,  
    557556        sage: integer_list.upper_bound(0,4,f(0), f(1),-infinity,infinity)
    558557        4
    559558        sage: integer_list.upper_bound(0, infinity, f(0), f(1), -infinity, infinity)
    560         +Infinity
     559        inf
    561560        sage: integer_list.upper_bound(0, infinity, f(0), f(1), -infinity, -1)
    562561        1
    563562        sage: integer_list.upper_bound(0, infinity, f(0), f(5), -infinity, -1)
    def upper_bound(min_length, max_length,  
    566565        9
    567566    """
    568567    from sage.functions.all import floor as flr
    569     if max_length < infinity:
     568    if max_length < float('inf'):
    570569        return sum( [ ceiling(j) for j in range(max_length)] )
    571     elif max_slope < 0 and ceiling(1) < infinity:
     570    elif max_slope < 0 and ceiling(1) < float('inf'):
    572571        maxl = flr(-ceiling(1)/max_slope)
    573572        return ceiling(1)*(maxl+1) + binomial(maxl+1,2)*max_slope
    574573    #FIXME: only checking the first 10000 values, but that should generally
    def upper_bound(min_length, max_length,  
    576575    elif [ceiling(j) for j in range(10000)] == [0]*10000:
    577576        return 0
    578577    else:
    579         return infinity
     578        return float('inf')
    580579   
    581580
    582581
    class IntegerListsLex(CombinatorialClass 
    881880    """
    882881    def __init__(self,
    883882                 n,
    884                  length = None, min_length=0, max_length=infinity,
     883                 length = None, min_length=0, max_length=float('+inf'),
    885884                 floor=None, ceiling = None,
    886                  min_part = 0, max_part = infinity,
    887                  min_slope=-infinity, max_slope=infinity,
     885                 min_part = 0, max_part = float('+inf'),
     886                 min_slope=float('-inf'), max_slope=float('+inf'),
    888887                 name = None,
    889888                 element_constructor = None):
    890889        """
    class IntegerListsLex(CombinatorialClass 
    900899            sage: C.cardinality().parent() is ZZ
    901900            True
    902901        """
     902        # Convert to float infinity
     903        from sage.rings.infinity import infinity
     904        if max_slope == infinity:
     905            max_slope = float('+inf')
     906        if min_slope == -infinity:
     907            min_slope = float('-inf')
     908        if max_length == infinity:
     909            max_length = float('inf')
     910        if max_part == infinity:
     911            max_part = float('+inf')
     912
    903913        if floor is None:
    904914            self.floor_list = []
    905         elif type(floor) is type([]): # FIXME: how to refer to type list rather than the function list above?
     915        elif isinstance(floor, __builtin__.list):
    906916            self.floor_list = floor
    907917            # Make sure the floor list will make the list satisfy the constraints
    908             if max_slope != infinity:
     918            if max_slope != float('+inf'):
    909919                for i in reversed(range(len(self.floor_list)-1)):
    910920                    self.floor_list[i] = max(self.floor_list[i], self.floor_list[i+1] - max_slope)
    911             if min_slope != -infinity:
     921            if min_slope != float('-inf'):
    912922                for i in range(1, len(self.floor_list)):
    913923                    self.floor_list[i] = max(self.floor_list[i], self.floor_list[i-1] + min_slope)
    914924        else:
    915925            self.floor = floor
    916926        if ceiling is None:
    917927            self.ceiling_list = []
    918         elif type(ceiling) is type([]):
     928        elif isinstance(ceiling, __builtin__.list):
    919929            self.ceiling_list = ceiling
    920930            # Make sure the ceiling list will make the list satisfy the constraints
    921             if max_slope != infinity:
     931            if max_slope != float('+inf'):
    922932                for i in range(1, len(self.ceiling_list)):
    923933                    self.ceiling_list[i] = min(self.ceiling_list[i], self.ceiling_list[i-1] + max_slope)
    924             if min_slope != -infinity:
     934            if min_slope != float('-inf'):
    925935                for i in reversed(range(len(self.ceiling_list)-1)):
    926936                    self.ceiling_list[i] = min(self.ceiling_list[i], self.ceiling_list[i+1] - min_slope)
    927937        else:
    class IntegerListsLex(CombinatorialClass 
    10261036             3,
    10271037             <function <lambda> at 0x...>,
    10281038             <function <lambda> at 0x...>,
    1029              -Infinity,
    1030              +Infinity]
     1039             -inf,
     1040             inf]
    10311041 
    10321042        """
    10331043        return [self.min_length, self.max_length,
    class IntegerListsLex(CombinatorialClass 
    11051115            True
    11061116        """
    11071117        return type(v) is type([]) and is_a(v, *(self.build_args())) and sum(v) in self.n_range
     1118