Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/algebras/group_algebra_new.py
4107 views
1
r"""
2
Group algebras
3
4
This module implements group algebras for arbitrary groups over
5
arbitrary commutative rings.
6
7
EXAMPLES::
8
9
sage: D4 = DihedralGroup(4)
10
sage: kD4 = GroupAlgebra(D4, GF(7))
11
sage: a = kD4.an_element(); a
12
() + 2*(2,4) + 3*(1,2)(3,4) + (1,2,3,4)
13
sage: a * a
14
(1,2)(3,4) + (1,2,3,4) + 3*(1,3) + (1,3)(2,4) + 6*(1,4,3,2) + 2*(1,4)(2,3)
15
16
Given the group and the base ring, the corresponding group algebra is unique::
17
18
sage: A = GroupAlgebra(GL(3, QQ), ZZ)
19
sage: B = GroupAlgebra(GL(3, QQ), ZZ)
20
sage: A is B
21
True
22
sage: C = GroupAlgebra(GL(3, QQ), QQ)
23
sage: A == C
24
False
25
26
As long as there is no natural map from the group to the base ring,
27
you can easily convert elements of the group to the group algebra::
28
29
sage: A = GroupAlgebra(DihedralGroup(2), ZZ)
30
sage: g = DihedralGroup(2).gen(0); g
31
(3,4)
32
sage: A(g)
33
(3,4)
34
sage: A(2) * g
35
2*(3,4)
36
37
Since there is a natural inclusion from the dihedral group `D_2` of
38
order 4 into the symmetric group `S_4` of order 4!, and since there is
39
a natural map from the integers to the rationals, there is a natural
40
map from `\ZZ[D_2]` to `\QQ[S_4]`::
41
42
sage: A = GroupAlgebra(DihedralGroup(2), ZZ)
43
sage: B = GroupAlgebra(SymmetricGroup(4), QQ)
44
sage: a = A.an_element(); a
45
() + 3*(3,4) + 3*(1,2)
46
sage: b = B.an_element(); b
47
() + 2*(3,4) + 3*(2,3) + (1,2,3,4)
48
sage: B(a)
49
() + 3*(3,4) + 3*(1,2)
50
sage: a * b # a is automatically converted to an element of B
51
7*() + 5*(3,4) + 3*(2,3) + 9*(2,3,4) + 3*(1,2) + 6*(1,2)(3,4) + 3*(1,2,3) + (1,2,3,4) + 9*(1,3,2) + 3*(1,3,4)
52
sage: parent(a * b)
53
Group algebra of group "Symmetric group of order 4! as a permutation group" over base ring Rational Field
54
55
sage: G = GL(3, GF(7))
56
sage: ZG = GroupAlgebra(G)
57
sage: c, d = G.random_element(), G.random_element()
58
sage: zc, zd = ZG(c), ZG(d)
59
sage: zc * d == zc * zd # d is automatically converted to an element of ZG
60
True
61
62
There is no obvious map in the other direction, though::
63
64
sage: A(b)
65
Traceback (most recent call last):
66
...
67
TypeError: Don't know how to create an element of Group algebra of group "Dihedral group of order 4 as a permutation group" over base ring Integer Ring from () + 2*(3,4) + 3*(2,3) + (1,2,3,4)
68
69
Group algebras have the structure of Hopf algebras::
70
71
sage: a = kD4.an_element(); a
72
() + 2*(2,4) + 3*(1,2)(3,4) + (1,2,3,4)
73
sage: a.antipode()
74
() + 2*(2,4) + 3*(1,2)(3,4) + (1,4,3,2)
75
sage: a.coproduct()
76
() # () + 2*(2,4) # (2,4) + 3*(1,2)(3,4) # (1,2)(3,4) + (1,2,3,4) # (1,2,3,4)
77
78
.. note::
79
80
As alluded to above, it is problematic to make group algebras fit
81
nicely with Sage's coercion model. The problem is that (for
82
example) if G is the additive group `(\ZZ,+)`, and `R = \ZZ[G]` is
83
its group ring, then the integer 2 can be coerced into R in two
84
ways -- via G, or via the base ring -- and *the answers are
85
different*. In practice we get around this by preventing elements
86
of a group `H` from coercing automatically into a group ring
87
`k[G]` if `H` coerces into both `k` and `G`. This is unfortunate,
88
but it seems like the most sensible solution in this ambiguous
89
situation.
90
91
AUTHOR:
92
93
- David Loeffler (2008-08-24): initial version
94
- Martin Raum (2009-08): update to use new coercion model -- see trac
95
ticket #6670
96
- John Palmieri (2011-07): more updates to coercion, categories, etc.,
97
group algebras constructed using CombinatorialFreeModule -- see trac
98
ticket #6670
99
"""
100
101
#*****************************************************************************
102
# Copyright (C) 2008 William Stein <[email protected]>
103
# 2008 David Loeffler <[email protected]>
104
# 2009 Martin Raum <[email protected]>
105
# 2011 John Palmieri <[email protected]>
106
# Distributed under the terms of the GNU General Public License (GPL)
107
# http://www.gnu.org/licenses/
108
#*****************************************************************************
109
110
from sage.algebras.algebra import Algebra
111
from sage.rings.all import IntegerRing
112
from sage.misc.cachefunc import cached_method
113
from sage.categories.pushout import ConstructionFunctor
114
from sage.combinat.free_module import CombinatorialFreeModule
115
from sage.categories.all import Rings, HopfAlgebrasWithBasis, Hom
116
from sage.categories.morphism import SetMorphism
117
118
119
class GroupAlgebraFunctor (ConstructionFunctor) :
120
r"""
121
For a fixed group, a functor sending a commutative ring to the
122
corresponding group algebra.
123
124
INPUT :
125
126
- ``group`` -- the group associated to each group algebra under
127
consideration.
128
129
EXAMPLES::
130
131
sage: from sage.algebras.group_algebra_new import GroupAlgebraFunctor
132
sage: F = GroupAlgebraFunctor(KleinFourGroup())
133
sage: loads(dumps(F)) == F
134
True
135
sage: GroupAlgebra(SU(2, GF(4, 'a')), IntegerModRing(12)).category()
136
Category of hopf algebras with basis over Ring of integers modulo 12
137
"""
138
def __init__(self, group) :
139
r"""
140
See :class:`GroupAlgebraFunctor` for full documentation.
141
142
EXAMPLES::
143
144
sage: from sage.algebras.group_algebra_new import GroupAlgebraFunctor
145
sage: GroupAlgebra(SU(2, GF(4, 'a')), IntegerModRing(12)).category()
146
Category of hopf algebras with basis over Ring of integers modulo 12
147
"""
148
self.__group = group
149
150
ConstructionFunctor.__init__(self, Rings(), Rings())
151
152
def group(self) :
153
r"""
154
Return the group which is associated to this functor.
155
156
EXAMPLES::
157
158
sage: from sage.algebras.group_algebra_new import GroupAlgebraFunctor
159
sage: GroupAlgebraFunctor(CyclicPermutationGroup(17)).group() == CyclicPermutationGroup(17)
160
True
161
"""
162
return self.__group
163
164
def _apply_functor(self, base_ring) :
165
r"""
166
Create the group algebra with given base ring over ``self.group()``.
167
168
INPUT :
169
170
- ``base_ring`` - the base ring of the group algebra.
171
172
OUTPUT:
173
174
A group algebra.
175
176
EXAMPLES::
177
178
sage: from sage.algebras.group_algebra_new import GroupAlgebraFunctor
179
sage: F = GroupAlgebraFunctor(CyclicPermutationGroup(17))
180
sage: F(QQ)
181
Group algebra of group "Cyclic group of order 17 as a permutation group" over base ring Rational Field
182
"""
183
return GroupAlgebra(self.__group, base_ring)
184
185
def _apply_functor_to_morphism(self, f) :
186
r"""
187
Lift a homomorphism of rings to the corresponding homomorphism of the group algebras of ``self.group()``.
188
189
INPUT:
190
191
- ``f`` - a morphism of rings.
192
193
OUTPUT:
194
195
A morphism of group algebras.
196
197
EXAMPLES::
198
199
sage: G = SymmetricGroup(3)
200
sage: A = GroupAlgebra(G, ZZ)
201
sage: h = sage.categories.morphism.SetMorphism(Hom(ZZ, GF(5), Rings()), lambda x: GF(5)(x))
202
sage: hh = A.construction()[0](h)
203
sage: hh(A.0 + 5 * A.1)
204
(1,2,3)
205
"""
206
codomain = self(f.codomain())
207
return SetMorphism(Hom(self(f.domain()), codomain, Rings()), lambda x: sum(codomain(g) * f(c) for (g, c) in dict(x).iteritems()))
208
209
class GroupAlgebra(CombinatorialFreeModule, Algebra):
210
r"""
211
Create the given group algebra.
212
213
INPUT:
214
215
- ``group``, a group
216
- ``base_ring`` (optional, default `\ZZ`), a commutative ring
217
218
OUTPUT:
219
220
-- a ``GroupAlgebra`` instance.
221
222
EXAMPLES::
223
224
sage: GroupAlgebra(GL(3, GF(7)))
225
Group algebra of group "General Linear Group of degree 3 over Finite Field of size 7" over base ring Integer Ring
226
sage: GroupAlgebra(GL(3, GF(7)), QQ)
227
Group algebra of group "General Linear Group of degree 3 over Finite Field of size 7" over base ring Rational Field
228
sage: GroupAlgebra(1)
229
Traceback (most recent call last):
230
...
231
TypeError: "1" is not a group
232
233
sage: GroupAlgebra(SU(2, GF(4, 'a')), IntegerModRing(12)).category()
234
Category of hopf algebras with basis over Ring of integers modulo 12
235
sage: GroupAlgebra(KleinFourGroup()) is GroupAlgebra(KleinFourGroup())
236
True
237
238
TESTS::
239
240
sage: A = GroupAlgebra(GL(3, GF(7)))
241
sage: A.has_coerce_map_from(GL(3, GF(7)))
242
True
243
sage: G = SymmetricGroup(5)
244
sage: x,y = G.gens()
245
sage: A = GroupAlgebra(G)
246
sage: A( A(x) )
247
(1,2,3,4,5)
248
"""
249
def __init__(self, group, base_ring=IntegerRing()):
250
r"""
251
See :class:`GroupAlgebra` for full documentation.
252
253
EXAMPLES::
254
255
sage: GroupAlgebra(GL(3, GF(7)))
256
Group algebra of group "General Linear Group of degree 3 over Finite Field of size 7" over base ring Integer Ring
257
"""
258
from sage.groups.group import Group
259
if not base_ring.is_commutative():
260
raise NotImplementedError("Base ring must be commutative")
261
262
if not isinstance(group, Group):
263
raise TypeError('"%s" is not a group' % group)
264
265
self._group = group
266
CombinatorialFreeModule.__init__(self, base_ring, group,
267
prefix='',
268
bracket=False,
269
category=HopfAlgebrasWithBasis(base_ring))
270
271
if not base_ring.has_coerce_map_from(group) :
272
## some matrix groups assume that coercion is only valid to
273
## other matrix groups. This is a workaround
274
## call _element_constructor_ to coerce group elements
275
#try :
276
self._populate_coercion_lists_(coerce_list=[base_ring, group])
277
#except TypeError :
278
# self._populate_coercion_lists_( coerce_list = [base_ring] )
279
else :
280
self._populate_coercion_lists_(coerce_list=[base_ring])
281
282
# Methods taken from sage.categories.examples.hopf_algebras_with_basis:
283
284
@cached_method
285
def one_basis(self):
286
"""
287
Returns the one of the group, which indexes the one of this
288
algebra, as per :meth:`AlgebrasWithBasis.ParentMethods.one_basis`.
289
290
EXAMPLES::
291
292
sage: A = GroupAlgebra(DihedralGroup(6), QQ)
293
sage: A.one_basis()
294
()
295
sage: A.one()
296
()
297
"""
298
return self._group.one()
299
300
def product_on_basis(self, g1, g2):
301
r"""
302
Product, on basis elements, as per
303
:meth:`AlgebrasWithBasis.ParentMethods.product_on_basis`.
304
305
The product of two basis elements is induced by the product of
306
the corresponding elements of the group.
307
308
EXAMPLES::
309
310
sage: A = GroupAlgebra(DihedralGroup(3), QQ)
311
sage: (a, b) = A._group.gens()
312
sage: a*b
313
(1,2)
314
sage: A.product_on_basis(a, b)
315
(1,2)
316
"""
317
return self.basis()[g1 * g2]
318
319
@cached_method
320
def algebra_generators(self):
321
r"""
322
The generators of this algebra, as per
323
:meth:`Algebras.ParentMethods.algebra_generators`.
324
325
They correspond to the generators of the group.
326
327
EXAMPLES::
328
329
sage: A = GroupAlgebra(DihedralGroup(3), QQ); A
330
Group algebra of group "Dihedral group of order 6 as a permutation group" over base ring Rational Field
331
sage: A.algebra_generators()
332
Finite family {(1,2,3): (1,2,3), (1,3): (1,3)}
333
"""
334
from sage.sets.family import Family
335
return Family(self._group.gens(), self.monomial)
336
337
gens = algebra_generators
338
339
def coproduct_on_basis(self, g):
340
r"""
341
Coproduct, on basis elements, as per :meth:`HopfAlgebrasWithBasis.ParentMethods.coproduct_on_basis`.
342
343
The basis elements are group-like: `\Delta(g) = g \otimes g`.
344
345
EXAMPLES::
346
347
sage: A = GroupAlgebra(DihedralGroup(3), QQ)
348
sage: (a, b) = A._group.gens()
349
sage: A.coproduct_on_basis(a)
350
(1,2,3) # (1,2,3)
351
"""
352
from sage.categories.all import tensor
353
g = self.monomial(g)
354
return tensor([g, g])
355
356
def counit_on_basis(self, g):
357
r"""
358
Counit, on basis elements, as per :meth:`HopfAlgebrasWithBasis.ParentMethods.counit_on_basis`.
359
360
The counit on the basis elements is 1.
361
362
EXAMPLES::
363
364
sage: A = GroupAlgebra(DihedralGroup(6), QQ)
365
sage: (a, b) = A._group.gens()
366
sage: A.counit_on_basis(a)
367
1
368
"""
369
return self.base_ring().one()
370
371
def antipode_on_basis(self, g):
372
r"""
373
Antipode, on basis elements, as per :meth:`HopfAlgebrasWithBasis.ParentMethods.antipode_on_basis`.
374
375
It is given, on basis elements, by `\chi(g) = g^{-1}`
376
377
EXAMPLES::
378
379
sage: A = GroupAlgebra(DihedralGroup(3), QQ)
380
sage: (a, b) = A._group.gens()
381
sage: A.antipode_on_basis(a)
382
(1,3,2)
383
"""
384
return self.monomial(~g)
385
386
# other methods:
387
388
def ngens(self) :
389
r"""
390
Return the number of generators.
391
392
EXAMPLES::
393
394
sage: GroupAlgebra(SL2Z).ngens()
395
2
396
sage: GroupAlgebra(DihedralGroup(4), RR).ngens()
397
2
398
"""
399
return self.algebra_generators().cardinality()
400
401
def gen(self, i = 0) :
402
r"""
403
EXAMPLES::
404
405
sage: A = GroupAlgebra(GL(3, GF(7)))
406
sage: A.gen(0)
407
[3 0 0]
408
[0 1 0]
409
[0 0 1]
410
"""
411
return self.monomial(self._group.gen(i))
412
413
def group(self):
414
r"""
415
Return the group of this group algebra.
416
417
EXAMPLES::
418
419
sage: GroupAlgebra(GL(3, GF(11))).group()
420
General Linear Group of degree 3 over Finite Field of size 11
421
sage: GroupAlgebra(SymmetricGroup(10)).group()
422
Symmetric group of order 10! as a permutation group
423
"""
424
return self._group
425
426
def is_commutative(self):
427
r"""
428
Return True if self is a commutative ring. True if and only if
429
``self.group()`` is abelian.
430
431
EXAMPLES::
432
433
sage: GroupAlgebra(SymmetricGroup(2)).is_commutative()
434
True
435
sage: GroupAlgebra(SymmetricGroup(3)).is_commutative()
436
False
437
"""
438
return self.group().is_abelian()
439
440
def is_field(self, proof = True):
441
r"""
442
Return True if self is a field. This is always false unless
443
``self.group()`` is trivial and ``self.base_ring()`` is a field.
444
445
EXAMPLES::
446
447
sage: GroupAlgebra(SymmetricGroup(2)).is_field()
448
False
449
sage: GroupAlgebra(SymmetricGroup(1)).is_field()
450
False
451
sage: GroupAlgebra(SymmetricGroup(1), QQ).is_field()
452
True
453
"""
454
if not self.base_ring().is_field(proof):
455
return False
456
return (self.group().order() == 1)
457
458
def is_finite(self):
459
r"""
460
Return True if self is finite, which is true if and only if
461
``self.group()`` and ``self.base_ring()`` are both finite.
462
463
EXAMPLES::
464
465
sage: GroupAlgebra(SymmetricGroup(2), IntegerModRing(10)).is_finite()
466
True
467
sage: GroupAlgebra(SymmetricGroup(2)).is_finite()
468
False
469
sage: GroupAlgebra(AbelianGroup(1), IntegerModRing(10)).is_finite()
470
False
471
"""
472
return (self.base_ring().is_finite() and self.group().is_finite())
473
474
def is_exact(self):
475
r"""
476
Return True if elements of self have exact representations, which is
477
true of self if and only if it is true of ``self.group()`` and
478
``self.base_ring()``.
479
480
EXAMPLES::
481
482
sage: GroupAlgebra(GL(3, GF(7))).is_exact()
483
True
484
sage: GroupAlgebra(GL(3, GF(7)), RR).is_exact()
485
False
486
sage: GroupAlgebra(GL(3, pAdicRing(7))).is_exact() # not implemented correctly (not my fault)!
487
False
488
"""
489
return self.group().is_exact() and self.base_ring().is_exact()
490
491
def is_integral_domain(self, proof = True):
492
r"""
493
Return True if self is an integral domain.
494
495
This is false unless ``self.base_ring()`` is an integral domain, and
496
even then it is false unless ``self.group()`` has no nontrivial
497
elements of finite order. I don't know if this condition suffices, but
498
it obviously does if the group is abelian and finitely generated.
499
500
EXAMPLES::
501
502
sage: GroupAlgebra(SymmetricGroup(2)).is_integral_domain()
503
False
504
sage: GroupAlgebra(SymmetricGroup(1)).is_integral_domain()
505
True
506
sage: GroupAlgebra(SymmetricGroup(1), IntegerModRing(4)).is_integral_domain()
507
False
508
sage: GroupAlgebra(AbelianGroup(1)).is_integral_domain()
509
True
510
sage: GroupAlgebra(AbelianGroup(2, [0,2])).is_integral_domain()
511
False
512
sage: GroupAlgebra(GL(2, ZZ)).is_integral_domain() # not implemented
513
False
514
"""
515
from sage.sets.set import Set
516
ans = False
517
try:
518
if self.base_ring().is_integral_domain():
519
if self.group().is_finite():
520
if self.group().order() > 1:
521
ans = False
522
else:
523
ans = True
524
else:
525
if self.group().is_abelian():
526
invs = self.group().invariants()
527
if Set(invs) != Set([0]):
528
ans = False
529
else:
530
ans = True
531
else:
532
raise NotImplementedError
533
else:
534
ans = False
535
except AttributeError:
536
if proof:
537
raise NotImplementedError("cannot determine whether self is an integral domain")
538
except NotImplementedError:
539
if proof:
540
raise NotImplementedError("cannot determine whether self is an integral domain")
541
542
return ans
543
544
# I haven't written is_noetherian(), because I don't know when group
545
# algebras are noetherian, and I haven't written is_prime_field(), because
546
# I don't know if that means "is canonically isomorphic to a prime field"
547
# or "is identical to a prime field".
548
549
def __cmp__(self, other) :
550
r"""
551
Compare two algebras self and other. They are considered equal if and only
552
if their base rings and their groups coincide.
553
554
EXAMPLES::
555
556
sage: GroupAlgebra(AbelianGroup(1)) == GroupAlgebra(AbelianGroup(1))
557
True
558
sage: GroupAlgebra(AbelianGroup(1), QQ) == GroupAlgebra(AbelianGroup(1), ZZ)
559
False
560
sage: GroupAlgebra(AbelianGroup(2)) == GroupAlgebra(AbelianGroup(1))
561
False
562
sage: A = GroupAlgebra(KleinFourGroup(), ZZ)
563
sage: B = GroupAlgebra(KleinFourGroup(), QQ)
564
sage: A == B
565
False
566
sage: A == A
567
True
568
"""
569
c = cmp(type(self), type(other))
570
571
if c == 0 :
572
c = cmp(self._group, other._group)
573
if c == 0 :
574
c = cmp(self.base_ring(), other.base_ring())
575
576
return c
577
578
def random_element(self, n=2):
579
r"""
580
Return a 'random' element of self.
581
582
INPUT:
583
584
- n -- integer (optional, default 2), number of summands
585
586
Algorithm: return a sum of n terms, each of which is formed by
587
multiplying a random element of the base ring by a random
588
element of the group.
589
590
EXAMPLE::
591
592
sage: GroupAlgebra(DihedralGroup(6), QQ).random_element()
593
-97/190*(1,6)(2,5)(3,4)
594
sage: GroupAlgebra(SU(2, 13), QQ).random_element(1)
595
1/2*[ 8 11*a + 1]
596
[ a + 6 3]
597
"""
598
a = self(0)
599
for i in range(n):
600
a += self.term(self.group().random_element(),
601
self.base_ring().random_element())
602
return a
603
604
def construction(self) :
605
r"""
606
EXAMPLES::
607
608
sage: A = GroupAlgebra(KleinFourGroup(), QQ)
609
sage: A.construction()
610
(GroupAlgebraFunctor, Rational Field)
611
"""
612
return GroupAlgebraFunctor(self._group), self.base_ring()
613
614
def _repr_(self):
615
r"""
616
String representation of self. See GroupAlgebra.__init__ for a doctest.
617
618
EXAMPLES::
619
620
sage: A = GroupAlgebra(KleinFourGroup(), ZZ)
621
sage: A # indirect doctest
622
Group algebra of group "The Klein 4 group of order 4, as a permutation group" over base ring Integer Ring
623
"""
624
return "Group algebra of group \"%s\" over base ring %s" % \
625
(self.group(), self.base_ring())
626
627
def _latex_(self):
628
r"""Latex string of self.
629
630
EXAMPLES::
631
632
sage: A = GroupAlgebra(KleinFourGroup(), ZZ)
633
sage: latex(A) # indirect doctest
634
\Bold{Z}[\langle (3,4), (1,2) \rangle]
635
"""
636
from sage.misc.all import latex
637
return "%s[%s]" % (latex(self.base_ring()), latex(self.group()))
638
639
# coercion methods:
640
641
def _coerce_map_from_(self, S):
642
r"""
643
True if there is a coercion from ``S`` to ``self``, False otherwise.
644
The actual coercion is done by the :meth:`_element_constructor_`
645
method.
646
647
INPUT:
648
649
- ``S`` - a Sage object.
650
651
The objects that coerce into a group algebra `k[G]` are:
652
653
- any group algebra `R[H]` as long as `R` coerces into `k` and
654
`H` coerces into `G`.
655
656
- any ring `R` which coerces into `k`
657
658
- any group `H` which coerces into either `k` or `G`.
659
660
Note that if `H` is a group which coerces into both `k` and
661
`G`, then Sage will always use the map to `k`. For example,
662
if `\ZZ` is the ring (or group) of integers, then `\ZZ` will
663
coerce to any `k[G]`, by sending `\ZZ` to `k`.
664
665
EXAMPLES::
666
667
sage: A = GroupAlgebra(SymmetricGroup(4), QQ)
668
sage: B = GroupAlgebra(SymmetricGroup(3), ZZ)
669
sage: A._coerce_map_from_(B)
670
True
671
sage: B._coerce_map_from_(A)
672
False
673
sage: A._coerce_map_from_(ZZ)
674
True
675
sage: A._coerce_map_from_(CC)
676
False
677
sage: A._coerce_map_from_(SymmetricGroup(5))
678
False
679
sage: A._coerce_map_from_(SymmetricGroup(2))
680
True
681
"""
682
from sage.rings.all import is_Ring
683
from sage.groups.group import Group
684
k = self.base_ring()
685
G = self.group()
686
if isinstance(S, GroupAlgebra):
687
return (k.has_coerce_map_from(S.base_ring())
688
and G.has_coerce_map_from(S.group()))
689
if is_Ring(S):
690
return k.has_coerce_map_from(S)
691
if isinstance(S,Group):
692
return k.has_coerce_map_from(S) or G.has_coerce_map_from(S)
693
694
def _element_constructor_(self, x):
695
r"""
696
Try to turn ``x`` into an element of ``self``.
697
698
INPUT:
699
700
- ``x`` - an element of some group algebra or of a
701
ring or of a group
702
703
OUTPUT: ``x`` as a member of ``self``.
704
705
sage: G = KleinFourGroup()
706
sage: f = G.gen(0)
707
sage: ZG = GroupAlgebra(G)
708
sage: ZG(f) # indirect doctest
709
(3,4)
710
sage: ZG(1) == ZG(G(1))
711
True
712
sage: G = AbelianGroup(1)
713
sage: ZG = GroupAlgebra(G)
714
sage: f = ZG.group().gen()
715
sage: ZG(FormalSum([(1,f), (2, f**2)]))
716
f + 2*f^2
717
sage: G = GL(2,7)
718
sage: OG = GroupAlgebra(G, ZZ[sqrt(5)])
719
sage: OG(2)
720
2*[1 0]
721
[0 1]
722
sage: OG(G(2)) # conversion is not the obvious one
723
[2 0]
724
[0 2]
725
sage: OG(FormalSum([ (1, G(2)), (2, RR(0.77)) ]) )
726
Traceback (most recent call last):
727
...
728
TypeError: Cannot coerce 0.770000000000000 to a 2-by-2 matrix over Finite Field of size 7
729
730
sage: OG(OG.base_ring().gens()[1])
731
sqrt5*[1 0]
732
[0 1]
733
"""
734
from sage.rings.all import is_Ring
735
from sage.groups.group import Group
736
from sage.structure.formal_sum import FormalSum
737
k = self.base_ring()
738
G = self.group()
739
S = x.parent()
740
if isinstance(S, GroupAlgebra):
741
if self.has_coerce_map_from(S):
742
# coerce monomials, coerce coefficients, reassemble
743
d = x.monomial_coefficients()
744
new_d = {}
745
for g in d:
746
g1 = G(g)
747
if g1 in new_d:
748
new_d[g1] += k(d[g]) + new_d[g1]
749
else:
750
new_d[g1] = k(d[g])
751
return self._from_dict(new_d)
752
elif is_Ring(S):
753
# coerce to multiple of identity element
754
return k(x) * self(1)
755
elif isinstance(S, Group):
756
# Check whether group coerces to base_ring first.
757
if k.has_coerce_map_from(S):
758
return k(x) * self(1)
759
if G.has_coerce_map_from(S):
760
return self.monomial(self.group()(x))
761
elif isinstance(x, FormalSum) and k.has_coerce_map_from(S.base_ring()):
762
y = [(G(g), k(coeff)) for coeff,g in x]
763
return self.sum_of_terms(y)
764
raise TypeError("Don't know how to create an element of %s from %s" % \
765
(self, x))
766
767