Ticket #5891: categories-combinat-nt.patch

File categories-combinat-nt.patch, 13.2 KB (added by nthiery, 10 years ago)
  • sage/combinat/combinat.py

    Categories: cleanup of sage.combinat
    
    This patch cleans up the code in sage.combinat. In particular, it
    tries to make instances of CombinatorialClass more compatible with
    Parent (long run goal: make them actual instances of Parent):
    
     - object_class renamed to Element
     - element_class is an alias for Element
     - __call__ calls _element_constructor_
     - elements of `cc` can be constructed with cc(x)
    
    diff --git a/sage/combinat/combinat.py b/sage/combinat/combinat.py
    a b from sage.libs.all import pari 
    230230from sage.misc.prandom import randint
    231231from sage.misc.misc import prod
    232232from sage.structure.sage_object import SageObject
     233from sage.structure.parent import Parent
     234from sage.misc.lazy_attribute import lazy_attribute
    233235from sage.misc.misc import deprecation
    234236######### combinatorial sequences
    235237
    class CombinatorialClass(SageObject): 
    987989        return c
    988990    cardinality = __cardinality_from_iterator
    989991
     992    # __call__, element_class, and _element_constructor_ are poor
     993    # man's versions of those from Parent. This is for transition,
     994    # until all combinatorial classes are proper parents (in Parent)
     995    # and use coercion, etcc
     996
    990997    def __call__(self, x):
    991998        """
    992999        Returns x as an element of the combinatorial class's object class.
    class CombinatorialClass(SageObject): 
    10061013            ValueError: [2, 1] not in Partitions of the integer 5
    10071014        """
    10081015        if x in self:
    1009             return self.object_class(x)
     1016            return self._element_constructor_(x)
    10101017        else:
    10111018            raise ValueError, "%s not in %s"%(x, self)
    10121019
     1020    Element = CombinatorialObject # mostly for backward compatibility
     1021
     1022    @lazy_attribute
     1023    def element_class(self):
     1024        """
     1025       
     1026        """
     1027        assert not isinstance(self, Parent) # Raises an alert if we override the proper definition from Parent
     1028        if hasattr(self, "object_class"):
     1029            from sage.misc.misc import deprecation
     1030            deprecation("Using object_class for specifying the class of the elements of a combinatorial class is deprecated. Please use Element instead")
     1031        return self.Element
     1032
     1033    def _element_constructor_(self, x):
     1034        """
     1035       
     1036        """
     1037        assert not isinstance(self, Parent) # Raises an alert if we override the proper definition from Parent
     1038        return self.element_class(x)
     1039
    10131040    def __list_from_iterator(self):
    10141041        """
    10151042        The default implementation of list which builds the list from the
    class CombinatorialClass(SageObject): 
    10301057    list  = __list_from_iterator
    10311058
    10321059    #Set the default object class to be CombinatorialObject
    1033     object_class = CombinatorialObject
     1060    Element = CombinatorialObject
    10341061
    10351062    def __iterator_from_next(self):
    10361063        """
  • sage/combinat/composition.py

    diff --git a/sage/combinat/composition.py b/sage/combinat/composition.py
    a b class Compositions_all(InfiniteAbstractC 
    757757        """
    758758        return "Compositions of non-negative integers"
    759759
    760     object_class = Composition_class
     760    Element = Composition_class
    761761
    762762    def __contains__(self, x):
    763763        """
  • sage/combinat/finite_class.py

    diff --git a/sage/combinat/finite_class.py b/sage/combinat/finite_class.py
    a b class FiniteCombinatorialClass(Combinato 
    4848        """
    4949        self.l = list(l) # Probably would be better to use a tuple
    5050
    51     def object_class(self, x):
     51    def _element_constructor_(self, x):
    5252        """
    5353        EXAMPLES::
    5454       
    5555            sage: F = FiniteCombinatorialClass([1,2,3])
    56             sage: F.object_class(1)
     56            sage: F._element_constructor_(1)
     57            1
     58            sage: F(1)
    5759            1
    5860        """
    5961        return x
  • sage/combinat/free_module.py

    diff --git a/sage/combinat/free_module.py b/sage/combinat/free_module.py
    a b class CombinatorialFreeModuleElement(Ele 
    396396        # NT: coefficient_fast should be the default, just with appropriate assertions
    397397        # that can be turned on or off
    398398        p = self.parent()
    399         if isinstance(m, p._combinatorial_class.object_class):
    400             return self._monomial_coefficients.get(m, p.base_ring().zero_element())
    401         if m in p._combinatorial_class:
    402             return self._monomial_coefficients.get(p._combinatorial_class.object_class(m), p.base_ring().zero_element())
    403         else:
    404             raise TypeError, "you must specify an element of %s"%p._combinatorial_class
     399        if not isinstance(m, p._combinatorial_class.element_class):
     400            if m in p._combinatorial_class:
     401                m = p._combinatorial_class(m)
     402            else:
     403                raise TypeError, "you must specify an element of %s"%p._combinatorial_class
     404        return self._coefficient_fast(m)
    405405
    406406
    407407    def is_zero(self):
    class CombinatorialFreeModuleInterface(P 
    802802        if not hasattr(self, "_name"):
    803803            self._name = "CombinatorialModule -- change me"
    804804
    805         self._element_class = self.element_class
    806805        self._order = None
    807806
    808807    #_prefix = ""
    809808
     809    @lazy_attribute
     810    def _element_class(self):
     811        return self.element_class
    810812
    811813    # Should be an attribute?
    812814    def basis(self):
    class CombinatorialFreeModuleInterface(P 
    907909                return self.zero()
    908910            else:
    909911                raise TypeError, "do not know how to make x (= %s) an element of %s"%(x, self)
    910         elif (isinstance(self._combinatorial_class.object_class, type) and isinstance(x, self._combinatorial_class.object_class)) or (isinstance(x, sage.structure.element.Element) and x.parent() == self._combinatorial_class):
     912        elif (isinstance(self._combinatorial_class.element_class, type) and isinstance(x, self._combinatorial_class.element_class)) or (isinstance(x, sage.structure.element.Element) and x.parent() == self._combinatorial_class):
    911913            return self.term(x)
    912914        elif x in self._combinatorial_class:
    913915            return self.term(self._combinatorial_class(x))
  • sage/combinat/partition.py

    diff --git a/sage/combinat/partition.py b/sage/combinat/partition.py
    a b class RestrictedPartitions_nsk(Combinato 
    25082508        self.S = S
    25092509        self.S.sort()
    25102510        self.k = k
    2511         self.object_class = Partition_class
     2511
     2512    Element = Partition_class
    25122513       
    25132514    def __contains__(self, x):
    25142515        """
    class PartitionTuples_nk(CombinatorialCl 
    26362637        self.n = n
    26372638        self.k = k
    26382639
    2639     object_class = Partition_class
     2640    Element = Partition_class
    26402641
    26412642
    26422643    def __contains__(self, x):
    class Partitions_all(InfiniteAbstractCom 
    30053006        """
    30063007        pass
    30073008   
    3008     object_class = Partition_class
     3009    Element = Partition_class
    30093010
    30103011
    30113012    def cardinality(self):
    class Partitions_all(InfiniteAbstractCom 
    30823083   
    30833084       
    30843085class Partitions_n(CombinatorialClass):
    3085     object_class = Partition_class
     3086    Element = Partition_class
    30863087    def __init__(self, n):
    30873088        """
    30883089        TESTS::
    class Partitions_n(CombinatorialClass): 
    32693270
    32703271
    32713272class Partitions_parts_in(CombinatorialClass):
    3272     object_class = Partition_class
     3273    Element = Partition_class
    32733274    def __init__(self, n, parts):
    32743275        """
    32753276        TESTS::
    class PartitionsInBox_hw(CombinatorialCl 
    36733674        self.h = h
    36743675        self.w = w
    36753676        self._name = "Integer partitions which fit in a %s x %s box" % (self.h, self.w)
    3676         self._object_class = Partition_class
     3677
     3678    Element = Partition_class
    36773679
    36783680    def __contains__(self, x):
    36793681        """
  • sage/combinat/permutation.py

    diff --git a/sage/combinat/permutation.py b/sage/combinat/permutation.py
    a b class StandardPermutations_n(Combinatori 
    28252825        self.n = n
    28262826        self.object_class = Permutation_class
    28272827
     2828    def __call__(self, x):
     2829        """
     2830        A close variant of CombinatorialClass.__call__ which just
     2831        attempts to extend the permutation
     2832
     2833            sage: P = Permutations(5)
     2834            sage: P([2,3,1])
     2835            [2, 3, 1, 4, 5]
     2836        """
     2837       
     2838        if len(x) < self.n:
     2839            x = list(x) + range(len(x)+1, self.n+1)
     2840        if x in self:
     2841            return self.object_class(x)
     2842        else:
     2843            raise ValueError, "%s not in %s"%(x, self)
    28282844
    28292845    def __contains__(self,x):
    28302846        """
  • sage/combinat/permutation.py

    diff --git a/sage/combinat/permutation.py b/sage/combinat/permutation.py
    a b class StandardPermutations_all(InfiniteA 
    27552755            sage: SP == loads(dumps(SP))
    27562756            True
    27572757        """
    2758         self.object_class = Permutation_class
     2758
     2759    Element = Permutation_class
    27592760
    27602761    def __repr__(self):
    27612762        """
    class StandardPermutations_n(Combinatori 
    28232824            True
    28242825        """
    28252826        self.n = n
    2826         self.object_class = Permutation_class
     2827
     2828    Element = Permutation_class
    28272829
    28282830    def __call__(self, x):
    28292831        """
    class StandardPermutations_n(Combinatori 
    28372839       
    28382840        if len(x) < self.n:
    28392841            x = list(x) + range(len(x)+1, self.n+1)
    2840         if x in self:
    2841             return self.object_class(x)
    2842         else:
    2843             raise ValueError, "%s not in %s"%(x, self)
     2842        return super(StandardPermutations_n, self).__call__(x)
    28442843
    28452844    def __contains__(self,x):
    28462845        """
    class StandardPermutations_descents(Comb 
    32163215        """
    32173216        return "Standard permutations of %s with descents %s"%(self.n, self.d)
    32183217
    3219     object_class = Permutation_class
     3218    Element = Permutation_class
    32203219
    32213220    def first(self):
    32223221        """
    class StandardPermutations_recoilsfiner( 
    33703369        """
    33713370        return "Standard permutations whose recoils composition is finer than %s"%self.recoils
    33723371
    3373     object_class = Permutation_class
     3372    Element = Permutation_class
    33743373   
    33753374    def list(self):
    33763375        """
    class StandardPermutations_recoilsfatter 
    34283427        """
    34293428        return "Standard permutations whose recoils composition is fatter than %s"%self.recoils
    34303429
    3431     object_class = Permutation_class
     3430    Element = Permutation_class
    34323431
    34333432    def list(self):
    34343433        """
    class StandardPermutations_recoils(Combi 
    34913490        """
    34923491        return "Standard permutations whose recoils composition is %s"%self.recoils
    34933492
    3494     object_class = Permutation_class
     3493    Element = Permutation_class
    34953494
    34963495
    34973496    def list(self):
  • sage/combinat/set_partition.py

    diff --git a/sage/combinat/set_partition.py b/sage/combinat/set_partition.py
    a b def SetPartitions(s, part=None): 
    144144
    145145
    146146class SetPartitions_setparts(CombinatorialClass):
    147     object_class = Set_object_enumerated
     147    Element = Set_object_enumerated
    148148    def __init__(self, set, parts):
    149149        """
    150150        TESTS::
  • sage/combinat/skew_partition.py

    diff --git a/sage/combinat/skew_partition.py b/sage/combinat/skew_partition.py
    a b class SkewPartitions_all(CombinatorialCl 
    652652        """
    653653        pass
    654654   
    655     object_class = SkewPartition_class
     655    Element = SkewPartition_class
    656656   
    657657    def __contains__(self, x):
    658658        """
    class SkewPartitions_n(CombinatorialClas 
    777777        else:
    778778            self.overlap = overlap
    779779           
    780     object_class = SkewPartition_class
     780    Element = SkewPartition_class
    781781
    782782    def __contains__(self, x):
    783783        """
    class SkewPartitions_rowlengths(Combinat 
    959959        else:
    960960            self.overlap = overlap
    961961
    962     object_class = SkewPartition_class
     962    Element = SkewPartition_class
    963963   
    964964    def __contains__(self, x):
    965965        """
  • sage/combinat/skew_tableau.py

    diff --git a/sage/combinat/skew_tableau.py b/sage/combinat/skew_tableau.py
    a b class StandardSkewTableaux_n(Combinatori 
    788788                yield sst
    789789       
    790790class StandardSkewTableaux_skewpartition(CombinatorialClass):
    791     object_class = SkewTableau_class
     791    Element = SkewTableau_class
    792792    def __init__(self, skp):
    793793        """
    794794        TESTS::
  • sage/combinat/tableau.py

    diff --git a/sage/combinat/tableau.py b/sage/combinat/tableau.py
    a b class StandardTableaux_n(CombinatorialCl 
    17961796        """
    17971797        self.n = n
    17981798
    1799     object_class = Tableau_class
     1799    Element = Tableau_class
    18001800
    18011801    def __repr__(self):
    18021802        """
    class SemistandardTableaux_n(Combinatori 
    23292329        """
    23302330        return x in SemistandardTableaux() and sum(map(len, x)) == self.n
    23312331
    2332     object_class = Tableau_class
     2332    Element = Tableau_class
    23332333       
    23342334    def cardinality(self):
    23352335        """
    class SemistandardTableaux_pmu(Combinato 
    23922392        self.p = p
    23932393        self.mu = mu
    23942394
    2395     object_class = Tableau_class
     2395    Element = Tableau_class
    23962396
    23972397    def __repr__(self):
    23982398        """
    class SemistandardTableaux_p(Combinatori 
    24802480        """
    24812481        self.p = p
    24822482
    2483     object_class = Tableau_class
     2483    Element = Tableau_class
    24842484
    24852485    def __contains__(self, x):
    24862486        """