# 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 |
27 | 27 | from sage.categories.poor_man_map import PoorManMap |
28 | 28 | from sage.categories.all import ModulesWithBasis |
29 | 29 | from sage.combinat.dict_addition import dict_addition, dict_linear_combination |
| 30 | from sage.sets.family import Family |
30 | 31 | |
31 | 32 | # TODO: move the content of this class to CombinatorialFreeModule.Element and ModulesWithBasis.Element |
32 | 33 | class CombinatorialFreeModuleElement(Element): |
… |
… |
class CombinatorialFreeModule(UniqueRepr |
1848 | 1849 | |
1849 | 1850 | def _apply_module_morphism( self, x, on_basis, codomain=False ): |
1850 | 1851 | """ |
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. |
1853 | 1854 | |
1854 | 1855 | INPUT: |
1855 | 1856 | |
… |
… |
class CombinatorialFreeModule(UniqueRepr |
1860 | 1861 | object indexing a basis element and returns an element of the |
1861 | 1862 | codomain |
1862 | 1863 | |
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. |
1865 | 1871 | |
1866 | 1872 | EXAMPLES:: |
1867 | 1873 | |
… |
… |
class CombinatorialFreeModule(UniqueRepr |
1873 | 1879 | 6 |
1874 | 1880 | sage: s._apply_module_morphism(b, f) #2*(1+2+3) |
1875 | 1881 | 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 |
1876 | 1892 | """ |
1877 | 1893 | |
1878 | 1894 | if x == self.zero(): |
1879 | 1895 | 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() |
1888 | 1902 | return codomain.zero() |
1889 | | |
1890 | 1903 | else: |
1891 | 1904 | if not codomain: |
1892 | 1905 | keys = x.support() |
1893 | 1906 | 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') |
1895 | 1911 | |
1896 | 1912 | if hasattr( codomain, 'linear_combination' ): |
1897 | 1913 | return codomain.linear_combination( ( on_basis( key ), coeff ) for key, coeff in x._monomial_coefficients.iteritems() ) |