Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/modules/free_module_homspace.py
8815 views
1
r"""
2
Homspaces between free modules
3
4
EXAMPLES: We create `\mathrm{End}(\ZZ^2)` and compute a
5
basis.
6
7
::
8
9
sage: M = FreeModule(IntegerRing(),2)
10
sage: E = End(M)
11
sage: B = E.basis()
12
sage: len(B)
13
4
14
sage: B[0]
15
Free module morphism defined by the matrix
16
[1 0]
17
[0 0]
18
Domain: Ambient free module of rank 2 over the principal ideal domain ...
19
Codomain: Ambient free module of rank 2 over the principal ideal domain ...
20
21
We create `\mathrm{Hom}(\ZZ^3, \ZZ^2)` and
22
compute a basis.
23
24
::
25
26
sage: V3 = FreeModule(IntegerRing(),3)
27
sage: V2 = FreeModule(IntegerRing(),2)
28
sage: H = Hom(V3,V2)
29
sage: H
30
Set of Morphisms from Ambient free module of rank 3
31
over the principal ideal domain Integer Ring to
32
Ambient free module of rank 2
33
over the principal ideal domain Integer Ring
34
in Category of modules with basis over Integer Ring
35
sage: B = H.basis()
36
sage: len(B)
37
6
38
sage: B[0]
39
Free module morphism defined by the matrix
40
[1 0]
41
[0 0]
42
[0 0]...
43
44
TESTS::
45
46
sage: H = Hom(QQ^2, QQ^1)
47
sage: loads(dumps(H)) == H
48
True
49
50
See trac 5886::
51
52
sage: V = (ZZ^2).span_of_basis([[1,2],[3,4]])
53
sage: V.hom([V.0, V.1])
54
Free module morphism defined by the matrix
55
[1 0]
56
[0 1]...
57
58
"""
59
60
#*****************************************************************************
61
# Copyright (C) 2005 William Stein <[email protected]>
62
#
63
# Distributed under the terms of the GNU General Public License (GPL)
64
#
65
# This code is distributed in the hope that it will be useful,
66
# but WITHOUT ANY WARRANTY; without even the implied warranty
67
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
68
#
69
# See the GNU General Public License for more details; the full text
70
# is available at:
71
#
72
# http://www.gnu.org/licenses/
73
#*****************************************************************************
74
75
import sage.categories.homset
76
import sage.matrix.all as matrix
77
import free_module_morphism
78
from inspect import isfunction
79
from sage.misc.cachefunc import cached_method
80
81
82
def is_FreeModuleHomspace(x):
83
r"""
84
Return ``True`` if ``x`` is a free module homspace.
85
86
EXAMPLES:
87
88
Notice that every vector space is a free module, but when we construct
89
a set of morphisms between two vector spaces, it is a
90
``VectorSpaceHomspace``, which qualifies as a ``FreeModuleHomspace``,
91
since the former is special case of the latter.
92
93
sage: H = Hom(ZZ^3, ZZ^2)
94
sage: type(H)
95
<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'>
96
sage: sage.modules.free_module_homspace.is_FreeModuleHomspace(H)
97
True
98
99
sage: K = Hom(QQ^3, ZZ^2)
100
sage: type(K)
101
<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'>
102
sage: sage.modules.free_module_homspace.is_FreeModuleHomspace(K)
103
True
104
105
sage: L = Hom(ZZ^3, QQ^2)
106
sage: type(L)
107
<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'>
108
sage: sage.modules.free_module_homspace.is_FreeModuleHomspace(L)
109
True
110
111
sage: P = Hom(QQ^3, QQ^2)
112
sage: type(P)
113
<class 'sage.modules.vector_space_homspace.VectorSpaceHomspace_with_category'>
114
sage: sage.modules.free_module_homspace.is_FreeModuleHomspace(P)
115
True
116
117
sage: sage.modules.free_module_homspace.is_FreeModuleHomspace('junk')
118
False
119
"""
120
return isinstance(x, FreeModuleHomspace)
121
122
class FreeModuleHomspace(sage.categories.homset.HomsetWithBase):
123
def __call__(self, A, check=True):
124
r"""
125
INPUT:
126
127
- A -- either a matrix or a list/tuple of images of generators,
128
or a function returning elements of the codomain for elements
129
of the domain.
130
- check -- bool (default: True)
131
132
If A is a matrix, then it is the matrix of this linear
133
transformation, with respect to the basis for the domain and
134
codomain. Thus the identity matrix always defines the
135
identity morphism.
136
137
EXAMPLES::
138
139
sage: V = (ZZ^3).span_of_basis([[1,1,0],[1,0,2]])
140
sage: H = V.Hom(V); H
141
Set of Morphisms from ...
142
sage: H([V.0,V.1]) # indirect doctest
143
Free module morphism defined by the matrix
144
[1 0]
145
[0 1]...
146
sage: phi = H([V.1,V.0]); phi
147
Free module morphism defined by the matrix
148
[0 1]
149
[1 0]...
150
sage: phi(V.1) == V.0
151
True
152
sage: phi(V.0) == V.1
153
True
154
155
The following tests against a bug that was fixed in trac
156
ticket #9944. The method ``zero()`` calls this hom space with
157
a function, not with a matrix, and that case had previously
158
not been taken care of::
159
160
sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ)
161
sage: V.Hom(V).zero() # indirect doctest
162
Free module morphism defined by the matrix
163
[0 0 0]
164
[0 0 0]
165
[0 0 0]
166
Domain: Free module of degree 3 and rank 3 over Integer Ring
167
Echelon ...
168
Codomain: Free module of degree 3 and rank 3 over Integer Ring
169
Echelon ...
170
171
"""
172
if not sage.matrix.matrix.is_Matrix(A):
173
# Compute the matrix of the morphism that sends the
174
# generators of the domain to the elements of A.
175
C = self.codomain()
176
if isfunction(A):
177
try:
178
v = [C(A(g)) for g in self.domain().gens()]
179
A = matrix.matrix([C.coordinates(a) for a in v])
180
except TypeError, msg:
181
# Let us hope that FreeModuleMorphism knows to handle that case
182
pass
183
else:
184
try:
185
v = [C(a) for a in A]
186
A = matrix.matrix([C.coordinates(a) for a in v])
187
except TypeError, msg:
188
# Let us hope that FreeModuleMorphism knows to handle that case
189
pass
190
return free_module_morphism.FreeModuleMorphism(self, A)
191
192
@cached_method
193
def zero(self):
194
"""
195
EXAMPLES::
196
197
sage: E = ZZ^2
198
sage: F = ZZ^3
199
sage: H = Hom(E, F)
200
sage: f = H.zero()
201
sage: f
202
Free module morphism defined by the matrix
203
[0 0 0]
204
[0 0 0]
205
Domain: Ambient free module of rank 2 over the principal ideal domain Integer Ring
206
Codomain: Ambient free module of rank 3 over the principal ideal domain Integer Ring
207
sage: f(E.an_element())
208
(0, 0, 0)
209
sage: f(E.an_element()) == F.zero()
210
True
211
212
TESTS:
213
214
We check that ``H.zero()`` is picklable::
215
216
sage: loads(dumps(f.parent().zero()))
217
Free module morphism defined by the matrix
218
[0 0 0]
219
[0 0 0]
220
Domain: Ambient free module of rank 2 over the principal ideal domain Integer Ring
221
Codomain: Ambient free module of rank 3 over the principal ideal domain Integer Ring
222
"""
223
return self(lambda x: self.codomain().zero())
224
225
def _matrix_space(self):
226
"""
227
Return underlying matrix space that contains the matrices that define
228
the homomorphisms in this free module homspace.
229
230
OUTPUT:
231
232
- matrix space
233
234
EXAMPLES::
235
236
sage: H = Hom(QQ^3, QQ^2)
237
sage: H._matrix_space()
238
Full MatrixSpace of 3 by 2 dense matrices over Rational Field
239
"""
240
try:
241
return self.__matrix_space
242
except AttributeError:
243
R = self.domain().base_ring()
244
M = matrix.MatrixSpace(R, self.domain().rank(), self.codomain().rank())
245
self.__matrix_space = M
246
return M
247
248
def basis(self):
249
"""
250
Return a basis for this space of free module homomorphisms.
251
252
OUTPUT:
253
254
- tuple
255
256
EXAMPLES::
257
258
sage: H = Hom(ZZ^2, ZZ^1)
259
sage: H.basis()
260
(Free module morphism defined by the matrix
261
[1]
262
[0]
263
Domain: Ambient free module of rank 2 over the principal ideal domain ...
264
Codomain: Ambient free module of rank 1 over the principal ideal domain ..., Free module morphism defined by the matrix
265
[0]
266
[1]
267
Domain: Ambient free module of rank 2 over the principal ideal domain ...
268
Codomain: Ambient free module of rank 1 over the principal ideal domain ...)
269
"""
270
try:
271
return self.__basis
272
except AttributeError:
273
M = self._matrix_space()
274
B = M.basis()
275
self.__basis = tuple([self(x) for x in B])
276
return self.__basis
277
278
def identity(self):
279
r"""
280
Return identity morphism in an endomorphism ring.
281
282
EXAMPLE::
283
284
sage: V=FreeModule(ZZ,5)
285
sage: H=V.Hom(V)
286
sage: H.identity()
287
Free module morphism defined by the matrix
288
[1 0 0 0 0]
289
[0 1 0 0 0]
290
[0 0 1 0 0]
291
[0 0 0 1 0]
292
[0 0 0 0 1]
293
Domain: Ambient free module of rank 5 over the principal ideal domain ...
294
Codomain: Ambient free module of rank 5 over the principal ideal domain ...
295
"""
296
if self.is_endomorphism_set():
297
return self(matrix.identity_matrix(self.base_ring(),self.domain().rank()))
298
else:
299
raise TypeError, "Identity map only defined for endomorphisms. Try natural_map() instead."
300
301
302