Path: blob/master/sage/modules/vector_space_homspace.py
4045 views
r"""1Space of Morphisms of Vector Spaces (Linear Transformations)23AUTHOR:45- Rob Beezer: (2011-06-29)67A :class:`VectorSpaceHomspace` object represents the set of all8possible homomorphisms from one vector space to another.9These mappings are usually known as linear transformations.1011For more information on the use of linear transformations,12consult the documentation for vector space morphisms at13:mod:`sage.modules.vector_space_morphism`. Also, this is14an extremely thin veneer on free module homspaces15(:mod:`sage.modules.free_module_homspace`) and free module16morphisms (:mod:`sage.modules.free_module_morphism`) -17objects which might also be useful, and places18where much of the documentation resides.1920EXAMPLES:2122Creation and basic examination is simple. ::2324sage: V = QQ^325sage: W = QQ^226sage: H = Hom(V, W)27sage: H28Set of Morphisms (Linear Transformations) from29Vector space of dimension 3 over Rational Field to30Vector space of dimension 2 over Rational Field31sage: H.domain()32Vector space of dimension 3 over Rational Field33sage: H.codomain()34Vector space of dimension 2 over Rational Field3536Homspaces have a few useful properties. A basis is provided by37a list of matrix representations, where these matrix representatives38are relative to the bases of the domain and codomain. ::3940sage: K = Hom(GF(3)^2, GF(3)^2)41sage: B = K.basis()42sage: for f in B:43... print f, "\n"44Vector space morphism represented by the matrix:45[1 0]46[0 0]47Domain: Vector space of dimension 2 over Finite Field of size 348Codomain: Vector space of dimension 2 over Finite Field of size 349<BLANKLINE>50Vector space morphism represented by the matrix:51[0 1]52[0 0]53Domain: Vector space of dimension 2 over Finite Field of size 354Codomain: Vector space of dimension 2 over Finite Field of size 355<BLANKLINE>56Vector space morphism represented by the matrix:57[0 0]58[1 0]59Domain: Vector space of dimension 2 over Finite Field of size 360Codomain: Vector space of dimension 2 over Finite Field of size 361<BLANKLINE>62Vector space morphism represented by the matrix:63[0 0]64[0 1]65Domain: Vector space of dimension 2 over Finite Field of size 366Codomain: Vector space of dimension 2 over Finite Field of size 367<BLANKLINE>6869The zero and identity mappings are properties of the space.70The identity mapping will only be available if the domain and codomain71allow for endomorphisms (equal vector spaces with equal bases). ::7273sage: H = Hom(QQ^3, QQ^3)74sage: g = H.zero()75sage: g([1, 1/2, -3])76(0, 0, 0)77sage: f = H.identity()78sage: f([1, 1/2, -3])79(1, 1/2, -3)8081The homspace may be used with various representations of a82morphism in the space to create the morphism. We demonstrate83three ways to create the same linear transformation between84two two-dimensional subspaces of ``QQ^3``. The ``V.n`` notation85is a shortcut to the generators of each vector space, better86known as the basis elements. Note that the matrix representations87are relative to the bases, which are purposely fixed when the88subspaces are created ("user bases"). ::8990sage: U = QQ^391sage: V = U.subspace_with_basis([U.0+U.1, U.1-U.2])92sage: W = U.subspace_with_basis([U.0, U.1+U.2])93sage: H = Hom(V, W)9495First, with a matrix. Note that the matrix representation96acts by matrix multiplication with the vector on the left.97The input to the linear transformation, ``(3, 1, 2)``,98is converted to the coordinate vector ``(3, -2)``, then99matrix multiplication yields the vector ``(-3, -2)``,100which represents the vector ``(-3, -2, -2)`` in the codomain. ::101102sage: m = matrix(QQ, [[1, 2], [3, 4]])103sage: f1 = H(m)104sage: f1105Vector space morphism represented by the matrix:106[1 2]107[3 4]108Domain: Vector space of degree 3 and dimension 2 over Rational Field109User basis matrix:110[ 1 1 0]111[ 0 1 -1]112Codomain: Vector space of degree 3 and dimension 2 over Rational Field113User basis matrix:114[1 0 0]115[0 1 1]116sage: f1([3,1,2])117(-3, -2, -2)118119Second, with a list of images of the domain's basis elements. ::120121sage: img = [1*(U.0) + 2*(U.1+U.2), 3*U.0 + 4*(U.1+U.2)]122sage: f2 = H(img)123sage: f2124Vector space morphism represented by the matrix:125[1 2]126[3 4]127Domain: Vector space of degree 3 and dimension 2 over Rational Field128User basis matrix:129[ 1 1 0]130[ 0 1 -1]131Codomain: Vector space of degree 3 and dimension 2 over Rational Field132User basis matrix:133[1 0 0]134[0 1 1]135sage: f2([3,1,2])136(-3, -2, -2)137138Third, with a linear function taking the domain to the codomain. ::139140sage: g = lambda x: vector(QQ, [-2*x[0]+3*x[1], -2*x[0]+4*x[1], -2*x[0]+4*x[1]])141sage: f3 = H(g)142sage: f3143Vector space morphism represented by the matrix:144[1 2]145[3 4]146Domain: Vector space of degree 3 and dimension 2 over Rational Field147User basis matrix:148[ 1 1 0]149[ 0 1 -1]150Codomain: Vector space of degree 3 and dimension 2 over Rational Field151User basis matrix:152[1 0 0]153[0 1 1]154sage: f3([3,1,2])155(-3, -2, -2)156157The three linear transformations look the same, and are the same. ::158159sage: f1 == f2160True161sage: f2 == f3162True163164TESTS::165166sage: V = QQ^2167sage: W = QQ^3168sage: H = Hom(QQ^2, QQ^3)169sage: loads(dumps(H))170Set of Morphisms (Linear Transformations) from171Vector space of dimension 2 over Rational Field to172Vector space of dimension 3 over Rational Field173sage: loads(dumps(H)) == H174True175"""176177####################################################################################178# Copyright (C) 2011 Rob Beezer <[email protected]>179#180# Distributed under the terms of the GNU General Public License (GPL)181#182# This code is distributed in the hope that it will be useful,183# but WITHOUT ANY WARRANTY; without even the implied warranty of184# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU185# General Public License for more details.186#187# The full text of the GPL is available at:188#189# http://www.gnu.org/licenses/190####################################################################################191192import inspect193import sage.matrix.all as matrix194import sage.modules.free_module_homspace195import vector_space_morphism196197# This module initially overrides just the minimum functionality necessary198# 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 with201# examples that are actually vector spaces and not so many use "pure" modules202# for the examples.203204205def is_VectorSpaceHomspace(x):206r"""207Return ``True`` if ``x`` is a vector space homspace.208209INPUT:210211``x`` - anything212213EXAMPLES:214215To be a vector space morphism, the domain and codomain must both be216vector spaces, in other words, modules over fields. If either217set is just a module, then the ``Hom()`` constructor will build a218space of free module morphisms. ::219220sage: H = Hom(QQ^3, QQ^2)221sage: type(H)222<class 'sage.modules.vector_space_homspace.VectorSpaceHomspace_with_category'>223sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace(H)224True225226sage: K = Hom(QQ^3, ZZ^2)227sage: type(K)228<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'>229sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace(K)230False231232sage: L = Hom(ZZ^3, QQ^2)233sage: type(L)234<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'>235sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace(L)236False237238sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace('junk')239False240"""241return isinstance(x, VectorSpaceHomspace)242243class VectorSpaceHomspace(sage.modules.free_module_homspace.FreeModuleHomspace):244245def __call__(self, A, check=True):246r"""247INPUT:248249- ``A`` - one of several possible inputs representing250a morphism from this vector space homspace.251- a vector space morphism in this homspace252- a matrix representation relative to the bases of the vector spaces,253which acts on a vector placed to the left of the matrix254- a list or tuple containing images of the domain's basis vectors255- a function from the domain to the codomain256- ``check`` (default: True) - ``True`` or ``False``, required for257compatibility with calls from258:meth:`sage.structure.parent_gens.ParentWithGens.hom`.259260EXAMPLES::261262sage: V = (QQ^3).span_of_basis([[1,1,0],[1,0,2]])263sage: H = V.Hom(V)264sage: H265Set of Morphisms (Linear Transformations) from266Vector space of degree 3 and dimension 2 over Rational Field267User basis matrix:268[1 1 0]269[1 0 2]270to271Vector space of degree 3 and dimension 2 over Rational Field272User basis matrix:273[1 1 0]274[1 0 2]275276Coercing a matrix::277278sage: A = matrix(QQ, [[0, 1], [1, 0]])279sage: rho = H(A) # indirect doctest280sage: rho281Vector space morphism represented by the matrix:282[0 1]283[1 0]284Domain: Vector space of degree 3 and dimension 2 over Rational Field285User basis matrix:286[1 1 0]287[1 0 2]288Codomain: Vector space of degree 3 and dimension 2 over Rational Field289User basis matrix:290[1 1 0]291[1 0 2]292293Coercing a list of images::294295sage: phi = H([V.1, V.0])296sage: phi(V.1) == V.0297True298sage: phi(V.0) == V.1299True300sage: phi301Vector space morphism represented by the matrix:302[0 1]303[1 0]304Domain: Vector space of degree 3 and dimension 2 over Rational Field305User basis matrix:306[1 1 0]307[1 0 2]308Codomain: Vector space of degree 3 and dimension 2 over Rational Field309User basis matrix:310[1 1 0]311[1 0 2]312313Coercing a lambda function::314315sage: f = lambda x: vector(QQ, [x[0], (1/2)*x[2], 2*x[1]])316sage: zeta = H(f)317sage: zeta318Vector space morphism represented by the matrix:319[0 1]320[1 0]321Domain: Vector space of degree 3 and dimension 2 over Rational Field322User basis matrix:323[1 1 0]324[1 0 2]325Codomain: Vector space of degree 3 and dimension 2 over Rational Field326User basis matrix:327[1 1 0]328[1 0 2]329330Coercing a vector space morphism into the parent of a second vector331space morphism will unify their parents. ::332333sage: U = QQ^3334sage: V = QQ^4335sage: W = QQ^3336sage: X = QQ^4337sage: H = Hom(U, V)338sage: K = Hom(W, X)339340sage: A = matrix(QQ, 3, 4, [0]*12)341sage: f = H(A)342sage: B = matrix(QQ, 3, 4, range(12))343sage: g = K(B)344sage: f.parent() is g.parent()345False346347sage: h = H(g)348sage: f.parent() is h.parent()349True350351See other examples in the module-level documentation.352353TESTS::354355sage: V = GF(3)^0356sage: W = GF(3)^1357sage: H = V.Hom(W)358sage: H.zero_element().is_zero()359True360361Previously the above code resulted in a TypeError because the362dimensions of the matrix were incorrect.363"""364import vector_space_morphism365D = self.domain()366C = self.codomain()367if matrix.is_Matrix(A):368pass369elif vector_space_morphism.is_VectorSpaceMorphism(A):370A = A.matrix()371elif inspect.isfunction(A):372try:373images = [A(g) for g in D.basis()]374except (ValueError, TypeError, IndexError), e:375msg = 'function cannot be applied properly to some basis element because\n' + e.args[0]376raise ValueError(msg)377try:378A = matrix.matrix(D.dimension(), C.dimension(), [C.coordinates(C(a)) for a in images])379except (ArithmeticError, TypeError), e:380msg = 'some image of the function is not in the codomain, because\n' + e.args[0]381raise ArithmeticError(msg)382elif isinstance(A, (list, tuple)):383if len(A) != len(D.basis()):384msg = "number of images should equal the size of the domain's basis (={0}), not {1}"385raise ValueError(msg.format(len(D.basis()), len(A)))386try:387v = [C(a) for a in A]388A = matrix.matrix(D.dimension(), C.dimension(), [C.coordinates(a) for a in v])389except (ArithmeticError, TypeError), e:390msg = 'some proposed image is not in the codomain, because\n' + e.args[0]391raise ArithmeticError(msg)392else:393msg = 'vector space homspace can only coerce matrices, vector space morphisms, functions or lists, not {0}'394raise TypeError(msg.format(A))395return vector_space_morphism.VectorSpaceMorphism(self, A)396397def _repr_(self):398r"""399Text representation of a space of vector space morphisms.400401EXAMPLE::402403sage: H = Hom(QQ^2, QQ^3)404sage: H._repr_().split(' ')405['Set', 'of', 'Morphisms', '(Linear', 'Transformations)',406'from', 'Vector', 'space', 'of', 'dimension', '2', 'over',407'Rational', 'Field', 'to', 'Vector', 'space', 'of',408'dimension', '3', 'over', 'Rational', 'Field']409"""410msg = 'Set of Morphisms (Linear Transformations) from {0} to {1}'411return msg.format(self.domain(), self.codomain())412413