Path: blob/master/src/sage/modules/vector_space_homspace.py
8815 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_homspace195196# This module initially overrides just the minimum functionality necessary197# from sage.modules.free_module_homspace.FreeModuleHomSpace.198# If additional methods here override the free module homspace methods,199# consider adjusting the free module doctests, since many are written with200# examples that are actually vector spaces and not so many use "pure" modules201# for the examples.202203204def is_VectorSpaceHomspace(x):205r"""206Return ``True`` if ``x`` is a vector space homspace.207208INPUT:209210``x`` - anything211212EXAMPLES:213214To be a vector space morphism, the domain and codomain must both be215vector spaces, in other words, modules over fields. If either216set is just a module, then the ``Hom()`` constructor will build a217space of free module morphisms. ::218219sage: H = Hom(QQ^3, QQ^2)220sage: type(H)221<class 'sage.modules.vector_space_homspace.VectorSpaceHomspace_with_category'>222sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace(H)223True224225sage: K = Hom(QQ^3, ZZ^2)226sage: type(K)227<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'>228sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace(K)229False230231sage: L = Hom(ZZ^3, QQ^2)232sage: type(L)233<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'>234sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace(L)235False236237sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace('junk')238False239"""240return isinstance(x, VectorSpaceHomspace)241242class VectorSpaceHomspace(sage.modules.free_module_homspace.FreeModuleHomspace):243244def __call__(self, A, check=True):245r"""246INPUT:247248- ``A`` - one of several possible inputs representing249a morphism from this vector space homspace.250- a vector space morphism in this homspace251- a matrix representation relative to the bases of the vector spaces,252which acts on a vector placed to the left of the matrix253- a list or tuple containing images of the domain's basis vectors254- a function from the domain to the codomain255- ``check`` (default: True) - ``True`` or ``False``, required for256compatibility with calls from257:meth:`sage.structure.parent_gens.ParentWithGens.hom`.258259EXAMPLES::260261sage: V = (QQ^3).span_of_basis([[1,1,0],[1,0,2]])262sage: H = V.Hom(V)263sage: H264Set of Morphisms (Linear Transformations) from265Vector space of degree 3 and dimension 2 over Rational Field266User basis matrix:267[1 1 0]268[1 0 2]269to270Vector space of degree 3 and dimension 2 over Rational Field271User basis matrix:272[1 1 0]273[1 0 2]274275Coercing a matrix::276277sage: A = matrix(QQ, [[0, 1], [1, 0]])278sage: rho = H(A) # indirect doctest279sage: rho280Vector space morphism represented by the matrix:281[0 1]282[1 0]283Domain: Vector space of degree 3 and dimension 2 over Rational Field284User basis matrix:285[1 1 0]286[1 0 2]287Codomain: Vector space of degree 3 and dimension 2 over Rational Field288User basis matrix:289[1 1 0]290[1 0 2]291292Coercing a list of images::293294sage: phi = H([V.1, V.0])295sage: phi(V.1) == V.0296True297sage: phi(V.0) == V.1298True299sage: phi300Vector space morphism represented by the matrix:301[0 1]302[1 0]303Domain: Vector space of degree 3 and dimension 2 over Rational Field304User basis matrix:305[1 1 0]306[1 0 2]307Codomain: Vector space of degree 3 and dimension 2 over Rational Field308User basis matrix:309[1 1 0]310[1 0 2]311312Coercing a lambda function::313314sage: f = lambda x: vector(QQ, [x[0], (1/2)*x[2], 2*x[1]])315sage: zeta = H(f)316sage: zeta317Vector space morphism represented by the matrix:318[0 1]319[1 0]320Domain: Vector space of degree 3 and dimension 2 over Rational Field321User basis matrix:322[1 1 0]323[1 0 2]324Codomain: Vector space of degree 3 and dimension 2 over Rational Field325User basis matrix:326[1 1 0]327[1 0 2]328329Coercing a vector space morphism into the parent of a second vector330space morphism will unify their parents::331332sage: U = FreeModule(QQ,3, sparse=True ); V = QQ^4333sage: W = FreeModule(QQ,3, sparse=False); X = QQ^4334sage: H = Hom(U, V)335sage: K = Hom(W, X)336sage: H is K, H == K337(False, True)338339sage: A = matrix(QQ, 3, 4, [0]*12)340sage: f = H(A)341sage: B = matrix(QQ, 3, 4, range(12))342sage: g = K(B)343sage: f.parent() is H and g.parent() is K344True345346sage: h = H(g)347sage: f.parent() is h.parent()348True349350See other examples in the module-level documentation.351352TESTS::353354sage: V = GF(3)^0355sage: W = GF(3)^1356sage: H = V.Hom(W)357sage: H.zero_element().is_zero()358True359360Previously the above code resulted in a TypeError because the361dimensions of the matrix were incorrect.362"""363from vector_space_morphism import is_VectorSpaceMorphism, VectorSpaceMorphism364D = self.domain()365C = self.codomain()366from sage.matrix.matrix import is_Matrix367if is_Matrix(A):368pass369elif 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 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())412413414