Ticket #3662: trac_3662-3.patch

File trac_3662-3.patch, 6.0 kB (added by mhansen, 4 months ago)
  • a/sage/combinat/free_module.py

    old new  
    227227 
    228228        EXAMPLES: 
    229229            sage: p = Partition([2,1]) 
     230            sage: q = Partition([1,1,1]) 
    230231            sage: s = SFASchur(QQ) 
    231             sage: a = s([2,1]
     232            sage: a = s(p
    232233            sage: a._coefficient_fast([2,1]) 
    233234            Traceback (most recent call last): 
    234235            ... 
     
    237238            1 
    238239            sage: a._coefficient_fast(p, 2) 
    239240            1 
     241            sage: a._coefficient_fast(q) 
     242            0 
     243            sage: a._coefficient_fast(q, 2) 
     244            2 
     245            sage: a[p] 
     246            1 
     247            sage: a[q] 
     248            0 
    240249        """ 
    241250        if default is None: 
    242251            default = self.base_ring()(0) 
    243252        return self._monomial_coefficients.get(m, default) 
     253 
     254    __getitem__ = _coefficient_fast 
    244255 
    245256    def coefficient(self, m): 
    246257        """ 
     
    470481        x = self.base_ring()(x) 
    471482        return self.map_coefficients(lambda c: c*x) 
    472483 
     484    def __div__(self, x): 
     485        """ 
     486        Division by coefficients 
     487 
     488        EXAMPLES: 
     489            sage: F = CombinatorialFreeModule (QQ, [1,2,3]) 
     490            sage: x = F._from_dict({1:2, 2:3}) 
     491            sage: x/2 
     492            B(1) + 3/2*B(2) 
     493        """ 
     494        # FIXME: make this robust, especially over rings! 
     495        x = self.base_ring()(x) 
     496        return self.map_coefficients(lambda c: c/x) 
    473497 
    474498# NT: There is nothing combinatorial here 
    475499# NT: It's really too bad that we can't have the ring as second optional argument 
     
    521545            2*[1, 2, 3] 
    522546            sage: QS3([2,3,1]) 
    523547            [2, 3, 1] 
     548            sage: F = CombinatorialFreeModule(QQ,[0,1]) 
     549            sage: F(0) 
     550            0 
    524551        """ 
    525552        R = self.base_ring() 
    526553        eclass = self._element_class 
     
    529556        if isinstance(x, int): 
    530557            x = Integer(x) 
    531558 
     559        # Workaround when the indexing set contains 0 
     560        if isinstance(x, Integer) and x == 0: 
     561            return self.zero() 
    532562 
    533563        if hasattr(self, '_coerce_start'): 
    534564            try: 
     
    750780            B('a') 
    751781        """ 
    752782        return self._from_dict({i:self.base_ring().one_element()}) 
     783 
     784    def zero(self): 
     785        # TODO: cache 
     786        """ 
     787        EXAMPLES: 
     788            sage: F = CombinatorialFreeModule(QQ, ['a', 'b', 'c']) 
     789            sage: F.zero() 
     790            0 
     791        """ 
     792        return self._from_dict({}) 
    753793     
     794    def sum(self, operands): 
     795        """ 
     796        EXAMPLES: 
     797            sage: F = CombinatorialFreeModule(QQ, [1,2,3,4]) 
     798            sage: F.sum(F.term(i) for i in [1,2,3]) 
     799            B(1) + B(2) + B(3) 
     800        """ 
     801        return sum(operands, self.zero()) 
     802 
    754803    def _from_dict(self, d, coerce=False): 
    755804        """ 
    756805        Given a monomial coefficient dictionary d, return the element 
     
    785834        We construct a free module whose basis is indexed by the letters a,b,c: 
    786835 
    787836            sage: F = CombinatorialFreeModule(QQ, ['a','b','c']) 
    788             sage: F 
     837            sage: F  # todo: not implemented: F.basis().keys() should be ['a','b','c'] 
    789838            Free module generated by ['a', 'b', 'c'] over Rational Field 
    790839 
    791840        Its basis is a family, indexed by a,b,c: 
    792         FIXME in family: we should preserve the order of the indices 
     841        Caveat in family: the order of the indices is not preserved 
    793842            sage: e = F.basis() 
    794843 
    795             sage: e.keys() 
     844            sage: e.keys()  # todo: not implemented 
    796845            ['a', 'b', 'c'] 
    797             sage: list(e) 
     846            sage: list(e)   # todo: not implemented 
    798847            [B('a'), B('b'), B('c')] 
    799848 
    800849        Let us construct some elements, and compute with them: 
     
    807856    """ 
    808857 
    809858 
    810     def __init__(self, R, cc, prefix="B"): 
     859    def __init__(self, R, cc, element_class = CombinatorialFreeModuleElement, prefix="B"): 
    811860        if isinstance(cc, list): 
    812861            cc = FiniteCombinatorialClass(cc) 
    813862        if not isinstance(cc, CombinatorialClass): 
     
    815864        self._combinatorial_class = cc 
    816865        self._prefix = prefix 
    817866        self._name = "Free module generated by %s"%cc 
    818         CombinatorialFreeModuleInterface.__init__(self, R, CombinatorialFreeModuleElement
     867        CombinatorialFreeModuleInterface.__init__(self, R, element_class
    819868    pass 
  • a/sage/combinat/sf/sfa.py

    old new  
    354354        f = lambda m,c: (m, c*prod([expr_k(k) for k in m])) 
    355355        return self(p_x.map_mc(f)) 
    356356 
    357  
     357    # TODO: 
     358    #  - lift to combinatorial_module 
     359    #  - rename to _apply_bimodule_morphism or generalize to true multi_module 
     360    #  - generalization with a "neighbor function" that given x says 
     361    #    for which y one has f(x,y) != 0 
     362    #  - add option orthonormal 
    358363    def _apply_multi_module_morphism(self, x, y, f, orthogonal=False): 
    359364        """ 
    360365        INPUT: 
     
    380385            sage: s._apply_multi_module_morphism(a,b,f2,orthogonal=True)  #2+2 
    381386            4 
    382387        """ 
     388        # broken for most coeff ring 
    383389        res = 0 
    384390        if orthogonal: 
     391            # could check which of x and y has less terms 
     392            # for mx, cx in x: 
    385393            for mx, cx in x._monomial_coefficients.iteritems(): 
     394                # if not y.has_key(mx): 
    386395                if mx not in y._monomial_coefficients: 
    387396                    continue 
    388397                else: 
     398                    # cy = y[mx] 
    389399                    cy = y._monomial_coefficients[mx] 
     400                # might as well call f(mx) 
    390401                res += cx*cy*f(mx, mx) 
    391402            return res 
    392403        else: