Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/groups/old.pyx
8814 views
1
"""
2
Base class for groups
3
"""
4
5
#*****************************************************************************
6
# Copyright (C) 2005 William Stein <[email protected]>
7
#
8
# Distributed under the terms of the GNU General Public License (GPL)
9
#
10
# This code is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
# General Public License for more details.
14
#
15
# The full text of the GPL is available at:
16
#
17
# http://www.gnu.org/licenses/
18
#*****************************************************************************
19
20
doc="""
21
Base class for all groups
22
"""
23
24
import random
25
26
from sage.rings.infinity import infinity
27
import sage.rings.integer_ring
28
29
cdef class Group(sage.structure.parent_gens.ParentWithGens):
30
"""
31
Generic group class
32
"""
33
def __init__(self, category = None):
34
"""
35
36
TESTS::
37
38
sage: from sage.groups.old import Group
39
sage: G = Group()
40
sage: G.category()
41
Category of groups
42
sage: G = Group(category = Groups()) # todo: do the same test with some subcategory of Groups when there will exist one
43
sage: G.category()
44
Category of groups
45
sage: G = Group(category = CommutativeAdditiveGroups())
46
Traceback (most recent call last):
47
...
48
AssertionError: Category of commutative additive groups is not a subcategory of Category of groups
49
50
Check for #8119::
51
52
sage: G = SymmetricGroup(2)
53
sage: h = hash(G)
54
sage: G.rename('S2')
55
sage: h == hash(G)
56
True
57
"""
58
from sage.categories.basic import Groups
59
if category is None:
60
category = Groups()
61
else:
62
assert category.is_subcategory(Groups()), "%s is not a subcategory of %s"%(category, Groups())
63
64
sage.structure.parent_gens.ParentWithGens.__init__(self,
65
sage.rings.integer_ring.ZZ, category = category)
66
67
#def __call__(self, x): # this gets in the way of the coercion mechanism
68
# """
69
# Coerce x into this group.
70
# """
71
# raise NotImplementedError
72
73
def __contains__(self, x):
74
r"""
75
True if coercion of `x` into self is defined.
76
77
EXAMPLES::
78
79
sage: from sage.groups.old import Group
80
sage: G = Group()
81
sage: 4 in G #indirect doctest
82
Traceback (most recent call last):
83
...
84
NotImplementedError
85
"""
86
try:
87
self(x)
88
except TypeError:
89
return False
90
return True
91
92
# def category(self):
93
# """
94
# The category of all groups
95
# """
96
# import sage.categories.all
97
# return sage.categories.all.Groups()
98
99
def is_abelian(self):
100
"""
101
Return True if this group is abelian.
102
103
EXAMPLES::
104
105
sage: from sage.groups.old import Group
106
sage: G = Group()
107
sage: G.is_abelian()
108
Traceback (most recent call last):
109
...
110
NotImplementedError
111
"""
112
raise NotImplementedError
113
114
def is_commutative(self):
115
r"""
116
Return True if this group is commutative. This is an alias for
117
is_abelian, largely to make groups work well with the Factorization
118
class.
119
120
(Note for developers: Derived classes should override is_abelian, not
121
is_commutative.)
122
123
EXAMPLE::
124
125
sage: SL(2, 7).is_commutative()
126
False
127
"""
128
return self.is_abelian()
129
130
def order(self):
131
"""
132
Returns the number of elements of this group, which is either a
133
positive integer or infinity.
134
135
EXAMPLES::
136
137
sage: from sage.groups.old import Group
138
sage: G = Group()
139
sage: G.order()
140
Traceback (most recent call last):
141
...
142
NotImplementedError
143
"""
144
raise NotImplementedError
145
146
def is_finite(self):
147
"""
148
Returns True if this group is finite.
149
150
EXAMPLES::
151
152
sage: from sage.groups.old import Group
153
sage: G = Group()
154
sage: G.is_finite()
155
Traceback (most recent call last):
156
...
157
NotImplementedError
158
"""
159
return self.order() != infinity
160
161
def is_multiplicative(self):
162
"""
163
Returns True if the group operation is given by \* (rather than
164
+).
165
166
Override for additive groups.
167
168
EXAMPLES::
169
170
sage: from sage.groups.old import Group
171
sage: G = Group()
172
sage: G.is_multiplicative()
173
True
174
"""
175
return True
176
177
def random_element(self, bound=None):
178
"""
179
Return a random element of this group.
180
181
EXAMPLES::
182
183
sage: from sage.groups.old import Group
184
sage: G = Group()
185
sage: G.random_element()
186
Traceback (most recent call last):
187
...
188
NotImplementedError
189
"""
190
raise NotImplementedError
191
192
def quotient(self, H):
193
"""
194
Return the quotient of this group by the normal subgroup
195
`H`.
196
197
EXAMPLES::
198
199
sage: from sage.groups.old import Group
200
sage: G = Group()
201
sage: G.quotient(G)
202
Traceback (most recent call last):
203
...
204
NotImplementedError
205
"""
206
raise NotImplementedError
207
208
cdef class AbelianGroup(Group):
209
"""
210
Generic abelian group.
211
"""
212
def is_abelian(self):
213
"""
214
Return True.
215
216
EXAMPLES::
217
218
sage: from sage.groups.old import AbelianGroup
219
sage: G = AbelianGroup()
220
sage: G.is_abelian()
221
True
222
"""
223
return True
224
225
cdef class FiniteGroup(Group):
226
"""
227
Generic finite group.
228
"""
229
def is_finite(self):
230
"""
231
Return True.
232
233
EXAMPLES::
234
235
sage: from sage.groups.old import FiniteGroup
236
sage: G = FiniteGroup()
237
sage: G.is_finite()
238
True
239
"""
240
return True
241
242
def cayley_graph(self, connecting_set=None):
243
"""
244
Returns the cayley graph for this finite group, as a Sage DiGraph
245
object. To plot the graph with with different colors
246
247
INPUT::
248
249
`connecting_set` - (optional) list of elements to use for edges,
250
default is the stored generators
251
252
EXAMPLES::
253
254
sage: D4 = DihedralGroup(4); D4
255
Dihedral group of order 8 as a permutation group
256
sage: G = D4.cayley_graph()
257
sage: show(G, color_by_label=True, edge_labels=True)
258
sage: A5 = AlternatingGroup(5); A5
259
Alternating group of order 5!/2 as a permutation group
260
sage: G = A5.cayley_graph()
261
sage: G.show3d(color_by_label=True, edge_size=0.01, edge_size2=0.02, vertex_size=0.03)
262
sage: G.show3d(vertex_size=0.03, edge_size=0.01, edge_size2=0.02, vertex_colors={(1,1,1):G.vertices()}, bgcolor=(0,0,0), color_by_label=True, xres=700, yres=700, iterations=200) # long time (less than a minute)
263
sage: G.num_edges()
264
120
265
sage: G = A5.cayley_graph(connecting_set=[A5.gens()[0]])
266
sage: G.num_edges()
267
60
268
sage: g=PermutationGroup([(i+1,j+1) for i in range(5) for j in range(5) if j!=i])
269
sage: g.cayley_graph(connecting_set=[(1,2),(2,3)])
270
Digraph on 120 vertices
271
272
::
273
274
sage: s1 = SymmetricGroup(1); s = s1.cayley_graph(); s.vertices()
275
[()]
276
277
AUTHORS:
278
279
- Bobby Moretti (2007-08-10)
280
281
- Robert Miller (2008-05-01): editing
282
"""
283
if connecting_set is None:
284
connecting_set = self.gens()
285
else:
286
try:
287
for g in connecting_set:
288
assert g in self
289
except AssertionError:
290
raise RuntimeError("Each element of the connecting set must be in the group!")
291
connecting_set = [self(g) for g in connecting_set]
292
from sage.graphs.all import DiGraph
293
arrows = {}
294
for x in self:
295
arrows[x] = {}
296
for g in connecting_set:
297
xg = x*g # cache the multiplication
298
if not xg == x:
299
arrows[x][xg] = g
300
301
return DiGraph(arrows, implementation='networkx')
302
303
cdef class AlgebraicGroup(Group):
304
"""
305
Generic algebraic group.
306
"""
307
308