Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/modules/vector_space_homspace.py
4045 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
import vector_space_morphism
197
198
# This module initially overrides just the minimum functionality necessary
199
# from sage.modules.free_module_homspace.FreeModuleHomSpace.
200
# If additional methods here override the free module homspace methods,
201
# consider adjusting the free module doctests, since many are written with
202
# examples that are actually vector spaces and not so many use "pure" modules
203
# for the examples.
204
205
206
def is_VectorSpaceHomspace(x):
207
r"""
208
Return ``True`` if ``x`` is a vector space homspace.
209
210
INPUT:
211
212
``x`` - anything
213
214
EXAMPLES:
215
216
To be a vector space morphism, the domain and codomain must both be
217
vector spaces, in other words, modules over fields. If either
218
set is just a module, then the ``Hom()`` constructor will build a
219
space of free module morphisms. ::
220
221
sage: H = Hom(QQ^3, QQ^2)
222
sage: type(H)
223
<class 'sage.modules.vector_space_homspace.VectorSpaceHomspace_with_category'>
224
sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace(H)
225
True
226
227
sage: K = Hom(QQ^3, ZZ^2)
228
sage: type(K)
229
<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'>
230
sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace(K)
231
False
232
233
sage: L = Hom(ZZ^3, QQ^2)
234
sage: type(L)
235
<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'>
236
sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace(L)
237
False
238
239
sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace('junk')
240
False
241
"""
242
return isinstance(x, VectorSpaceHomspace)
243
244
class VectorSpaceHomspace(sage.modules.free_module_homspace.FreeModuleHomspace):
245
246
def __call__(self, A, check=True):
247
r"""
248
INPUT:
249
250
- ``A`` - one of several possible inputs representing
251
a morphism from this vector space homspace.
252
- a vector space morphism in this homspace
253
- a matrix representation relative to the bases of the vector spaces,
254
which acts on a vector placed to the left of the matrix
255
- a list or tuple containing images of the domain's basis vectors
256
- a function from the domain to the codomain
257
- ``check`` (default: True) - ``True`` or ``False``, required for
258
compatibility with calls from
259
:meth:`sage.structure.parent_gens.ParentWithGens.hom`.
260
261
EXAMPLES::
262
263
sage: V = (QQ^3).span_of_basis([[1,1,0],[1,0,2]])
264
sage: H = V.Hom(V)
265
sage: H
266
Set of Morphisms (Linear Transformations) from
267
Vector space of degree 3 and dimension 2 over Rational Field
268
User basis matrix:
269
[1 1 0]
270
[1 0 2]
271
to
272
Vector space of degree 3 and dimension 2 over Rational Field
273
User basis matrix:
274
[1 1 0]
275
[1 0 2]
276
277
Coercing a matrix::
278
279
sage: A = matrix(QQ, [[0, 1], [1, 0]])
280
sage: rho = H(A) # indirect doctest
281
sage: rho
282
Vector space morphism represented by the matrix:
283
[0 1]
284
[1 0]
285
Domain: Vector space of degree 3 and dimension 2 over Rational Field
286
User basis matrix:
287
[1 1 0]
288
[1 0 2]
289
Codomain: Vector space of degree 3 and dimension 2 over Rational Field
290
User basis matrix:
291
[1 1 0]
292
[1 0 2]
293
294
Coercing a list of images::
295
296
sage: phi = H([V.1, V.0])
297
sage: phi(V.1) == V.0
298
True
299
sage: phi(V.0) == V.1
300
True
301
sage: phi
302
Vector space morphism represented by the matrix:
303
[0 1]
304
[1 0]
305
Domain: Vector space of degree 3 and dimension 2 over Rational Field
306
User basis matrix:
307
[1 1 0]
308
[1 0 2]
309
Codomain: Vector space of degree 3 and dimension 2 over Rational Field
310
User basis matrix:
311
[1 1 0]
312
[1 0 2]
313
314
Coercing a lambda function::
315
316
sage: f = lambda x: vector(QQ, [x[0], (1/2)*x[2], 2*x[1]])
317
sage: zeta = H(f)
318
sage: zeta
319
Vector space morphism represented by the matrix:
320
[0 1]
321
[1 0]
322
Domain: Vector space of degree 3 and dimension 2 over Rational Field
323
User basis matrix:
324
[1 1 0]
325
[1 0 2]
326
Codomain: Vector space of degree 3 and dimension 2 over Rational Field
327
User basis matrix:
328
[1 1 0]
329
[1 0 2]
330
331
Coercing a vector space morphism into the parent of a second vector
332
space morphism will unify their parents. ::
333
334
sage: U = QQ^3
335
sage: V = QQ^4
336
sage: W = QQ^3
337
sage: X = QQ^4
338
sage: H = Hom(U, V)
339
sage: K = Hom(W, X)
340
341
sage: A = matrix(QQ, 3, 4, [0]*12)
342
sage: f = H(A)
343
sage: B = matrix(QQ, 3, 4, range(12))
344
sage: g = K(B)
345
sage: f.parent() is g.parent()
346
False
347
348
sage: h = H(g)
349
sage: f.parent() is h.parent()
350
True
351
352
See other examples in the module-level documentation.
353
354
TESTS::
355
356
sage: V = GF(3)^0
357
sage: W = GF(3)^1
358
sage: H = V.Hom(W)
359
sage: H.zero_element().is_zero()
360
True
361
362
Previously the above code resulted in a TypeError because the
363
dimensions of the matrix were incorrect.
364
"""
365
import vector_space_morphism
366
D = self.domain()
367
C = self.codomain()
368
if matrix.is_Matrix(A):
369
pass
370
elif vector_space_morphism.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 vector_space_morphism.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