Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/modules/free_module_homspace.py
4056 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
80
81
def is_FreeModuleHomspace(x):
82
r"""
83
Return ``True`` if ``x`` is a free module homspace.
84
85
EXAMPLES:
86
87
Notice that every vector space is a field, but when we construct a set of
88
morphisms between two vector spaces, it is a ``VectorSpaceHomspace``,
89
which qualifies as a ``FreeModuleHomspace``, since the former is
90
special case of the latter.
91
92
sage: H = Hom(ZZ^3, ZZ^2)
93
sage: type(H)
94
<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'>
95
sage: sage.modules.free_module_homspace.is_FreeModuleHomspace(H)
96
True
97
98
sage: K = Hom(QQ^3, ZZ^2)
99
sage: type(K)
100
<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'>
101
sage: sage.modules.free_module_homspace.is_FreeModuleHomspace(K)
102
True
103
104
sage: L = Hom(ZZ^3, QQ^2)
105
sage: type(L)
106
<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'>
107
sage: sage.modules.free_module_homspace.is_FreeModuleHomspace(L)
108
True
109
110
sage: P = Hom(QQ^3, QQ^2)
111
sage: type(P)
112
<class 'sage.modules.vector_space_homspace.VectorSpaceHomspace_with_category'>
113
sage: sage.modules.free_module_homspace.is_FreeModuleHomspace(P)
114
True
115
116
sage: sage.modules.free_module_homspace.is_FreeModuleHomspace('junk')
117
False
118
"""
119
return isinstance(x, FreeModuleHomspace)
120
121
class FreeModuleHomspace(sage.categories.homset.HomsetWithBase):
122
def __call__(self, A, check=True):
123
r"""
124
INPUT:
125
126
- A -- either a matrix or a list/tuple of images of generators,
127
or a function returning elements of the codomain for elements
128
of the domain.
129
- check -- bool (default: True)
130
131
If A is a matrix, then it is the matrix of this linear
132
transformation, with respect to the basis for the domain and
133
codomain. Thus the identity matrix always defines the
134
identity morphism.
135
136
EXAMPLES::
137
138
sage: V = (ZZ^3).span_of_basis([[1,1,0],[1,0,2]])
139
sage: H = V.Hom(V); H
140
Set of Morphisms from ...
141
sage: H([V.0,V.1]) # indirect doctest
142
Free module morphism defined by the matrix
143
[1 0]
144
[0 1]...
145
sage: phi = H([V.1,V.0]); phi
146
Free module morphism defined by the matrix
147
[0 1]
148
[1 0]...
149
sage: phi(V.1) == V.0
150
True
151
sage: phi(V.0) == V.1
152
True
153
154
The following tests against a bug that was fixed in trac
155
ticket #9944. The method ``zero()`` calls this hom space with
156
a function, not with a matrix, and that case had previously
157
not been taken care of::
158
159
sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ)
160
sage: V.Hom(V).zero() # indirect doctest
161
Free module morphism defined by the matrix
162
[0 0 0]
163
[0 0 0]
164
[0 0 0]
165
Domain: Free module of degree 3 and rank 3 over Integer Ring
166
Echelon ...
167
Codomain: Free module of degree 3 and rank 3 over Integer Ring
168
Echelon ...
169
170
"""
171
if not matrix.is_Matrix(A):
172
# Compute the matrix of the morphism that sends the
173
# generators of the domain to the elements of A.
174
C = self.codomain()
175
if isfunction(A):
176
try:
177
v = [C(A(g)) for g in self.domain().gens()]
178
A = matrix.matrix([C.coordinates(a) for a in v])
179
except TypeError, msg:
180
# Let us hope that FreeModuleMorphism knows to handle that case
181
pass
182
else:
183
try:
184
v = [C(a) for a in A]
185
A = matrix.matrix([C.coordinates(a) for a in v])
186
except TypeError, msg:
187
# Let us hope that FreeModuleMorphism knows to handle that case
188
pass
189
return free_module_morphism.FreeModuleMorphism(self, A)
190
191
def _matrix_space(self):
192
"""
193
Return underlying matrix space that contains the matrices that define
194
the homomorphisms in this free module homspace.
195
196
OUTPUT:
197
198
- matrix space
199
200
EXAMPLES::
201
202
sage: H = Hom(QQ^3, QQ^2)
203
sage: H._matrix_space()
204
Full MatrixSpace of 3 by 2 dense matrices over Rational Field
205
"""
206
try:
207
return self.__matrix_space
208
except AttributeError:
209
R = self.domain().base_ring()
210
M = matrix.MatrixSpace(R, self.domain().rank(), self.codomain().rank())
211
self.__matrix_space = M
212
return M
213
214
def basis(self):
215
"""
216
Return a basis for this space of free module homomorphisms.
217
218
OUTPUT:
219
220
- tuple
221
222
EXAMPLES::
223
224
sage: H = Hom(ZZ^2, ZZ^1)
225
sage: H.basis()
226
(Free module morphism defined by the matrix
227
[1]
228
[0]
229
Domain: Ambient free module of rank 2 over the principal ideal domain ...
230
Codomain: Ambient free module of rank 1 over the principal ideal domain ..., Free module morphism defined by the matrix
231
[0]
232
[1]
233
Domain: Ambient free module of rank 2 over the principal ideal domain ...
234
Codomain: Ambient free module of rank 1 over the principal ideal domain ...)
235
"""
236
try:
237
return self.__basis
238
except AttributeError:
239
M = self._matrix_space()
240
B = M.basis()
241
self.__basis = tuple([self(x) for x in B])
242
return self.__basis
243
244
def identity(self):
245
r"""
246
Return identity morphism in an endomorphism ring.
247
248
EXAMPLE::
249
250
sage: V=FreeModule(ZZ,5)
251
sage: H=V.Hom(V)
252
sage: H.identity()
253
Free module morphism defined by the matrix
254
[1 0 0 0 0]
255
[0 1 0 0 0]
256
[0 0 1 0 0]
257
[0 0 0 1 0]
258
[0 0 0 0 1]
259
Domain: Ambient free module of rank 5 over the principal ideal domain ...
260
Codomain: Ambient free module of rank 5 over the principal ideal domain ...
261
"""
262
if self.is_endomorphism_set():
263
return self(matrix.identity_matrix(self.base_ring(),self.domain().rank()))
264
else:
265
raise TypeError, "Identity map only defined for endomorphisms. Try natural_map() instead."
266
267
268