Ticket #3662: trac_3662-2.patch

File trac_3662-2.patch, 9.7 kB (added by mhansen, 4 months ago)
  • a/sage/combinat/combinatorial_algebra.py

    old new  
    6262from sage.algebras.algebra_element import AlgebraElement 
    6363from sage.matrix.all import MatrixSpace 
    6464from sage.combinat.free_module import CombinatorialFreeModuleElement, CombinatorialFreeModule, CombinatorialFreeModuleInterface 
     65from sage.misc.misc import repr_lincomb 
    6566 
    6667class CombinatorialAlgebraElement(AlgebraElement, CombinatorialFreeModuleElement): 
    6768    def __init__(self, A, x): 
     
    225226        """ 
    226227        return self._matrix_() 
    227228 
     229 
     230    def __repr__(self): 
     231        """ 
     232        EXAMPLES: 
     233            sage: QS3 = SymmetricGroupAlgebra(QQ,3) 
     234            sage: a = 2 + QS3([2,1,3]) 
     235            sage: print a.__repr__() 
     236            2*[1, 2, 3] + [2, 1, 3] 
     237        """ 
     238        v = self._monomial_coefficients.items() 
     239        v.sort() 
     240        prefix = self.parent().prefix() 
     241        mons = [ prefix + repr(m) for (m, _) in v ] 
     242        cffs = [ x for (_, x) in v ] 
     243        x = repr_lincomb(mons, cffs).replace("*1 "," ") 
     244        if x[len(x)-2:] == "*1": 
     245            return x[:len(x)-2] 
     246        else: 
     247            return x 
     248 
    228249     
    229250class CombinatorialAlgebra(CombinatorialFreeModuleInterface, Algebra): 
    230251    def __init__(self, R, element_class = None): 
     
    256277            self._element_class = element_class 
    257278 
    258279        CombinatorialFreeModuleInterface.__init__(self, R, self._element_class) 
     280 
     281    def __call__(self, x): 
     282        try: 
     283            return CombinatorialFreeModuleInterface.__call__(self, x) 
     284        except TypeError: 
     285            pass 
     286 
     287        R = self.base_ring() 
     288        eclass = self._element_class 
     289        #Coerce elements of the base ring 
     290        if not hasattr(x, 'parent'): 
     291            x = R(x) 
     292        if x.parent() == R: 
     293            if x == R(0): 
     294                return eclass(self, {}) 
     295            else: 
     296                return eclass(self, {self._one:x}) 
     297        #Coerce things that coerce into the base ring 
     298        elif R.has_coerce_map_from(x.parent()): 
     299            rx = R(x) 
     300            if rx == R(0): 
     301                return eclass(self, {}) 
     302            else: 
     303                return eclass(self, {self._one:R(x)}) 
     304 
     305        raise TypeError, "do not know how to make x (= %s) an element of self (=%s)"%(x,self) 
     306 
     307    def _an_element_impl(self): 
     308        """ 
     309        Returns an element of self, namely the unit element. 
     310 
     311        EXAMPLES: 
     312            sage: s = SFASchur(QQ) 
     313            sage: s._an_element_impl() 
     314            s[] 
     315            sage: _.parent() is s 
     316            True 
     317        """ 
     318        return self._element_class(self, {self._one:self.base_ring()(1)}) 
     319 
    259320             
    260321    def multiply(self,left,right): 
    261322        """ 
  • a/sage/combinat/finite_class.py

    old new  
    4848        """ 
    4949        self.l = l 
    5050 
     51    def object_class(self, x): 
     52        """ 
     53        EXAMPLES: 
     54            sage: F = FiniteCombinatorialClass([1,2,3]) 
     55            sage: F.object_class(1) 
     56            1 
     57        """ 
     58        return x 
     59 
    5160    def __repr__(self): 
    5261        """ 
    5362        TESTS: 
  • a/sage/combinat/free_module.py

    old new  
    1919from sage.rings.all import Ring, Integer 
    2020import sage.structure.parent_base 
    2121from sage.combinat.family import Family 
     22from sage.combinat.finite_class import FiniteCombinatorialClass 
     23from sage.combinat.combinat import CombinatorialClass 
    2224 
    2325# TODO: 
    2426# Rewrite all tests in a more self contained way 
     
    8284    def __repr__(self): 
    8385        """ 
    8486        EXAMPLES: 
    85             sage: QS3 = SymmetricGroupAlgebra(QQ,3) 
    86             sage: a = 2 + QS3([2,1,3]) 
    87             sage: print a.__repr__() 
    88             2*[1, 2, 3] + [2, 1, 3] 
     87            sage: F = CombinatorialFreeModule(QQ, ['a', 'b', 'c'], prefix='F') 
     88            sage: e = F.basis() 
     89            sage: e['a'] + 2*e['b'] 
     90            F('a') + 2*F('b') 
     91 
    8992        """ 
    9093        v = self._monomial_coefficients.items() 
    9194        v.sort() 
    9295        prefix = self.parent().prefix() 
    93         mons = [ prefix + repr(m) for (m, _) in v ] 
     96        mons = [ prefix + "(" + repr(m) + ")" for (m, _) in v ] 
    9497        cffs = [ x for (_, x) in v ] 
    9598        x = repr_lincomb(mons, cffs).replace("*1 "," ") 
    9699        if x[len(x)-2:] == "*1": 
     
    409412            sage: a.map_coefficients(lambda x: x*2) 
    410413            2*s[2, 1] + 4*s[3, 2] 
    411414        """ 
    412         res = self.parent()(0) 
    413415        z_elt = {} 
    414416        for m,c in self.monomial_coefficients().iteritems(): 
    415417            z_elt[m] = f(c) 
    416         res._monomial_coefficients = z_elt 
    417         return res 
     418        return self.parent()._from_dict(z_elt) 
     419 
    418420 
    419421    def map_basis(self, f): 
    420422        """ 
     
    437439        res._monomial_coefficients = z_elt 
    438440        return res 
    439441 
    440     def map_mc(self, f): 
     442    def map(self, f): 
    441443        """ 
    442444        Returns a new element of self.parent() obtained 
    443445        by applying the function f to a monomial coefficient 
     
    457459            new_m, new_c = f(m,c) 
    458460            z_elt[new_m] = new_c 
    459461        return self.parent()._from_dict(z_elt) 
     462 
     463    map_mc = map 
     464 
     465    def _l_action(self, x): 
     466        x = self.base_ring()(x) 
     467        return self.map_coefficients(lambda c: x*c) 
     468 
     469    def _r_action(self, x): 
     470        x = self.base_ring()(x) 
     471        return self.map_coefficients(lambda c: c*x) 
     472 
    460473 
    461474# NT: There is nothing combinatorial here 
    462475# NT: It's really too bad that we can't have the ring as second optional argument 
     
    537550            else: 
    538551                return eclass(self, dict([ (e1,R(e2)) for e1,e2 in x._monomial_coefficients.items()])) 
    539552        #x is an element of the basis combinatorial class 
    540         elif isinstance(x, self._combinatorial_class.object_class): 
     553        elif isinstance(self._combinatorial_class.object_class, type) and isinstance(x, self._combinatorial_class.object_class): 
    541554            return eclass(self, {x:R(1)})  
    542555        elif x in self._combinatorial_class: 
    543556            return eclass(self, {self._combinatorial_class.object_class(x):R(1)}) 
    544         #Coerce elements of the base ring 
    545         elif hasattr(x, 'parent') and x.parent() is R: 
    546             if x == R(0): 
    547                 return eclass(self, {}) 
    548             else: 
    549                 return eclass(self, {self._one:x}) 
    550         #Coerce things that coerce into the base ring 
    551         elif R.has_coerce_map_from(x.parent()): 
    552             rx = R(x) 
    553             if rx == R(0): 
    554                 return eclass(self, {}) 
    555             else: 
    556                 return eclass(self, {self._one:R(x)}) 
    557557        else: 
    558558            if hasattr(self, '_coerce_end'): 
    559559                try: 
    560                     return self._coerce_start(x) 
     560                    return self._coerce_end(x) 
    561561                except TypeError: 
    562562                    pass 
    563563            raise TypeError, "do not know how to make x (= %s) an element of self (=%s)"%(x,self) 
     
    567567        Returns an element of self, namely the unit element. 
    568568 
    569569        EXAMPLES: 
    570             sage: s = SFASchur(QQ
    571             sage: s._an_element_impl() 
    572             s[] 
    573             sage: _.parent() is s 
     570            sage: F = CombinatorialFreeModule(QQ, ['a', 'b', 'c']
     571            sage: F._an_element_impl() 
     572            0 
     573            sage: _.parent() is F 
    574574            True 
    575575        """ 
    576         return self._element_class(self, {self._one:self.base_ring()(1)}) 
     576        return self._element_class(self, {}) 
    577577 
    578578    def __repr__(self): 
    579579        """ 
     
    581581            sage: QS3 = SymmetricGroupAlgebra(QQ,3) 
    582582            sage: print QS3.__repr__() 
    583583            Symmetric group algebra of order 3 over Rational Field 
    584  
    585584        """ 
    586585        return self._name + " over %s"%self.base_ring() 
    587586 
     
    744743        return self._from_dict(z_elt) 
    745744 
    746745    def term(self, i): 
    747         return self._from_dict({i:self.base_ring()._one_element}) 
     746        """ 
     747        EXAMPLES: 
     748            sage: F = CombinatorialFreeModule(QQ, ['a', 'b', 'c']) 
     749            sage: F.term('a') 
     750            B('a') 
     751        """ 
     752        return self._from_dict({i:self.base_ring().one_element()}) 
    748753     
    749754    def _from_dict(self, d, coerce=False): 
    750755        """ 
     
    773778 
    774779        return self._element_class(self, d) 
    775780 
     781 
    776782class CombinatorialFreeModule(CombinatorialFreeModuleInterface, Module): 
    777783    r""" 
    778784    EXAMPLES: 
     
    797803            sage: 2*e['a'] 
    798804            2*B('a') 
    799805            sage: e['a'] + 3*e['b'] 
    800             2*B('a') + 3*e['b'] 
     806            B('a') + 3*B('b') 
    801807    """ 
    802808 
    803809 
    804     def __init__(self, R, combinatorial_class): 
    805         self._combinatorial_class = combinatorial_class 
    806         self._name = "Free module generated by %s"%combinatorial_class 
     810    def __init__(self, R, cc, prefix="B"): 
     811        if isinstance(cc, list): 
     812            cc = FiniteCombinatorialClass(cc) 
     813        if not isinstance(cc, CombinatorialClass): 
     814            raise TypeError, "cc = (%s) must be an instance of CombinatorialClass"%cc 
     815        self._combinatorial_class = cc 
     816        self._prefix = prefix 
     817        self._name = "Free module generated by %s"%cc 
    807818        CombinatorialFreeModuleInterface.__init__(self, R, CombinatorialFreeModuleElement) 
    808819    pass