Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/modules/vector_space_homspace.py
8815 views
1
r"""
2
Space of Morphisms of Vector Spaces (Linear Transformations)
3
4
AUTHOR:
5
6
- Rob Beezer: (2011-06-29)
7
8
A :class:`VectorSpaceHomspace` object represents the set of all
9
possible homomorphisms from one vector space to another.
10
These mappings are usually known as linear transformations.
11
12
For more information on the use of linear transformations,
13
consult the documentation for vector space morphisms at
14
:mod:`sage.modules.vector_space_morphism`. Also, this is
15
an extremely thin veneer on free module homspaces
16
(:mod:`sage.modules.free_module_homspace`) and free module
17
morphisms (:mod:`sage.modules.free_module_morphism`) -
18
objects which might also be useful, and places
19
where much of the documentation resides.
20
21
EXAMPLES:
22
23
Creation and basic examination is simple. ::
24
25
sage: V = QQ^3
26
sage: W = QQ^2
27
sage: H = Hom(V, W)
28
sage: H
29
Set of Morphisms (Linear Transformations) from
30
Vector space of dimension 3 over Rational Field to
31
Vector space of dimension 2 over Rational Field
32
sage: H.domain()
33
Vector space of dimension 3 over Rational Field
34
sage: H.codomain()
35
Vector space of dimension 2 over Rational Field
36
37
Homspaces have a few useful properties. A basis is provided by
38
a list of matrix representations, where these matrix representatives
39
are relative to the bases of the domain and codomain. ::
40
41
sage: K = Hom(GF(3)^2, GF(3)^2)
42
sage: B = K.basis()
43
sage: for f in B:
44
... print f, "\n"
45
Vector space morphism represented by the matrix:
46
[1 0]
47
[0 0]
48
Domain: Vector space of dimension 2 over Finite Field of size 3
49
Codomain: Vector space of dimension 2 over Finite Field of size 3
50
<BLANKLINE>
51
Vector space morphism represented by the matrix:
52
[0 1]
53
[0 0]
54
Domain: Vector space of dimension 2 over Finite Field of size 3
55
Codomain: Vector space of dimension 2 over Finite Field of size 3
56
<BLANKLINE>
57
Vector space morphism represented by the matrix:
58
[0 0]
59
[1 0]
60
Domain: Vector space of dimension 2 over Finite Field of size 3
61
Codomain: Vector space of dimension 2 over Finite Field of size 3
62
<BLANKLINE>
63
Vector space morphism represented by the matrix:
64
[0 0]
65
[0 1]
66
Domain: Vector space of dimension 2 over Finite Field of size 3
67
Codomain: Vector space of dimension 2 over Finite Field of size 3
68
<BLANKLINE>
69
70
The zero and identity mappings are properties of the space.
71
The identity mapping will only be available if the domain and codomain
72
allow for endomorphisms (equal vector spaces with equal bases). ::
73
74
sage: H = Hom(QQ^3, QQ^3)
75
sage: g = H.zero()
76
sage: g([1, 1/2, -3])
77
(0, 0, 0)
78
sage: f = H.identity()
79
sage: f([1, 1/2, -3])
80
(1, 1/2, -3)
81
82
The homspace may be used with various representations of a
83
morphism in the space to create the morphism. We demonstrate
84
three ways to create the same linear transformation between
85
two two-dimensional subspaces of ``QQ^3``. The ``V.n`` notation
86
is a shortcut to the generators of each vector space, better
87
known as the basis elements. Note that the matrix representations
88
are relative to the bases, which are purposely fixed when the
89
subspaces are created ("user bases"). ::
90
91
sage: U = QQ^3
92
sage: V = U.subspace_with_basis([U.0+U.1, U.1-U.2])
93
sage: W = U.subspace_with_basis([U.0, U.1+U.2])
94
sage: H = Hom(V, W)
95
96
First, with a matrix. Note that the matrix representation
97
acts by matrix multiplication with the vector on the left.
98
The input to the linear transformation, ``(3, 1, 2)``,
99
is converted to the coordinate vector ``(3, -2)``, then
100
matrix multiplication yields the vector ``(-3, -2)``,
101
which represents the vector ``(-3, -2, -2)`` in the codomain. ::
102
103
sage: m = matrix(QQ, [[1, 2], [3, 4]])
104
sage: f1 = H(m)
105
sage: f1
106
Vector space morphism represented by the matrix:
107
[1 2]
108
[3 4]
109
Domain: Vector space of degree 3 and dimension 2 over Rational Field
110
User basis matrix:
111
[ 1 1 0]
112
[ 0 1 -1]
113
Codomain: Vector space of degree 3 and dimension 2 over Rational Field
114
User basis matrix:
115
[1 0 0]
116
[0 1 1]
117
sage: f1([3,1,2])
118
(-3, -2, -2)
119
120
Second, with a list of images of the domain's basis elements. ::
121
122
sage: img = [1*(U.0) + 2*(U.1+U.2), 3*U.0 + 4*(U.1+U.2)]
123
sage: f2 = H(img)
124
sage: f2
125
Vector space morphism represented by the matrix:
126
[1 2]
127
[3 4]
128
Domain: Vector space of degree 3 and dimension 2 over Rational Field
129
User basis matrix:
130
[ 1 1 0]
131
[ 0 1 -1]
132
Codomain: Vector space of degree 3 and dimension 2 over Rational Field
133
User basis matrix:
134
[1 0 0]
135
[0 1 1]
136
sage: f2([3,1,2])
137
(-3, -2, -2)
138
139
Third, with a linear function taking the domain to the codomain. ::
140
141
sage: g = lambda x: vector(QQ, [-2*x[0]+3*x[1], -2*x[0]+4*x[1], -2*x[0]+4*x[1]])
142
sage: f3 = H(g)
143
sage: f3
144
Vector space morphism represented by the matrix:
145
[1 2]
146
[3 4]
147
Domain: Vector space of degree 3 and dimension 2 over Rational Field
148
User basis matrix:
149
[ 1 1 0]
150
[ 0 1 -1]
151
Codomain: Vector space of degree 3 and dimension 2 over Rational Field
152
User basis matrix:
153
[1 0 0]
154
[0 1 1]
155
sage: f3([3,1,2])
156
(-3, -2, -2)
157
158
The three linear transformations look the same, and are the same. ::
159
160
sage: f1 == f2
161
True
162
sage: f2 == f3
163
True
164
165
TESTS::
166
167
sage: V = QQ^2
168
sage: W = QQ^3
169
sage: H = Hom(QQ^2, QQ^3)
170
sage: loads(dumps(H))
171
Set of Morphisms (Linear Transformations) from
172
Vector space of dimension 2 over Rational Field to
173
Vector space of dimension 3 over Rational Field
174
sage: loads(dumps(H)) == H
175
True
176
"""
177
178
####################################################################################
179
# Copyright (C) 2011 Rob Beezer <[email protected]>
180
#
181
# Distributed under the terms of the GNU General Public License (GPL)
182
#
183
# This code is distributed in the hope that it will be useful,
184
# but WITHOUT ANY WARRANTY; without even the implied warranty of
185
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
186
# General Public License for more details.
187
#
188
# The full text of the GPL is available at:
189
#
190
# http://www.gnu.org/licenses/
191
####################################################################################
192
193
import inspect
194
import sage.matrix.all as matrix
195
import sage.modules.free_module_homspace
196
197
# This module initially overrides just the minimum functionality necessary
198
# from sage.modules.free_module_homspace.FreeModuleHomSpace.
199
# If additional methods here override the free module homspace methods,
200
# consider adjusting the free module doctests, since many are written with
201
# examples that are actually vector spaces and not so many use "pure" modules
202
# for the examples.
203
204
205
def is_VectorSpaceHomspace(x):
206
r"""
207
Return ``True`` if ``x`` is a vector space homspace.
208
209
INPUT:
210
211
``x`` - anything
212
213
EXAMPLES:
214
215
To be a vector space morphism, the domain and codomain must both be
216
vector spaces, in other words, modules over fields. If either
217
set is just a module, then the ``Hom()`` constructor will build a
218
space of free module morphisms. ::
219
220
sage: H = Hom(QQ^3, QQ^2)
221
sage: type(H)
222
<class 'sage.modules.vector_space_homspace.VectorSpaceHomspace_with_category'>
223
sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace(H)
224
True
225
226
sage: K = Hom(QQ^3, ZZ^2)
227
sage: type(K)
228
<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'>
229
sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace(K)
230
False
231
232
sage: L = Hom(ZZ^3, QQ^2)
233
sage: type(L)
234
<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'>
235
sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace(L)
236
False
237
238
sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace('junk')
239
False
240
"""
241
return isinstance(x, VectorSpaceHomspace)
242
243
class VectorSpaceHomspace(sage.modules.free_module_homspace.FreeModuleHomspace):
244
245
def __call__(self, A, check=True):
246
r"""
247
INPUT:
248
249
- ``A`` - one of several possible inputs representing
250
a morphism from this vector space homspace.
251
- a vector space morphism in this homspace
252
- a matrix representation relative to the bases of the vector spaces,
253
which acts on a vector placed to the left of the matrix
254
- a list or tuple containing images of the domain's basis vectors
255
- a function from the domain to the codomain
256
- ``check`` (default: True) - ``True`` or ``False``, required for
257
compatibility with calls from
258
:meth:`sage.structure.parent_gens.ParentWithGens.hom`.
259
260
EXAMPLES::
261
262
sage: V = (QQ^3).span_of_basis([[1,1,0],[1,0,2]])
263
sage: H = V.Hom(V)
264
sage: H
265
Set of Morphisms (Linear Transformations) from
266
Vector space of degree 3 and dimension 2 over Rational Field
267
User basis matrix:
268
[1 1 0]
269
[1 0 2]
270
to
271
Vector space of degree 3 and dimension 2 over Rational Field
272
User basis matrix:
273
[1 1 0]
274
[1 0 2]
275
276
Coercing a matrix::
277
278
sage: A = matrix(QQ, [[0, 1], [1, 0]])
279
sage: rho = H(A) # indirect doctest
280
sage: rho
281
Vector space morphism represented by the matrix:
282
[0 1]
283
[1 0]
284
Domain: Vector space of degree 3 and dimension 2 over Rational Field
285
User basis matrix:
286
[1 1 0]
287
[1 0 2]
288
Codomain: Vector space of degree 3 and dimension 2 over Rational Field
289
User basis matrix:
290
[1 1 0]
291
[1 0 2]
292
293
Coercing a list of images::
294
295
sage: phi = H([V.1, V.0])
296
sage: phi(V.1) == V.0
297
True
298
sage: phi(V.0) == V.1
299
True
300
sage: phi
301
Vector space morphism represented by the matrix:
302
[0 1]
303
[1 0]
304
Domain: Vector space of degree 3 and dimension 2 over Rational Field
305
User basis matrix:
306
[1 1 0]
307
[1 0 2]
308
Codomain: Vector space of degree 3 and dimension 2 over Rational Field
309
User basis matrix:
310
[1 1 0]
311
[1 0 2]
312
313
Coercing a lambda function::
314
315
sage: f = lambda x: vector(QQ, [x[0], (1/2)*x[2], 2*x[1]])
316
sage: zeta = H(f)
317
sage: zeta
318
Vector space morphism represented by the matrix:
319
[0 1]
320
[1 0]
321
Domain: Vector space of degree 3 and dimension 2 over Rational Field
322
User basis matrix:
323
[1 1 0]
324
[1 0 2]
325
Codomain: Vector space of degree 3 and dimension 2 over Rational Field
326
User basis matrix:
327
[1 1 0]
328
[1 0 2]
329
330
Coercing a vector space morphism into the parent of a second vector
331
space morphism will unify their parents::
332
333
sage: U = FreeModule(QQ,3, sparse=True ); V = QQ^4
334
sage: W = FreeModule(QQ,3, sparse=False); X = QQ^4
335
sage: H = Hom(U, V)
336
sage: K = Hom(W, X)
337
sage: H is K, H == K
338
(False, True)
339
340
sage: A = matrix(QQ, 3, 4, [0]*12)
341
sage: f = H(A)
342
sage: B = matrix(QQ, 3, 4, range(12))
343
sage: g = K(B)
344
sage: f.parent() is H and g.parent() is K
345
True
346
347
sage: h = H(g)
348
sage: f.parent() is h.parent()
349
True
350
351
See other examples in the module-level documentation.
352
353
TESTS::
354
355
sage: V = GF(3)^0
356
sage: W = GF(3)^1
357
sage: H = V.Hom(W)
358
sage: H.zero_element().is_zero()
359
True
360
361
Previously the above code resulted in a TypeError because the
362
dimensions of the matrix were incorrect.
363
"""
364
from vector_space_morphism import is_VectorSpaceMorphism, VectorSpaceMorphism
365
D = self.domain()
366
C = self.codomain()
367
from sage.matrix.matrix import is_Matrix
368
if is_Matrix(A):
369
pass
370
elif is_VectorSpaceMorphism(A):
371
A = A.matrix()
372
elif inspect.isfunction(A):
373
try:
374
images = [A(g) for g in D.basis()]
375
except (ValueError, TypeError, IndexError), e:
376
msg = 'function cannot be applied properly to some basis element because\n' + e.args[0]
377
raise ValueError(msg)
378
try:
379
A = matrix.matrix(D.dimension(), C.dimension(), [C.coordinates(C(a)) for a in images])
380
except (ArithmeticError, TypeError), e:
381
msg = 'some image of the function is not in the codomain, because\n' + e.args[0]
382
raise ArithmeticError(msg)
383
elif isinstance(A, (list, tuple)):
384
if len(A) != len(D.basis()):
385
msg = "number of images should equal the size of the domain's basis (={0}), not {1}"
386
raise ValueError(msg.format(len(D.basis()), len(A)))
387
try:
388
v = [C(a) for a in A]
389
A = matrix.matrix(D.dimension(), C.dimension(), [C.coordinates(a) for a in v])
390
except (ArithmeticError, TypeError), e:
391
msg = 'some proposed image is not in the codomain, because\n' + e.args[0]
392
raise ArithmeticError(msg)
393
else:
394
msg = 'vector space homspace can only coerce matrices, vector space morphisms, functions or lists, not {0}'
395
raise TypeError(msg.format(A))
396
return VectorSpaceMorphism(self, A)
397
398
def _repr_(self):
399
r"""
400
Text representation of a space of vector space morphisms.
401
402
EXAMPLE::
403
404
sage: H = Hom(QQ^2, QQ^3)
405
sage: H._repr_().split(' ')
406
['Set', 'of', 'Morphisms', '(Linear', 'Transformations)',
407
'from', 'Vector', 'space', 'of', 'dimension', '2', 'over',
408
'Rational', 'Field', 'to', 'Vector', 'space', 'of',
409
'dimension', '3', 'over', 'Rational', 'Field']
410
"""
411
msg = 'Set of Morphisms (Linear Transformations) from {0} to {1}'
412
return msg.format(self.domain(), self.codomain())
413
414