Path: blob/master/src/sage/homology/simplicial_complex_homset.py
8818 views
r"""1Homsets between simplicial complexes23AUTHORS:45- Travis Scrimshaw (2012-08-18): Made all simplicial complexes immutable to6work with the homset cache.78EXAMPLES:910::1112sage: S = simplicial_complexes.Sphere(1)13sage: T = simplicial_complexes.Sphere(2)14sage: H = Hom(S,T)15sage: f = {0:0,1:1,2:3}16sage: x = H(f)17sage: x18Simplicial complex morphism {0: 0, 1: 1, 2: 3} from Simplicial complex with vertex set (0, 1, 2) and facets {(1, 2), (0, 2), (0, 1)} to Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (1, 2, 3), (0, 1, 3)}19sage: x.is_injective()20True21sage: x.is_surjective()22False23sage: x.image()24Simplicial complex with vertex set (0, 1, 3) and facets {(1, 3), (0, 3), (0, 1)}25sage: from sage.homology.simplicial_complex import Simplex26sage: s = Simplex([1,2])27sage: x(s)28(1, 3)2930TESTS::3132sage: S = simplicial_complexes.Sphere(1)33sage: T = simplicial_complexes.Sphere(2)34sage: H = Hom(S,T)35sage: loads(dumps(H))==H36True3738"""3940#*****************************************************************************41# Copyright (C) 2009 D. Benjamin Antieau <[email protected]>42#43# Distributed under the terms of the GNU General Public License (GPL)44#45# This code is distributed in the hope that it will be useful,46# but WITHOUT ANY WARRANTY; without even the implied warranty47# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.48#49# See the GNU General Public License for more details; the full text50# is available at:51#52# http://www.gnu.org/licenses/53#54#*****************************************************************************5556import sage.categories.homset57import sage.homology.simplicial_complex_morphism as simplicial_complex_morphism5859def is_SimplicialComplexHomset(x):60"""61Return ``True`` if and only if ``x`` is a simplicial complex homspace.6263EXAMPLES::6465sage: S = SimplicialComplex(is_mutable=False)66sage: T = SimplicialComplex(is_mutable=False)67sage: H = Hom(S, T)68sage: H69Set of Morphisms from Simplicial complex with vertex set () and facets {()} to Simplicial complex with vertex set () and facets {()} in Category of simplicial complexes70sage: from sage.homology.simplicial_complex_homset import is_SimplicialComplexHomset71sage: is_SimplicialComplexHomset(H)72True73"""74return isinstance(x, SimplicialComplexHomset)7576class SimplicialComplexHomset(sage.categories.homset.Homset):77def __call__(self, f):78"""79INPUT:8081- ``f`` -- a dictionary with keys exactly the vertices of the domain82and values vertices of the codomain8384EXAMPLES::8586sage: S = simplicial_complexes.Sphere(3)87sage: T = simplicial_complexes.Sphere(2)88sage: f = {0:0,1:1,2:2,3:2,4:2}89sage: H = Hom(S,T)90sage: x = H(f)91sage: x92Simplicial complex morphism {0: 0, 1: 1, 2: 2, 3: 2, 4: 2} from Simplicial complex with vertex set (0, 1, 2, 3, 4) and 5 facets to Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (1, 2, 3), (0, 1, 3)}93"""94return simplicial_complex_morphism.SimplicialComplexMorphism(f,self.domain(),self.codomain())9596def diagonal_morphism(self,rename_vertices=True):97r"""98Returns the diagonal morphism in `Hom(S, S \times S)`.99100EXAMPLES::101102sage: S = simplicial_complexes.Sphere(2)103sage: H = Hom(S,S.product(S, is_mutable=False))104sage: d = H.diagonal_morphism()105sage: d106Simplicial complex morphism {0: 'L0R0', 1: 'L1R1', 2: 'L2R2', 3: 'L3R3'} from107Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (1, 2, 3), (0, 1, 3)}108to Simplicial complex with 16 vertices and 96 facets109110sage: T = SimplicialComplex([[0], [1]], is_mutable=False)111sage: U = T.product(T,rename_vertices = False, is_mutable=False)112sage: G = Hom(T,U)113sage: e = G.diagonal_morphism(rename_vertices = False)114sage: e115Simplicial complex morphism {0: (0, 0), 1: (1, 1)} from116Simplicial complex with vertex set (0, 1) and facets {(0,), (1,)}117to Simplicial complex with 4 vertices and facets {((1, 1),), ((1, 0),), ((0, 0),), ((0, 1),)}118"""119120if self._codomain == self._domain.product(self._domain,rename_vertices=rename_vertices):121X = self._domain.product(self._domain,rename_vertices=rename_vertices)122f = dict()123if rename_vertices:124for i in self._domain.vertices().set():125f[i] = "L"+str(i)+"R"+str(i)126else:127for i in self._domain.vertices().set():128f[i] = (i,i)129return simplicial_complex_morphism.SimplicialComplexMorphism(f, self._domain,X)130else:131raise TypeError, "Diagonal morphism is only defined for Hom(X,XxX)."132133def identity(self):134"""135Returns the identity morphism of `Hom(S,S)`.136137EXAMPLES::138139sage: S = simplicial_complexes.Sphere(2)140sage: H = Hom(S,S)141sage: i = H.identity()142sage: i.is_identity()143True144145sage: T = SimplicialComplex([[0,1]], is_mutable=False)146sage: G = Hom(T,T)147sage: G.identity()148Simplicial complex morphism {0: 0, 1: 1} from149Simplicial complex with vertex set (0, 1) and facets {(0, 1)} to150Simplicial complex with vertex set (0, 1) and facets {(0, 1)}151"""152if self.is_endomorphism_set():153f = dict()154for i in self._domain.vertices().set():155f[i]=i156return simplicial_complex_morphism.SimplicialComplexMorphism(f,self._domain,self._codomain)157else:158raise TypeError("Identity map is only defined for endomorphism sets.")159160def an_element(self):161"""162Returns a (non-random) element of ``self``.163164EXAMPLES::165166sage: S = simplicial_complexes.KleinBottle()167sage: T = simplicial_complexes.Sphere(5)168sage: H = Hom(S,T)169sage: x = H.an_element()170sage: x171Simplicial complex morphism {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0} from Simplicial complex with vertex set (0, 1, 2, 3, 4, 5, 6, 7) and 16 facets to Simplicial complex with vertex set (0, 1, 2, 3, 4, 5, 6) and 7 facets172"""173X_vertices = self._domain.vertices().set()174try:175i = self._codomain.vertices().set().__iter__().next()176except StopIteration:177if len(X_vertices) == 0:178return dict()179else:180raise TypeError, "There are no morphisms from a non-empty simplicial complex to an empty simplicial comples."181f = dict()182for x in X_vertices:183f[x]=i184return simplicial_complex_morphism.SimplicialComplexMorphism(f,self._domain,self._codomain)185186187