Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/modules/module.pyx
8815 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
64
65
66
### The new Module class that should be the base of all Modules
67
### The derived Module class must implement the element
68
### constructor:
69
#
70
# class MyModule(sage.modules.module.Module):
71
# Element = MyElement
72
# def _element_constructor_(self, x):
73
# return self.element_class(x)
74
#
75
### The Element should also implement _rmul_ (or _lmul_)
76
#
77
# class MyElement(sage.structure.element.ModuleElement):
78
# def _rmul_(self, c):
79
# ...
80
81
cdef class Module(sage.structure.parent.Parent):
82
"""
83
Generic module class.
84
85
EXAMPLES::
86
87
sage: from sage.modules.module import Module
88
sage: M = Module(ZZ)
89
sage: M.category()
90
Category of modules over Integer Ring
91
sage: M.category().required_methods()
92
{'parent': {'required': ['__contains__'], 'optional': []}, 'element': {'required': ['__nonzero__'], 'optional': ['_add_']}}
93
sage: M_QQ = Module(QQ)
94
sage: M_QQ.category()
95
Category of vector spaces over Rational Field
96
97
TESTS:
98
99
We check for #8119::
100
101
sage: M = ZZ^3
102
sage: h = M.__hash__()
103
sage: M.rename('toto')
104
sage: h == M.__hash__()
105
True
106
"""
107
108
def __init__(self, base):
109
"""
110
Construct a module and set the category.
111
112
INPUT:
113
114
- ``base`` -- a ring. The base ring of the module.
115
116
EXAMPLES::
117
118
sage: from sage.modules.module import Module
119
sage: M = Module(ZZ); M
120
<type 'sage.modules.module.Module'>
121
sage: M.base_ring()
122
Integer Ring
123
"""
124
from sage.categories.modules import Modules
125
Parent.__init__(self, base=base, category=Modules(base))
126
127
128
def endomorphism_ring(self):
129
"""
130
Return the endomorphism ring of this module in its category.
131
132
EXAMPLES::
133
134
sage: from sage.modules.module import Module
135
sage: M = Module(ZZ); M
136
<type 'sage.modules.module.Module'>
137
sage: M.endomorphism_ring()
138
Set of Morphisms from <type 'sage.modules.module.Module'> to
139
<type 'sage.modules.module.Module'> in Category of
140
modules over Integer Ring
141
"""
142
from sage.categories.all import End
143
return End(self)
144
145
146
def is_Module(x):
147
"""
148
Return True if x is a module.
149
150
INPUT:
151
152
- ``x`` -- anything.
153
154
OUTPUT:
155
156
Boolean.
157
158
EXAMPLES::
159
160
sage: from sage.modules.module import is_Module
161
sage: M = FreeModule(RationalField(),30)
162
sage: is_Module(M)
163
True
164
sage: is_Module(10)
165
False
166
"""
167
return isinstance(x, Module) or isinstance(x, Module_old)
168
169
170
def is_VectorSpace(x):
171
"""
172
Return True if x is a vector space.
173
174
INPUT:
175
176
- ``x`` -- anything.
177
178
OUTPUT:
179
180
Boolean.
181
182
EXAMPLES::
183
184
sage: from sage.modules.module import is_Module, is_VectorSpace
185
sage: M = FreeModule(RationalField(),30)
186
sage: is_VectorSpace(M)
187
True
188
sage: M = FreeModule(IntegerRing(),30)
189
sage: is_Module(M)
190
True
191
sage: is_VectorSpace(M)
192
False
193
"""
194
try:
195
return is_Module(x) and x.base_ring().is_field()
196
except AttributeError:
197
return False
198
199
200
201