Ticket #8579: trac_8579-category-magmas-nt.patch
File trac_8579-category-magmas-nt.patch, 35.4 KB (added by , 12 years ago) |
---|
-
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 30 30 .. toctree:: 31 31 :maxdepth: 2 32 32 33 sage/categories/additive_magmas 33 34 sage/categories/algebra_ideals 34 35 sage/categories/algebra_modules 35 36 sage/categories/algebras … … Categories 83 84 sage/categories/infinite_enumerated_sets 84 85 sage/categories/integral_domains 85 86 sage/categories/left_modules 87 sage/categories/magmas 86 88 sage/categories/matrix_algebras 87 89 sage/categories/modular_abelian_varieties 88 90 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
- + 1 r""" 2 Additive 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 11 from sage.misc.abstract_method import abstract_method, AbstractMethod 12 from sage.misc.cachefunc import cached_method 13 from sage.categories.category import Category 14 from sage.categories.sets_cat import Sets 15 from sage.structure.sage_object import have_same_parent 16 17 class 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 16 16 # For backward compatibility; will be deprecated at some point 17 17 OrderedSets = PartiallyOrderedSets 18 18 19 from additive_magmas import AdditiveMagmas 19 20 from commutative_additive_semigroups import CommutativeAdditiveSemigroups 20 21 from commutative_additive_monoids import CommutativeAdditiveMonoids 21 22 from commutative_additive_groups import CommutativeAdditiveGroups 22 23 24 from magmas import Magmas 23 25 from semigroups import Semigroups 24 26 from monoids import Monoids 25 27 from 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 775 775 [Category of groups, 776 776 Category of monoids, 777 777 Category of semigroups, 778 Category of magmas, 778 779 Category of commutative additive monoids, 779 780 Category of commutative additive semigroups, 781 Category of additive magmas, 780 782 Category of sets, 781 783 Category of sets with partial maps, 782 784 Category of objects] … … def category_graph(categories = None): 1002 1004 1003 1005 sage: G = sage.categories.category.category_graph(categories = [Rings()]) 1004 1006 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'] 1015 1019 sage: G.plot() 1016 1020 1017 1021 sage: sage.categories.category.category_graph().plot() … … class JoinCategory(Category): 1199 1203 sage: J.super_categories() 1200 1204 [Category of groups, Category of commutative additive monoids] 1201 1205 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] 1204 1207 """ 1205 1208 1206 1209 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 33 33 Category of commutative additive groups, 34 34 Category of commutative additive monoids, 35 35 Category of commutative additive semigroups, 36 Category of additive magmas, 36 37 Category of sets, 37 38 Category of sets with partial maps, 38 39 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 24 24 sage: CommutativeAdditiveGroups().super_categories() 25 25 [Category of commutative additive monoids] 26 26 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] 28 28 29 29 TESTS:: 30 30 -
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 26 26 sage: CommutativeAdditiveMonoids().super_categories() 27 27 [Category of commutative additive semigroups] 28 28 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] 30 30 31 31 TESTS:: 32 32 -
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 12 12 from sage.misc.cachefunc import cached_method 13 13 from sage.categories.category import Category 14 14 from sage.categories.sets_cat import Sets 15 from sage.categories.additive_magmas import AdditiveMagmas 15 16 from sage.structure.sage_object import have_same_parent 16 17 17 18 class CommutativeAdditiveSemigroups(Category): … … class CommutativeAdditiveSemigroups(Cate 24 25 sage: CommutativeAdditiveSemigroups() 25 26 Category of commutative additive semigroups 26 27 sage: CommutativeAdditiveSemigroups().super_categories() 27 [Category of sets]28 [Category of additive magmas] 28 29 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] 30 31 31 32 TESTS:: 32 33 … … class CommutativeAdditiveSemigroups(Cate 41 42 EXAMPLES:: 42 43 43 44 sage: CommutativeAdditiveSemigroups().super_categories() 44 [Category of sets]45 [Category of additive magmas] 45 46 """ 46 return [ Sets()]47 return [AdditiveMagmas()] 47 48 48 49 class ParentMethods: 49 50 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): 31 31 sage: FiniteSemigroups().all_super_categories() 32 32 [Category of finite semigroups, 33 33 Category of semigroups, 34 Category of magmas, 34 35 Category of finite enumerated sets, 35 36 Category of enumerated sets, 36 37 Category of sets, 37 Category of sets with partial maps, 38 Category of sets with partial maps, 38 39 Category of objects] 39 40 sage: FiniteSemigroups().example() 40 41 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
- + 1 r""" 2 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 11 from sage.categories.category import Category 12 from sage.misc.abstract_method import abstract_method, AbstractMethod 13 from sage.misc.cachefunc import cached_method 14 from sage.misc.lazy_attribute import lazy_attribute 15 from sage.misc.misc_c import prod 16 from sage.categories.sets_cat import Sets 17 from sage.structure.unique_representation import UniqueRepresentation 18 from sage.structure.parent import Parent 19 from sage.structure.element import Element, generic_power 20 from sage.structure.sage_object import have_same_parent 21 22 class 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): 41 41 Category of commutative additive groups, 42 42 Category of commutative additive monoids, 43 43 Category of commutative additive semigroups, 44 Category of additive magmas, 44 45 Category of sets, 45 Category of sets with partial maps, 46 Category of sets with partial maps, 46 47 Category of objects] 47 48 48 49 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): 31 31 sage: Monoids().all_super_categories() 32 32 [Category of monoids, 33 33 Category of semigroups, 34 Category of magmas, 34 35 Category of sets, 35 Category of sets with partial maps, 36 Category of sets with partial maps, 36 37 Category of objects] 37 38 38 39 TESTS:: -
sage/categories/primer.py
diff --git a/sage/categories/primer.py b/sage/categories/primer.py
a b Example of mathematical information:: 124 124 Category of commutative additive groups, 125 125 Category of commutative additive monoids, 126 126 Category of commutative additive semigroups, 127 Category of additive magmas, 127 128 Category of monoids, 128 129 Category of semigroups, 130 Category of magmas, 129 131 Category of sets, 130 Category of sets with partial maps, 132 Category of sets with partial maps, 131 133 Category of objects] 132 134 133 135 sage: EuclideanDomains().category_graph().plot(talk = True) … … categories translates into *inheritance* 295 297 sage: FiniteSemigroups().all_super_categories() 296 298 [Category of finite semigroups, 297 299 Category of semigroups, 300 Category of magmas, 298 301 Category of finite enumerated sets, 299 302 Category of enumerated sets, 300 303 Category of sets, 301 Category of sets with partial maps, 304 Category of sets with partial maps, 302 305 Category of objects] 303 306 sage: S.__class__.mro() 304 307 [<class 'sage.categories.examples.finite_semigroups.LeftRegularBand_with_category'>, … … categories translates into *inheritance* 309 312 <type 'sage.structure.sage_object.SageObject'>, 310 313 <class 'sage.categories.finite_semigroups.FiniteSemigroups.parent_class'>, 311 314 <class 'sage.categories.semigroups.Semigroups.parent_class'>, 315 <class 'sage.categories.magmas.Magmas.parent_class'>, 312 316 <class 'sage.categories.finite_enumerated_sets.FiniteEnumeratedSets.parent_class'>, 313 317 <class 'sage.categories.enumerated_sets.EnumeratedSets.parent_class'>, 314 318 <class 'sage.categories.sets_cat.Sets.parent_class'>, … … categories translates into *inheritance* 323 327 <type 'sage.structure.sage_object.SageObject'>, 324 328 <class 'sage.categories.category.FiniteSemigroups.element_class'>, 325 329 <class 'sage.categories.semigroups.Semigroups.element_class'>, 330 <class 'sage.categories.magmas.Magmas.element_class'>, 326 331 <class 'sage.categories.category.FiniteEnumeratedSets.element_class'>, 327 332 <class 'sage.categories.enumerated_sets.EnumeratedSets.element_class'>, 328 333 <class 'sage.categories.sets_cat.Sets.element_class'>, … … This gives the following order:: 497 502 Category of rngs, 498 503 Category of monoids, 499 504 Category of semigroups, 505 Category of magmas, 500 506 Category of coalgebras over Rational Field, 501 507 Category of modules over Rational Field, 502 508 Category of bimodules over Rational Field on the left and Rational Field on the right, … … This gives the following order:: 505 511 Category of commutative additive groups, 506 512 Category of commutative additive monoids, 507 513 Category of commutative additive semigroups, 514 Category of additive magmas, 508 515 Category of sets, 509 516 Category of sets with partial maps, 510 517 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 17 17 from sage.misc.cachefunc import cached_method 18 18 from sage.misc.lazy_attribute import lazy_attribute 19 19 from sage.misc.misc_c import prod 20 from sage.categories. sets_cat import Sets20 from sage.categories.magmas import Magmas 21 21 from sage.structure.unique_representation import UniqueRepresentation 22 22 from sage.structure.parent import Parent 23 23 from sage.structure.element import Element, generic_power … … class Semigroups(Category): 33 33 sage: Semigroups() 34 34 Category of semigroups 35 35 sage: Semigroups().super_categories() 36 [Category of sets]36 [Category of magmas] 37 37 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] 39 39 40 40 TESTS:: 41 41 … … class Semigroups(Category): 52 52 EXAMPLES:: 53 53 54 54 sage: Semigroups().super_categories() 55 [Category of sets]55 [Category of magmas] 56 56 """ 57 return [ Sets()]57 return [Magmas()] 58 58 59 59 def example(self, choice="leftzero", **kwds): 60 60 r""" … … class Semigroups(Category): 117 117 for z in tester.some_elements(): 118 118 tester.assert_((x * y) * z == x * (y * z)) 119 119 120 def product(self, x, y):121 """122 The binary multiplication of the semigroup123 124 INPUT:125 126 - ``x``, ``y``: elements of this semigroup127 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 implement140 :meth:`.product` in the parent class or ``_mul_`` in the141 element class. By default, the addition method on elements142 ``x._mul_(y)`` calls ``S.product(x,y)``, and reciprocally.143 144 145 As a bonus, ``S.product`` models the binary function from146 ``S`` to ``S``::147 148 sage: bin = S.product149 sage: bin(x,y)150 'ab'151 152 Currently, ``S.product`` is just a bound method:153 154 sage: S.rename("S")155 sage: bin156 <bound method FreeSemigroup_with_category.product of S>157 158 When Sage will support multivariate morphisms, it will be159 possible, and in fact recommended, to enrich ``S.product``160 with extra mathematical structure. This will typically be161 implemented using lazy attributes.162 163 sage: bin # todo: not implemented164 Generic binary morphism:165 From: (S x S)166 To: S167 """168 return x._mul_(y)169 170 product_from_element_class_mul = product171 172 120 def prod(self, args): 173 121 r""" 174 122 Returns the product of the list of elements `args` inside `self`. … … class Semigroups(Category): 186 134 assert len(args) > 0, "Cannot compute an empty product in a semigroup" 187 135 return prod(args[1:], args[0]) 188 136 189 def __init_extra__(self):190 """191 sage: S = Semigroups().example("free")192 sage: S('a') * S('b') # indirect doctest193 'ab'194 sage: S('a').__class__._mul_ == S('a').__class__._mul_parent195 True196 """197 # This should instead register the multiplication to the coercion model198 # But this is not yet implemented in the coercion model199 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_parent201 202 137 def cayley_graph(self, side="right", simple=False, elements = None, generators = None, connecting_set = None): 203 138 r""" 204 139 … … class Semigroups(Category): 356 291 357 292 class ElementMethods: 358 293 359 def __mul__(self, right):360 r"""361 Product of two elements362 363 INPUT::364 365 - ``self``, ``right`` -- two elements366 367 This calls the `_mul_` method of ``self``, if it is368 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 the373 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 * y380 '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_model385 import operator386 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 elements394 395 INPUT::396 397 - ``self``, ``right`` -- two elements with the same parent398 399 OUTPUT::400 401 - an element of the same parent402 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 using414 the ``product`` method of the parent.415 416 This is the default implementation of _mul_ if417 ``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 437 294 def _pow_(self, n): 438 295 """ 439 296 Returns self to the $n^{th}$ power. … … class Semigroups(Category): 464 321 465 322 __pow__ = _pow_ 466 323 467 def is_idempotent(self):468 r"""469 Test whether ``self`` is idempotent.470 471 EXAMPLES::472 473 sage: S = Semigroups().example("free"); S474 An example of a semigroup: the free semigroup generated by ('a', 'b', 'c', 'd')475 sage: a = S('a')476 sage: a^2477 'aa'478 sage: a.is_idempotent()479 False480 481 ::482 483 sage: L = Semigroups().example("leftzero"); L484 An example of a semigroup: the left zero semigroup485 sage: x = L('x')486 sage: x^2487 'x'488 sage: x.is_idempotent()489 True490 491 """492 return self * self == self493 494 495 324 ####################################### 496 325 class SubQuotients(Category): # (SubQuotientCategory): 497 326 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 177 177 Category of commutative additive groups, 178 178 Category of commutative additive monoids, 179 179 Category of commutative additive semigroups, 180 Category of additive magmas, 180 181 Category of monoids, 181 182 Category of semigroups, 182 Category of sets, 183 Category of magmas, 184 Category of sets, 183 185 Category of sets with partial maps, 184 186 Category of objects] 185 187 """ -
sage/structure/element.pyx
diff --git a/sage/structure/element.pyx b/sage/structure/element.pyx
a b cdef class Element(sage_object.SageObjec 280 280 sage: 1.is_idempotent(), 2.is_idempotent() 281 281 (True, False) 282 282 283 This method is actually provided by the `` Semigroups()`` super283 This method is actually provided by the ``Magmas()`` super 284 284 category of ``CommutativeRings()``:: 285 285 286 286 sage: 1.is_idempotent 287 287 <bound method EuclideanDomains.element_class.is_idempotent of 1> 288 288 sage: 1.is_idempotent.__module__ 289 'sage.categories. semigroups'289 'sage.categories.magmas' 290 290 291 291 TESTS:: 292 292