Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/modules/module.pyx
4036 views
1
"""
2
Abstract base class for modules
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
import random
21
from sage.structure.parent_gens cimport ParentWithAdditiveAbelianGens
22
from sage.structure.parent cimport Parent
23
24
25
26
# This is the old Module class which does not conform to the new
27
# coercion model. Eventually all occurences shall be ported to the new
28
# Module class, which is defined below.
29
30
cdef class Module_old(sage.structure.parent_gens.ParentWithAdditiveAbelianGens):
31
"""
32
Generic module class.
33
"""
34
35
def category(self):
36
"""
37
Return the category to which this module belongs.
38
"""
39
# Defining a category method is deprecated for parents.
40
# Instead, the category should be specified in the constructor.
41
# See: http://sagetrac.org/sage_trac/wiki/CategoriesRoadMap
42
if self._is_category_initialized():
43
return Parent.category(self)
44
from sage.categories.modules import Modules
45
return Modules(self.base_ring())
46
47
def endomorphism_ring(self):
48
"""
49
Return the endomorphism ring of this module in its category.
50
"""
51
try:
52
return self.__endomorphism_ring
53
except AttributeError:
54
import sage.categories.all
55
E = sage.categories.all.End(self)
56
# the following is invalid code, you can't add attributes
57
# to Cython classes like this. I guess nobody calls this
58
# method.
59
self.__endomorphism_ring = E
60
return E
61
62
63
def is_atomic_repr(self):
64
"""
65
True if the elements have atomic string representations, in the
66
sense that they print if they print at s, then -s means the
67
negative of s. For example, integers are atomic but polynomials are
68
not.
69
"""
70
return False
71
72
73
74
75
76
### The new Module class that should be the base of all Modules
77
### The derived Module class must implement the element
78
### constructor:
79
#
80
# class MyModule(sage.modules.module.Module):
81
# Element = MyElement
82
# def _element_constructor_(self, x):
83
# return self.element_class(x)
84
#
85
### The Element should also implement _rmul_ (or _lmul_)
86
#
87
# class MyElement(sage.structure.element.ModuleElement):
88
# def _rmul_(self, c):
89
# ...
90
91
cdef class Module(sage.structure.parent.Parent):
92
"""
93
Generic module class.
94
95
EXAMPLES::
96
97
sage: from sage.modules.module import Module
98
sage: M = Module(ZZ)
99
sage: M.category()
100
Category of modules over Integer Ring
101
sage: M.category().required_methods()
102
{'parent': {'required': ['__contains__'],
103
'optional': []},
104
'element': {'required': [],
105
'optional': ['_add_']}}
106
sage: M_QQ = Module(QQ)
107
sage: M_QQ.category()
108
Category of vector spaces over Rational Field
109
110
TESTS:
111
112
We check for #8119::
113
114
sage: M = ZZ^3
115
sage: h = M.__hash__()
116
sage: M.rename('toto')
117
sage: h == M.__hash__()
118
True
119
"""
120
121
def __init__(self, base):
122
"""
123
Construct a module and set the category.
124
125
INPUT:
126
127
- ``base`` -- a ring. The base ring of the module.
128
129
EXAMPLES::
130
131
sage: from sage.modules.module import Module
132
sage: M = Module(ZZ); M
133
<type 'sage.modules.module.Module'>
134
sage: M.base_ring()
135
Integer Ring
136
"""
137
from sage.categories.modules import Modules
138
Parent.__init__(self, base=base, category=Modules(base))
139
140
141
def endomorphism_ring(self):
142
"""
143
Return the endomorphism ring of this module in its category.
144
145
EXAMPLES::
146
147
sage: from sage.modules.module import Module
148
sage: M = Module(ZZ); M
149
<type 'sage.modules.module.Module'>
150
sage: M.endomorphism_ring()
151
Set of Morphisms from <type 'sage.modules.module.Module'> to
152
<type 'sage.modules.module.Module'> in Category of
153
modules over Integer Ring
154
"""
155
from sage.categories.all import End
156
return End(self)
157
158
159
def is_atomic_repr(self):
160
"""
161
Whether the elements have atomic string representations.
162
163
OUTPUT:
164
165
Boolean. ``True`` if the elements have atomic string
166
representations, in the sense that they print if they print
167
``s``, then ``-s`` means the negative of ``s``. For example,
168
integers are atomic but polynomials are not.
169
170
EXAMPLES::
171
172
sage: from sage.modules.module import Module
173
sage: M = Module(ZZ)
174
sage: M.is_atomic_repr()
175
False
176
sage: ZZ.is_atomic_repr()
177
True
178
"""
179
return False
180
181
182
183
184
def is_Module(x):
185
"""
186
Return True if x is a module.
187
188
INPUT:
189
190
- ``x`` -- anything.
191
192
OUTPUT:
193
194
Boolean.
195
196
EXAMPLES::
197
198
sage: from sage.modules.module import is_Module
199
sage: M = FreeModule(RationalField(),30)
200
sage: is_Module(M)
201
True
202
sage: is_Module(10)
203
False
204
"""
205
return isinstance(x, Module) or isinstance(x, Module_old)
206
207
208
def is_VectorSpace(x):
209
"""
210
Return True if x is a vector space.
211
212
INPUT:
213
214
- ``x`` -- anything.
215
216
OUTPUT:
217
218
Boolean.
219
220
EXAMPLES::
221
222
sage: from sage.modules.module import is_Module, is_VectorSpace
223
sage: M = FreeModule(RationalField(),30)
224
sage: is_VectorSpace(M)
225
True
226
sage: M = FreeModule(IntegerRing(),30)
227
sage: is_Module(M)
228
True
229
sage: is_VectorSpace(M)
230
False
231
"""
232
try:
233
return is_Module(x) and x.base_ring().is_field()
234
except AttributeError:
235
return False
236
237
238
239