Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/homology/homology_group.py
8818 views
1
"""
2
Homology Groups
3
4
This module defines a :meth:`HomologyGroup` class which is an abelian
5
group that prints itself in a way that is suitable for homology
6
groups.
7
"""
8
9
########################################################################
10
# Copyright (C) 2013 John H. Palmieri <[email protected]>
11
# Volker Braun <[email protected]>
12
#
13
# Distributed under the terms of the GNU General Public License (GPL)
14
# as published by the Free Software Foundation; either version 2 of
15
# the License, or (at your option) any later version.
16
#
17
# http://www.gnu.org/licenses/
18
########################################################################
19
20
21
from sage.modules.free_module import VectorSpace
22
from sage.groups.additive_abelian.additive_abelian_group import AdditiveAbelianGroup_fixed_gens
23
from sage.rings.integer_ring import ZZ
24
25
26
class HomologyGroup_class(AdditiveAbelianGroup_fixed_gens):
27
"""
28
Discrete Abelian group on `n` generators. This class inherits from
29
:class:`~sage.groups.additive_abelian.additive_abelian_group.AdditiveAbelianGroup_fixed_gens`;
30
see :mod:`sage.groups.additive_abelian.additive_abelian_group` for more
31
documentation. The main difference between the classes is in the print
32
representation.
33
34
EXAMPLES::
35
36
sage: from sage.homology.homology_group import HomologyGroup
37
sage: G = AbelianGroup(5, [5,5,7,8,9]); G
38
Multiplicative Abelian group isomorphic to C5 x C5 x C7 x C8 x C9
39
sage: H = HomologyGroup(5, ZZ, [5,5,7,8,9]); H
40
C5 x C5 x C7 x C8 x C9
41
sage: G == loads(dumps(G))
42
True
43
sage: AbelianGroup(4)
44
Multiplicative Abelian group isomorphic to Z x Z x Z x Z
45
sage: HomologyGroup(4, ZZ)
46
Z x Z x Z x Z
47
sage: HomologyGroup(100, ZZ)
48
Z^100
49
"""
50
def __init__(self, n, invfac):
51
"""
52
See :func:`HomologyGroup` for full documentation.
53
54
EXAMPLES::
55
56
sage: from sage.homology.homology_group import HomologyGroup
57
sage: H = HomologyGroup(5, ZZ, [5,5,7,8,9]); H
58
C5 x C5 x C7 x C8 x C9
59
"""
60
n = len(invfac)
61
A = ZZ**n
62
B = A.span([A.gen(i) * invfac[i] for i in xrange(n)])
63
64
AdditiveAbelianGroup_fixed_gens.__init__(self, A, B, A.gens())
65
self._original_invts = invfac
66
67
def _repr_(self):
68
"""
69
Print representation of ``self``.
70
71
EXAMPLES::
72
73
sage: from sage.homology.homology_group import HomologyGroup
74
sage: H = HomologyGroup(7, ZZ, [4,4,4,4,4,7,7])
75
sage: H._repr_()
76
'C4^5 x C7 x C7'
77
sage: HomologyGroup(6, ZZ)
78
Z^6
79
"""
80
eldv = self._original_invts
81
if len(eldv) == 0:
82
return "0"
83
rank = len(filter(lambda x: x == 0, eldv))
84
torsion = sorted(filter(lambda x: x, eldv))
85
if rank > 4:
86
g = ["Z^%s" % rank]
87
else:
88
g = ["Z"] * rank
89
if len(torsion) != 0:
90
printed = []
91
for t in torsion:
92
numfac = torsion.count(t)
93
too_many = (numfac > 4)
94
if too_many:
95
if t not in printed:
96
g.append("C{}^{}".format(t, numfac))
97
printed.append(t)
98
else:
99
g.append("C%s" % t)
100
times = " x "
101
return times.join(g)
102
103
def _latex_(self):
104
"""
105
LaTeX representation of ``self``.
106
107
EXAMPLES::
108
109
sage: from sage.homology.homology_group import HomologyGroup
110
sage: H = HomologyGroup(7, ZZ, [4,4,4,4,4,7,7])
111
sage: H._latex_()
112
'C_{4}^{5} \\times C_{7} \\times C_{7}'
113
sage: latex(HomologyGroup(6, ZZ))
114
\ZZ^{6}
115
"""
116
eldv = self._original_invts
117
if len(eldv) == 0:
118
return "0"
119
rank = len(filter(lambda x: x == 0, eldv))
120
torsion = sorted(filter(lambda x: x, eldv))
121
if rank > 4:
122
g = ["\\ZZ^{{{}}}".format(rank)]
123
else:
124
g = ["\\ZZ"] * rank
125
if len(torsion) != 0:
126
printed = []
127
for t in torsion:
128
numfac = torsion.count(t)
129
too_many = (numfac > 4)
130
if too_many:
131
if t not in printed:
132
g.append("C_{{{}}}^{{{}}}".format(t, numfac))
133
printed.append(t)
134
else:
135
g.append("C_{{{}}}".format(t))
136
times = " \\times "
137
return times.join(g)
138
139
def HomologyGroup(n, base_ring, invfac=None):
140
"""
141
Abelian group on `n` generators which represents a homology group in a
142
fixed degree.
143
144
INPUT:
145
146
- ``n`` -- integer; the number of generators
147
148
- ``base_ring`` -- ring; the base ring over which the homology is computed
149
150
- ``inv_fac`` -- list of integers; the invariant factors -- ignored
151
if the base ring is a field
152
153
OUTPUT:
154
155
A class that can represent the homology group in a fixed
156
homological degree.
157
158
EXAMPLES::
159
160
sage: from sage.homology.homology_group import HomologyGroup
161
sage: G = AbelianGroup(5, [5,5,7,8,9]); G
162
Multiplicative Abelian group isomorphic to C5 x C5 x C7 x C8 x C9
163
sage: H = HomologyGroup(5, ZZ, [5,5,7,8,9]); H
164
C5 x C5 x C7 x C8 x C9
165
sage: AbelianGroup(4)
166
Multiplicative Abelian group isomorphic to Z x Z x Z x Z
167
sage: HomologyGroup(4, ZZ)
168
Z x Z x Z x Z
169
sage: HomologyGroup(100, ZZ)
170
Z^100
171
"""
172
if base_ring.is_field():
173
return VectorSpace(base_ring, n)
174
175
# copied from AbelianGroup:
176
if invfac is None:
177
if isinstance(n, (list, tuple)):
178
invfac = n
179
n = len(n)
180
else:
181
invfac = []
182
if len(invfac) < n:
183
invfac = [0] * (n - len(invfac)) + invfac
184
elif len(invfac) > n:
185
raise ValueError("invfac (={}) must have length n (={})".format(invfac, n))
186
M = HomologyGroup_class(n, invfac)
187
return M
188
189
190