Ticket #8579: trac_8579-category-magmas-nt.patch

File trac_8579-category-magmas-nt.patch, 35.4 KB (added by nthiery, 12 years ago)

This updated patch fixes the copyright headers

  • doc/en/reference/categories.rst

    # HG changeset patch
    # User Nicolas M. Thiery <nthiery@users.sf.net>
    # Date 1269420672 -3600
    # Node ID abdbcfcbbb544bcbd703df3ee994a32a242a44d9
    # Parent  eb8df32b48945a89994d7cdbc9e02426230ff323
    #8579: Add the categories of magmas and additive magmas
    
    diff --git a/doc/en/reference/categories.rst b/doc/en/reference/categories.rst
    a b Categories 
    3030.. toctree::
    3131   :maxdepth: 2
    3232
     33   sage/categories/additive_magmas
    3334   sage/categories/algebra_ideals
    3435   sage/categories/algebra_modules
    3536   sage/categories/algebras
    Categories 
    8384   sage/categories/infinite_enumerated_sets
    8485   sage/categories/integral_domains
    8586   sage/categories/left_modules
     87   sage/categories/magmas
    8688   sage/categories/matrix_algebras
    8789   sage/categories/modular_abelian_varieties
    8890   sage/categories/modules
  • new file sage/categories/additive_magmas.py

    diff --git a/sage/categories/additive_magmas.py b/sage/categories/additive_magmas.py
    new file mode 100644
    - +  
     1r"""
     2Additive Magmas
     3"""
     4#*****************************************************************************
     5#  Copyright (C) 2010 Nicolas M. Thiery <nthiery at users.sf.net>
     6#
     7#  Distributed under the terms of the GNU General Public License (GPL)
     8#                  http://www.gnu.org/licenses/
     9#******************************************************************************
     10
     11from sage.misc.abstract_method import abstract_method, AbstractMethod
     12from sage.misc.cachefunc import cached_method
     13from sage.categories.category import Category
     14from sage.categories.sets_cat import Sets
     15from sage.structure.sage_object import have_same_parent
     16
     17class AdditiveMagmas(Category):
     18    """
     19    The category of additive magmas, i.e. sets with an binary
     20    operation ``+``.
     21
     22    EXAMPLES::
     23
     24        sage: AdditiveMagmas()
     25        Category of additive magmas
     26        sage: AdditiveMagmas().super_categories()
     27        [Category of sets]
     28        sage: AdditiveMagmas().all_super_categories()
     29        [Category of additive magmas, Category of sets, Category of sets with partial maps, Category of objects]
     30
     31    TESTS::
     32
     33        sage: C = AdditiveMagmas()
     34        sage: TestSuite(C).run()
     35
     36    """
     37
     38    @cached_method
     39    def super_categories(self):
     40        """
     41        EXAMPLES::
     42
     43            sage: AdditiveMagmas().super_categories()
     44            [Category of sets]
     45        """
     46        return [Sets()]
     47
     48    class ParentMethods:
     49
     50        def summation(self, x, y):
     51            """
     52            The binary addition operator of the semigroup
     53
     54            INPUT:
     55
     56             - ``x``, ``y`` -- elements of this additive semigroup
     57
     58            Returns the sum of ``x`` and ``y``
     59
     60            EXAMPLES::
     61
     62                sage: S = CommutativeAdditiveSemigroups().example()
     63                sage: (a,b,c,d) = S.additive_semigroup_generators()
     64                sage: S.summation(a, b)
     65                a + b
     66
     67            A parent in ``AdditiveMagmas()`` must
     68            either implement :meth:`.summation` in the parent class or
     69            ``_add_`` in the element class. By default, the addition
     70            method on elements ``x._add_(y)`` calls
     71            ``S.summation(x,y)``, and reciprocally.
     72
     73
     74            As a bonus effect, ``S.summation`` by itself models the
     75            binary function from ``S`` to ``S``::
     76
     77                sage: bin = S.summation
     78                sage: bin(a,b)
     79                a + b
     80
     81            Here, ``S.summation`` is just a bound method. Whenever
     82            possible, it is recommended to enrich ``S.summation`` with
     83            extra mathematical structure. Lazy attributes can come
     84            handy for this.
     85
     86            Todo: add an example.
     87            """
     88            return x._add_(y)
     89
     90        summation_from_element_class_add = summation
     91
     92        def __init_extra__(self):
     93            """
     94            TESTS::
     95
     96                sage: S = CommutativeAdditiveSemigroups().example()
     97                sage: (a,b,c,d) = S.additive_semigroup_generators()
     98                sage: a + b # indirect doctest
     99                a + b
     100                sage: a.__class__._add_ == a.__class__._add_parent
     101                True
     102            """
     103            # This should instead register the summation to the coercion model
     104            # But this is not yet implemented in the coercion model
     105            if (self.summation != self.summation_from_element_class_add) and hasattr(self, "element_class") and hasattr(self.element_class, "_add_parent"):
     106                self.element_class._add_ = self.element_class._add_parent
     107
     108
     109    class ElementMethods:
     110
     111        # This could eventually be moved to SageObject
     112        def __add__(self, right):
     113            r"""
     114            Sum of two elements
     115
     116            This calls the `_add_` method of ``self``, if it is
     117            available and the two elements have the same parent.
     118
     119            Otherwise, the job is delegated to the coercion model.
     120
     121            Do not override; instead implement an ``_add_`` method in the
     122            element class or a ``summation`` method in the parent class.
     123
     124            EXAMPLES::
     125
     126                sage: F = CommutativeAdditiveSemigroups().example()
     127                sage: (a,b,c,d) = F.additive_semigroup_generators()
     128                sage: a + b
     129                a + b
     130            """
     131            if have_same_parent(self, right) and hasattr(self, "_add_"):
     132                return self._add_(right)
     133            from sage.structure.element import get_coercion_model
     134            import operator
     135            return get_coercion_model().bin_op(self, right, operator.add)
     136
     137        def __radd__(self, left):
     138            r"""
     139            Handles the sum of two elements, when the left hand side
     140            needs to be coerced first.
     141
     142            EXAMPLES::
     143
     144                sage: F = CommutativeAdditiveSemigroups().example()
     145                sage: (a,b,c,d) = F.additive_semigroup_generators()
     146                sage: a.__radd__(b)
     147                a + b
     148            """
     149            if have_same_parent(left, self) and hasattr(left, "_add_"):
     150                return left._add_(self)
     151            from sage.structure.element import get_coercion_model
     152            import operator
     153            return get_coercion_model().bin_op(left, self, operator.add)
     154
     155        @abstract_method(optional = True)
     156        def _add_(self, right):
     157            """
     158            Sum of two elements
     159
     160            INPUT:
     161
     162             - ``self``, ``right`` -- two elements with the same parent
     163
     164            OUTPUT:
     165
     166             - an element of the same parent
     167
     168            EXAMPLES::
     169
     170                sage: F = CommutativeAdditiveSemigroups().example()
     171                sage: (a,b,c,d) = F.additive_semigroup_generators()
     172                sage: a._add_(b)
     173                a + b
     174            """
     175
     176        def _add_parent(self, other):
     177            r"""
     178            Returns the sum of the two elements, calculated using
     179            the ``summation`` method of the parent.
     180
     181            This is the default implementation of _add_ if
     182            ``summation`` is implemented in the parent.
     183
     184            INPUT:
     185
     186             - ``other`` -- an element of the parent of ``self``
     187
     188            OUTPUT:
     189
     190            an element of the parent of ``self``
     191
     192            EXAMPLES::
     193
     194                sage: S = CommutativeAdditiveSemigroups().example()
     195                sage: (a,b,c,d) = S.additive_semigroup_generators()
     196                sage: a._add_parent(b)
     197                a + b
     198            """
     199            return self.parent().summation(self, other)
  • sage/categories/basic.py

    diff --git a/sage/categories/basic.py b/sage/categories/basic.py
    a b Posets = PartiallyOrderedSets 
    1616# For backward compatibility; will be deprecated at some point
    1717OrderedSets = PartiallyOrderedSets
    1818
     19from additive_magmas import AdditiveMagmas
    1920from commutative_additive_semigroups import CommutativeAdditiveSemigroups
    2021from commutative_additive_monoids import CommutativeAdditiveMonoids
    2122from commutative_additive_groups import CommutativeAdditiveGroups
    2223
     24from magmas import Magmas
    2325from semigroups import Semigroups
    2426from monoids import Monoids
    2527from groups import Groups
  • sage/categories/category.py

    diff --git a/sage/categories/category.py b/sage/categories/category.py
    a b class Category(UniqueRepresentation, Sag 
    775775            [Category of groups,
    776776             Category of monoids,
    777777             Category of semigroups,
     778             Category of magmas,
    778779             Category of commutative additive monoids,
    779780             Category of commutative additive semigroups,
     781             Category of additive magmas,
    780782             Category of sets,
    781783             Category of sets with partial maps,
    782784             Category of objects]
    def category_graph(categories = None): 
    10021004
    10031005        sage: G = sage.categories.category.category_graph(categories = [Rings()])
    10041006        sage: G.vertices()
    1005         ['commutative additive groups',
    1006         'commutative additive monoids',
    1007         'commutative additive semigroups',
    1008         'monoids',
    1009         'objects',
    1010         'rings',
    1011         'rngs',
    1012         'semigroups',
    1013         'sets',
    1014         'sets with partial maps']
     1007        ['additive magmas',
     1008         'commutative additive groups',
     1009         'commutative additive monoids',
     1010         'commutative additive semigroups',
     1011         'magmas',
     1012         'monoids',
     1013         'objects',
     1014         'rings',
     1015         'rngs',
     1016         'semigroups',
     1017         'sets',
     1018         'sets with partial maps']
    10151019        sage: G.plot()
    10161020
    10171021        sage: sage.categories.category.category_graph().plot()
    class JoinCategory(Category): 
    11991203        sage: J.super_categories()
    12001204        [Category of groups, Category of commutative additive monoids]
    12011205        sage: J.all_super_categories(proper = True)
    1202         [Category of groups, Category of monoids, Category of semigroups, Category of commutative additive monoids, Category of commutative additive semigroups, Category of sets, Category of sets with partial maps, Category of objects]
    1203 
     1206        [Category of groups, Category of monoids, Category of semigroups, Category of magmas, Category of commutative additive monoids, Category of commutative additive semigroups, Category of additive magmas, Category of sets, Category of sets with partial maps, Category of objects]
    12041207    """
    12051208
    12061209    def __init__(self, super_categories, **kwds):
  • sage/categories/coalgebras.py

    diff --git a/sage/categories/coalgebras.py b/sage/categories/coalgebras.py
    a b class Coalgebras(Category_over_base_ring 
    3333         Category of commutative additive groups,
    3434         Category of commutative additive monoids,
    3535         Category of commutative additive semigroups,
     36         Category of additive magmas,
    3637         Category of sets,
    3738         Category of sets with partial maps,
    3839         Category of objects]
  • sage/categories/commutative_additive_groups.py

    diff --git a/sage/categories/commutative_additive_groups.py b/sage/categories/commutative_additive_groups.py
    a b class CommutativeAdditiveGroups(AbelianC 
    2424        sage: CommutativeAdditiveGroups().super_categories()
    2525        [Category of commutative additive monoids]
    2626        sage: CommutativeAdditiveGroups().all_super_categories()
    27         [Category of commutative additive groups, Category of commutative additive monoids, Category of commutative additive semigroups, Category of sets, Category of sets with partial maps, Category of objects]
     27        [Category of commutative additive groups, Category of commutative additive monoids, Category of commutative additive semigroups, Category of additive magmas, Category of sets, Category of sets with partial maps, Category of objects]
    2828
    2929    TESTS::
    3030
  • sage/categories/commutative_additive_monoids.py

    diff --git a/sage/categories/commutative_additive_monoids.py b/sage/categories/commutative_additive_monoids.py
    a b class CommutativeAdditiveMonoids(Categor 
    2626        sage: CommutativeAdditiveMonoids().super_categories()
    2727        [Category of commutative additive semigroups]
    2828        sage: CommutativeAdditiveMonoids().all_super_categories()
    29         [Category of commutative additive monoids, Category of commutative additive semigroups, Category of sets, Category of sets with partial maps, Category of objects]
     29        [Category of commutative additive monoids, Category of commutative additive semigroups, Category of additive magmas, Category of sets, Category of sets with partial maps, Category of objects]
    3030
    3131    TESTS::
    3232
  • sage/categories/commutative_additive_semigroups.py

    diff --git a/sage/categories/commutative_additive_semigroups.py b/sage/categories/commutative_additive_semigroups.py
    a b from sage.misc.abstract_method import ab 
    1212from sage.misc.cachefunc import cached_method
    1313from sage.categories.category import Category
    1414from sage.categories.sets_cat import Sets
     15from sage.categories.additive_magmas import AdditiveMagmas
    1516from sage.structure.sage_object import have_same_parent
    1617
    1718class CommutativeAdditiveSemigroups(Category):
    class CommutativeAdditiveSemigroups(Cate 
    2425        sage: CommutativeAdditiveSemigroups()
    2526        Category of commutative additive semigroups
    2627        sage: CommutativeAdditiveSemigroups().super_categories()
    27         [Category of sets]
     28        [Category of additive magmas]
    2829        sage: CommutativeAdditiveSemigroups().all_super_categories()
    29         [Category of commutative additive semigroups, Category of sets, Category of sets with partial maps, Category of objects]
     30        [Category of commutative additive semigroups, Category of additive magmas, Category of sets, Category of sets with partial maps, Category of objects]
    3031
    3132    TESTS::
    3233
    class CommutativeAdditiveSemigroups(Cate 
    4142        EXAMPLES::
    4243
    4344            sage: CommutativeAdditiveSemigroups().super_categories()
    44             [Category of sets]
     45            [Category of additive magmas]
    4546        """
    46         return [Sets()]
     47        return [AdditiveMagmas()]
    4748
    4849    class ParentMethods:
    4950        def _test_additive_associativity(self, **options):
  • sage/categories/finite_semigroups.py

    diff --git a/sage/categories/finite_semigroups.py b/sage/categories/finite_semigroups.py
    a b class FiniteSemigroups(Category): 
    3131        sage: FiniteSemigroups().all_super_categories()
    3232        [Category of finite semigroups,
    3333         Category of semigroups,
     34         Category of magmas,
    3435         Category of finite enumerated sets,
    3536         Category of enumerated sets,
    3637         Category of sets,
    37          Category of sets with partial maps, 
     38         Category of sets with partial maps,
    3839         Category of objects]
    3940        sage: FiniteSemigroups().example()
    4041        An example of a finite semigroup: the left regular band generated by ('a', 'b', 'c', 'd')
  • new file sage/categories/magmas.py

    diff --git a/sage/categories/magmas.py b/sage/categories/magmas.py
    new file mode 100644
    - +  
     1r"""
     2Magmas
     3"""
     4#*****************************************************************************
     5#  Copyright (C) 2010 Nicolas M. Thiery <nthiery at users.sf.net>
     6#
     7#  Distributed under the terms of the GNU General Public License (GPL)
     8#                  http://www.gnu.org/licenses/
     9#******************************************************************************
     10
     11from sage.categories.category import Category
     12from sage.misc.abstract_method import abstract_method, AbstractMethod
     13from sage.misc.cachefunc import cached_method
     14from sage.misc.lazy_attribute import lazy_attribute
     15from sage.misc.misc_c import prod
     16from sage.categories.sets_cat import Sets
     17from sage.structure.unique_representation import UniqueRepresentation
     18from sage.structure.parent import Parent
     19from sage.structure.element import Element, generic_power
     20from sage.structure.sage_object import have_same_parent
     21
     22class Magmas(Category):
     23    """
     24    The category of (multiplicative) magmas, i.e. sets with a binary
     25    operation ``*``.
     26
     27    EXAMPLES::
     28
     29        sage: Magmas()
     30        Category of magmas
     31        sage: Magmas().super_categories()
     32        [Category of sets]
     33        sage: Magmas().all_super_categories()
     34        [Category of magmas, Category of sets, Category of sets with partial maps, Category of objects]
     35
     36    TESTS::
     37
     38        sage: C = Magmas()
     39        sage: TestSuite(C).run(verbose=True)
     40        running ._test_category() . . . pass
     41        running ._test_not_implemented_methods() . . . pass
     42        running ._test_pickling() . . . pass
     43
     44    """
     45    @cached_method
     46    def super_categories(self):
     47        """
     48        EXAMPLES::
     49
     50            sage: Magmas().super_categories()
     51            [Category of sets]
     52        """
     53        return [Sets()]
     54
     55    class ParentMethods:
     56
     57        def product(self, x, y):
     58            """
     59            The binary multiplication of the magma
     60
     61            INPUT:
     62
     63             - ``x``, ``y``: elements of this magma
     64
     65            OUTPUT:
     66
     67             - an element of the magma (the product of ``x`` and ``y``)
     68
     69            EXAMPLES::
     70
     71                sage: S = Semigroups().example("free")
     72                sage: x = S('a'); y = S('b')
     73                sage: S.product(x, y)
     74                'ab'
     75
     76            A parent in ``Magmas()`` must either implement
     77            :meth:`.product` in the parent class or ``_mul_`` in the
     78            element class. By default, the addition method on elements
     79            ``x._mul_(y)`` calls ``S.product(x,y)``, and reciprocally.
     80
     81
     82            As a bonus, ``S.product`` models the binary function from
     83            ``S`` to ``S``::
     84
     85                sage: bin = S.product
     86                sage: bin(x,y)
     87                'ab'
     88
     89            Currently, ``S.product`` is just a bound method:
     90
     91                sage: bin
     92                <bound method FreeSemigroup_with_category.product of An example of a semigroup: the free semigroup generated by ('a', 'b', 'c', 'd')>
     93
     94            When Sage will support multivariate morphisms, it will be
     95            possible, and in fact recommended, to enrich ``S.product``
     96            with extra mathematical structure. This will typically be
     97            implemented using lazy attributes.
     98
     99                sage: bin                 # todo: not implemented
     100                Generic binary morphism:
     101                From: (S x S)
     102                To:   S
     103            """
     104            return x._mul_(y)
     105
     106        product_from_element_class_mul = product
     107
     108        def __init_extra__(self):
     109            """
     110                sage: S = Semigroups().example("free")
     111                sage: S('a') * S('b') # indirect doctest
     112                'ab'
     113                sage: S('a').__class__._mul_ == S('a').__class__._mul_parent
     114                True
     115            """
     116            # This should instead register the multiplication to the coercion model
     117            # But this is not yet implemented in the coercion model
     118            if (self.product != self.product_from_element_class_mul) and hasattr(self, "element_class") and hasattr(self.element_class, "_mul_parent"):
     119                self.element_class._mul_ = self.element_class._mul_parent
     120
     121    class ElementMethods:
     122
     123        def __mul__(self, right):
     124            r"""
     125            Product of two elements
     126
     127            INPUT::
     128
     129             - ``self``, ``right`` -- two elements
     130
     131            This calls the `_mul_` method of ``self``, if it is
     132            available and the two elements have the same parent.
     133
     134            Otherwise, the job is delegated to the coercion model.
     135
     136            Do not override; instead implement a ``_mul_`` method in the
     137            element class or a ``product`` method in the parent class.
     138
     139            EXAMPLES::
     140
     141                sage: S = Semigroups().example("free")
     142                sage: x = S('a'); y = S('b')
     143                sage: x * y
     144                'ab'
     145            """
     146            if have_same_parent(self, right) and hasattr(self, "_mul_"):
     147                return self._mul_(right)
     148            from sage.structure.element import get_coercion_model
     149            import operator
     150            return get_coercion_model().bin_op(self, right, operator.mul)
     151
     152        __imul__ = __mul__
     153
     154        @abstract_method(optional = True)
     155        def _mul_(self, right):
     156            """
     157            Product of two elements
     158
     159            INPUT::
     160
     161             - ``self``, ``right`` -- two elements with the same parent
     162
     163            OUTPUT::
     164
     165             - an element of the same parent
     166
     167            EXAMPLES::
     168
     169                sage: S = Semigroups().example("free")
     170                sage: x = S('a'); y = S('b')
     171                sage: x._mul_(y)
     172                'ab'
     173            """
     174
     175        def _mul_parent(self, other):
     176            r"""
     177            Returns the product of the two elements, calculated using
     178            the ``product`` method of the parent.
     179
     180            This is the default implementation of _mul_ if
     181            ``product`` is implemented in the parent.
     182
     183            INPUT::
     184
     185             - ``other`` -- an element of the parent of ``self``
     186
     187            OUTPUT::
     188
     189             - an element of the parent of ``self``
     190
     191            EXAMPLES::
     192
     193                sage: S = Semigroups().example("free")
     194                sage: x = S('a'); y = S('b')
     195                sage: x._mul_parent(y)
     196                'ab'
     197
     198            """
     199            return self.parent().product(self, other)
     200
     201        def is_idempotent(self):
     202            r"""
     203            Test whether ``self`` is idempotent.
     204
     205            EXAMPLES::
     206
     207                sage: S = Semigroups().example("free"); S
     208                An example of a semigroup: the free semigroup generated by ('a', 'b', 'c', 'd')
     209                sage: a = S('a')
     210                sage: a^2
     211                'aa'
     212                sage: a.is_idempotent()
     213                False
     214
     215            ::
     216
     217                sage: L = Semigroups().example("leftzero"); L
     218                An example of a semigroup: the left zero semigroup
     219                sage: x = L('x')
     220                sage: x^2
     221                'x'
     222                sage: x.is_idempotent()
     223                True
     224
     225            """
     226            return self * self == self
  • sage/categories/modules.py

    diff --git a/sage/categories/modules.py b/sage/categories/modules.py
    a b class Modules(Category_module): 
    4141         Category of commutative additive groups,
    4242         Category of commutative additive monoids,
    4343         Category of commutative additive semigroups,
     44         Category of additive magmas,
    4445         Category of sets,
    45          Category of sets with partial maps, 
     46         Category of sets with partial maps,
    4647         Category of objects]
    4748
    4849        sage: Modules(ZZ).super_categories()
  • sage/categories/monoids.py

    diff --git a/sage/categories/monoids.py b/sage/categories/monoids.py
    a b class Monoids(Category): 
    3131        sage: Monoids().all_super_categories()
    3232        [Category of monoids,
    3333         Category of semigroups,
     34         Category of magmas,
    3435         Category of sets,
    35          Category of sets with partial maps, 
     36         Category of sets with partial maps,
    3637         Category of objects]
    3738
    3839    TESTS::
  • sage/categories/primer.py

    diff --git a/sage/categories/primer.py b/sage/categories/primer.py
    a b Example of mathematical information:: 
    124124         Category of commutative additive groups,
    125125         Category of commutative additive monoids,
    126126         Category of commutative additive semigroups,
     127         Category of additive magmas,
    127128         Category of monoids,
    128129         Category of semigroups,
     130         Category of magmas,
    129131         Category of sets,
    130          Category of sets with partial maps, 
     132         Category of sets with partial maps,
    131133         Category of objects]
    132134
    133135        sage: EuclideanDomains().category_graph().plot(talk = True)
    categories translates into *inheritance* 
    295297    sage: FiniteSemigroups().all_super_categories()
    296298    [Category of finite semigroups,
    297299     Category of semigroups,
     300     Category of magmas,
    298301     Category of finite enumerated sets,
    299302     Category of enumerated sets,
    300303     Category of sets,
    301      Category of sets with partial maps, 
     304     Category of sets with partial maps,
    302305     Category of objects]
    303306    sage: S.__class__.mro()
    304307    [<class 'sage.categories.examples.finite_semigroups.LeftRegularBand_with_category'>,
    categories translates into *inheritance* 
    309312     <type 'sage.structure.sage_object.SageObject'>,
    310313     <class 'sage.categories.finite_semigroups.FiniteSemigroups.parent_class'>,
    311314     <class 'sage.categories.semigroups.Semigroups.parent_class'>,
     315     <class 'sage.categories.magmas.Magmas.parent_class'>,
    312316     <class 'sage.categories.finite_enumerated_sets.FiniteEnumeratedSets.parent_class'>,
    313317     <class 'sage.categories.enumerated_sets.EnumeratedSets.parent_class'>,
    314318     <class 'sage.categories.sets_cat.Sets.parent_class'>,
    categories translates into *inheritance* 
    323327     <type 'sage.structure.sage_object.SageObject'>,
    324328     <class 'sage.categories.category.FiniteSemigroups.element_class'>,
    325329     <class 'sage.categories.semigroups.Semigroups.element_class'>,
     330     <class 'sage.categories.magmas.Magmas.element_class'>,
    326331     <class 'sage.categories.category.FiniteEnumeratedSets.element_class'>,
    327332     <class 'sage.categories.enumerated_sets.EnumeratedSets.element_class'>,
    328333     <class 'sage.categories.sets_cat.Sets.element_class'>,
    This gives the following order:: 
    497502     Category of rngs,
    498503     Category of monoids,
    499504     Category of semigroups,
     505     Category of magmas,
    500506     Category of coalgebras over Rational Field,
    501507     Category of modules over Rational Field,
    502508     Category of bimodules over Rational Field on the left and Rational Field on the right,
    This gives the following order:: 
    505511     Category of commutative additive groups,
    506512     Category of commutative additive monoids,
    507513     Category of commutative additive semigroups,
     514     Category of additive magmas,
    508515     Category of sets,
    509516     Category of sets with partial maps,
    510517     Category of objects]
  • sage/categories/semigroups.py

    diff --git a/sage/categories/semigroups.py b/sage/categories/semigroups.py
    a b from sage.misc.abstract_method import ab 
    1717from sage.misc.cachefunc import cached_method
    1818from sage.misc.lazy_attribute import lazy_attribute
    1919from sage.misc.misc_c import prod
    20 from sage.categories.sets_cat import Sets
     20from sage.categories.magmas import Magmas
    2121from sage.structure.unique_representation import UniqueRepresentation
    2222from sage.structure.parent import Parent
    2323from sage.structure.element import Element, generic_power
    class Semigroups(Category): 
    3333        sage: Semigroups()
    3434        Category of semigroups
    3535        sage: Semigroups().super_categories()
    36         [Category of sets]
     36        [Category of magmas]
    3737        sage: Semigroups().all_super_categories()
    38         [Category of semigroups, Category of sets, Category of sets with partial maps, Category of objects]
     38        [Category of semigroups, Category of magmas, Category of sets, Category of sets with partial maps, Category of objects]
    3939
    4040    TESTS::
    4141
    class Semigroups(Category): 
    5252        EXAMPLES::
    5353
    5454            sage: Semigroups().super_categories()
    55             [Category of sets]
     55            [Category of magmas]
    5656        """
    57         return [Sets()]
     57        return [Magmas()]
    5858
    5959    def example(self, choice="leftzero", **kwds):
    6060        r"""
    class Semigroups(Category): 
    117117                    for z in tester.some_elements():
    118118                        tester.assert_((x * y) * z == x * (y * z))
    119119
    120         def product(self, x, y):
    121             """
    122             The binary multiplication of the semigroup
    123 
    124             INPUT:
    125 
    126              - ``x``, ``y``: elements of this semigroup
    127 
    128             OUTPUT:
    129 
    130              - an element of the semigroup (the product of ``x`` and ``y``)
    131 
    132             EXAMPLES::
    133 
    134                 sage: S = Semigroups().example("free")
    135                 sage: x = S('a'); y = S('b')
    136                 sage: S.product(x, y)
    137                 'ab'
    138 
    139             A parent in ``Semigroups()`` must either implement
    140             :meth:`.product` in the parent class or ``_mul_`` in the
    141             element class. By default, the addition method on elements
    142             ``x._mul_(y)`` calls ``S.product(x,y)``, and reciprocally.
    143 
    144 
    145             As a bonus, ``S.product`` models the binary function from
    146             ``S`` to ``S``::
    147 
    148                 sage: bin = S.product
    149                 sage: bin(x,y)
    150                 'ab'
    151 
    152             Currently, ``S.product`` is just a bound method:
    153 
    154                 sage: S.rename("S")
    155                 sage: bin
    156                 <bound method FreeSemigroup_with_category.product of S>
    157 
    158             When Sage will support multivariate morphisms, it will be
    159             possible, and in fact recommended, to enrich ``S.product``
    160             with extra mathematical structure. This will typically be
    161             implemented using lazy attributes.
    162 
    163                 sage: bin                 # todo: not implemented
    164                 Generic binary morphism:
    165                 From: (S x S)
    166                 To:   S
    167             """
    168             return x._mul_(y)
    169 
    170         product_from_element_class_mul = product
    171 
    172120        def prod(self, args):
    173121            r"""
    174122            Returns the product of the list of elements `args` inside `self`.
    class Semigroups(Category): 
    186134            assert len(args) > 0, "Cannot compute an empty product in a semigroup"
    187135            return prod(args[1:], args[0])
    188136
    189         def __init_extra__(self):
    190             """
    191                 sage: S = Semigroups().example("free")
    192                 sage: S('a') * S('b') # indirect doctest
    193                 'ab'
    194                 sage: S('a').__class__._mul_ == S('a').__class__._mul_parent
    195                 True
    196             """
    197             # This should instead register the multiplication to the coercion model
    198             # But this is not yet implemented in the coercion model
    199             if (self.product != self.product_from_element_class_mul) and hasattr(self, "element_class") and hasattr(self.element_class, "_mul_parent"):
    200                 self.element_class._mul_ = self.element_class._mul_parent
    201 
    202137        def cayley_graph(self, side="right", simple=False, elements = None, generators = None, connecting_set = None):
    203138            r"""
    204139
    class Semigroups(Category): 
    356291
    357292    class ElementMethods:
    358293
    359         def __mul__(self, right):
    360             r"""
    361             Product of two elements
    362 
    363             INPUT::
    364 
    365              - ``self``, ``right`` -- two elements
    366 
    367             This calls the `_mul_` method of ``self``, if it is
    368             available and the two elements have the same parent.
    369 
    370             Otherwise, the job is delegated to the coercion model.
    371 
    372             Do not override; instead implement a ``_mul_`` method in the
    373             element class or a ``product`` method in the parent class.
    374 
    375             EXAMPLES::
    376 
    377                 sage: S = Semigroups().example("free")
    378                 sage: x = S('a'); y = S('b')
    379                 sage: x * y
    380                 'ab'
    381             """
    382             if have_same_parent(self, right) and hasattr(self, "_mul_"):
    383                 return self._mul_(right)
    384             from sage.structure.element import get_coercion_model
    385             import operator
    386             return get_coercion_model().bin_op(self, right, operator.mul)
    387 
    388         __imul__ = __mul__
    389 
    390         @abstract_method(optional = True)
    391         def _mul_(self, right):
    392             """
    393             Product of two elements
    394 
    395             INPUT::
    396 
    397              - ``self``, ``right`` -- two elements with the same parent
    398 
    399             OUTPUT::
    400 
    401              - an element of the same parent
    402 
    403             EXAMPLES::
    404 
    405                 sage: S = Semigroups().example("free")
    406                 sage: x = S('a'); y = S('b')
    407                 sage: x._mul_(y)
    408                 'ab'
    409             """
    410 
    411         def _mul_parent(self, other):
    412             r"""
    413             Returns the product of the two elements, calculated using
    414             the ``product`` method of the parent.
    415 
    416             This is the default implementation of _mul_ if
    417             ``product`` is implemented in the parent.
    418 
    419             INPUT::
    420 
    421              - ``other`` -- an element of the parent of ``self``
    422 
    423             OUTPUT::
    424 
    425              - an element of the parent of ``self``
    426 
    427             EXAMPLES::
    428 
    429                 sage: S = Semigroups().example("free")
    430                 sage: x = S('a'); y = S('b')
    431                 sage: x._mul_parent(y)
    432                 'ab'
    433 
    434             """
    435             return self.parent().product(self, other)
    436 
    437294        def _pow_(self, n):
    438295            """
    439296            Returns self to the $n^{th}$ power.
    class Semigroups(Category): 
    464321
    465322        __pow__ = _pow_
    466323
    467         def is_idempotent(self):
    468             r"""
    469             Test whether ``self`` is idempotent.
    470 
    471             EXAMPLES::
    472 
    473                 sage: S = Semigroups().example("free"); S
    474                 An example of a semigroup: the free semigroup generated by ('a', 'b', 'c', 'd')
    475                 sage: a = S('a')
    476                 sage: a^2
    477                 'aa'
    478                 sage: a.is_idempotent()
    479                 False
    480 
    481             ::
    482 
    483                 sage: L = Semigroups().example("leftzero"); L
    484                 An example of a semigroup: the left zero semigroup
    485                 sage: x = L('x')
    486                 sage: x^2
    487                 'x'
    488                 sage: x.is_idempotent()
    489                 True
    490 
    491             """
    492             return self * self == self
    493 
    494 
    495324    #######################################
    496325    class SubQuotients(Category): # (SubQuotientCategory):
    497326        r"""
  • sage/structure/category_object.pyx

    diff --git a/sage/structure/category_object.pyx b/sage/structure/category_object.pyx
    a b cdef class CategoryObject(sage_object.Sa 
    177177             Category of commutative additive groups,
    178178             Category of commutative additive monoids,
    179179             Category of commutative additive semigroups,
     180             Category of additive magmas,
    180181             Category of monoids,
    181182             Category of semigroups,
    182              Category of sets,
     183             Category of magmas,
     184             Category of sets,
    183185             Category of sets with partial maps,
    184186             Category of objects]
    185187        """
  • sage/structure/element.pyx

    diff --git a/sage/structure/element.pyx b/sage/structure/element.pyx
    a b cdef class Element(sage_object.SageObjec 
    280280            sage: 1.is_idempotent(), 2.is_idempotent()
    281281            (True, False)
    282282
    283         This method is actually provided by the ``Semigroups()`` super
     283        This method is actually provided by the ``Magmas()`` super
    284284        category of ``CommutativeRings()``::
    285285
    286286            sage: 1.is_idempotent
    287287            <bound method EuclideanDomains.element_class.is_idempotent of 1>
    288288            sage: 1.is_idempotent.__module__
    289             'sage.categories.semigroups'
     289            'sage.categories.magmas'
    290290
    291291        TESTS::
    292292