Ticket #13119: trac_13119_apply_module_morphism_bugfix-mz.patch

File trac_13119_apply_module_morphism_bugfix-mz.patch, 3.8 KB (added by zabrocki, 10 years ago)
  • sage/combinat/free_module.py

    # HG changeset patch
    # User Mike Zabrocki
    # Date 1353790312 18000
    # Node ID 7bd170c46b2e1f66127b19d49083908d349c9492
    # Parent 23061edf00176562a9d754123cfebbb8e3f631d3
    #13119: modifying _apply_module_morphism in free_module.py so that it handles the 0 element correctly
    
    The function _apply_module_morphism raised an error in the case that it was applied to the 0 element.
    Doc tests were added and documentation clarified.
    
    diff --git a/sage/combinat/free_module.py b/sage/combinat/free_module.py
    a b from sage.misc.all import lazy_attribute 
    2727from sage.categories.poor_man_map import PoorManMap
    2828from sage.categories.all import ModulesWithBasis
    2929from sage.combinat.dict_addition import dict_addition, dict_linear_combination
     30from sage.sets.family import Family
    3031
    3132# TODO: move the content of this class to CombinatorialFreeModule.Element and ModulesWithBasis.Element
    3233class CombinatorialFreeModuleElement(Element):
    class CombinatorialFreeModule(UniqueRepr 
    18481849
    18491850    def _apply_module_morphism( self, x, on_basis, codomain=False ):
    18501851        """
    1851         Returns the image of x under the module morphism defined by
    1852         extending f by linearity.
     1852        Returns the image of ``x`` under the module morphism defined by
     1853        extending :func:`on_basis` by linearity.
    18531854
    18541855        INPUT:
    18551856
    class CombinatorialFreeModule(UniqueRepr 
    18601861          object indexing a basis element and returns an element of the
    18611862          codomain
    18621863
    1863         - ``codomain`` (optional) - the codomain of the morphism, otherwise it is computed using on_basis
    1864 
     1864        - ``codomain`` (optional) - the codomain of the morphism, otherwise it is computed
     1865          using :func:`on_basis`
     1866
     1867        If ``codomain`` is not specified, then the function tries to compute the codomain
     1868        of the module morphism by finding the image of one of the elements in the
     1869        support, hence :func:`on_basis` should return an element whose parent is the
     1870        codomain.
    18651871
    18661872        EXAMPLES::
    18671873
    class CombinatorialFreeModule(UniqueRepr 
    18731879            6
    18741880            sage: s._apply_module_morphism(b, f) #2*(1+2+3)
    18751881            12
     1882            sage: s._apply_module_morphism(s(0), f)
     1883            0
     1884            sage: s._apply_module_morphism(s(1), f)
     1885            0
     1886            sage: s._apply_module_morphism(s(1), lambda part: len(part), ZZ)   
     1887            0
     1888            sage: s._apply_module_morphism(s(1), lambda part: len(part))   
     1889            Traceback (most recent call last):
     1890            ...
     1891            ValueError: Codomain could not be determined
    18761892        """
    18771893
    18781894        if x == self.zero():
    18791895            if not codomain:
    1880                 B = self.basis()
    1881                 keys = list( B.keys() )
    1882                 if len( keys ) > 0:
    1883                     key = keys[0]
    1884                     codomain = on_basis( key ).parent()
    1885                 else:
    1886                     raise ValueError, 'Codomain could not be determined'
    1887 
     1896                B = Family(self.basis())
     1897                try:
     1898                    z = B.first()
     1899                except StopIteration:
     1900                    raise ValueError('Codomain could not be determined')
     1901                codomain = on_basis(z).parent()
    18881902            return codomain.zero()
    1889 
    18901903        else:
    18911904            if not codomain:
    18921905                keys = x.support()
    18931906                key = keys[0]
    1894                 codomain = on_basis( key ).parent()
     1907                if hasattr(on_basis( key ), 'parent'):
     1908                    codomain = on_basis( key ).parent()
     1909                else:
     1910                    raise ValueError('Codomain could not be determined')
    18951911
    18961912            if hasattr( codomain, 'linear_combination' ):
    18971913                return codomain.linear_combination( ( on_basis( key ), coeff ) for key, coeff in x._monomial_coefficients.iteritems() )