Homsets¶
The class Hom
is the base class used to represent sets of morphisms
between objects of a given category.
Hom
objects are usually “weakly” cached upon creation so that they
don’t have to be generated over and over but can be garbage collected together
with the corresponding objects when these are not strongly ref’ed anymore.
EXAMPLES:
In the following, the Hom
object is indeed cached:
sage: K = GF(17)
sage: H = Hom(ZZ, K)
sage: H
Set of Homomorphisms from Integer Ring to Finite Field of size 17
sage: H is Hom(ZZ, K)
True
Nonetheless, garbage collection occurs when the original references are overwritten:
sage: for p in prime_range(200):
....: K = GF(p)
....: H = Hom(ZZ, K)
sage: import gc
sage: _ = gc.collect()
sage: from sage.rings.finite_rings.finite_field_prime_modn import FiniteField_prime_modn as FF
sage: L = [x for x in gc.get_objects() if isinstance(x, FF)]
sage: len(L)
1
sage: L
[Finite Field of size 199]
AUTHORS:
 David Kohel and William Stein
 David Joyner (20051217): added examples
 William Stein (20060114): Changed from Homspace to Homset.
 Nicolas M. Thiery (200812): Updated for the new category framework
 Simon King (201112): Use a weak cache for homsets
 Simon King (201302): added examples

sage.categories.homset.
End
(X, category=None)¶ Create the set of endomorphisms of
X
in the category category.INPUT:
X
– anythingcategory
– (optional) category in which to coerceX
OUTPUT:
A set of endomorphisms in category
EXAMPLES:
sage: V = VectorSpace(QQ, 3) sage: End(V) Set of Morphisms (Linear Transformations) from Vector space of dimension 3 over Rational Field to Vector space of dimension 3 over Rational Field
sage: G = AlternatingGroup(3) sage: S = End(G); S Set of Morphisms from Alternating group of order 3!/2 as a permutation group to Alternating group of order 3!/2 as a permutation group in Category of finite enumerated permutation groups sage: from sage.categories.homset import is_Endset sage: is_Endset(S) True sage: S.domain() Alternating group of order 3!/2 as a permutation group
To avoid creating superfluous categories, a homset in a category
Cs()
is in the homset category of the lowest full super categoryBs()
ofCs()
that implementsBs.Homsets
(or the join thereof if there are several). For example, finite groups form a full subcategory of unital magmas: any unital magma morphism between two finite groups is a finite group morphism. Since finite groups currently implement nothing more than unital magmas about their homsets, we have:sage: G = GL(3,3) sage: G.category() Category of finite groups sage: H = Hom(G,G) sage: H.homset_category() Category of finite groups sage: H.category() Category of endsets of unital magmas
Similarly, a ring morphism just needs to preserve addition, multiplication, zero, and one. Accordingly, and since the category of rings implements nothing specific about its homsets, a ring homset is currently constructed in the category of homsets of unital magmas and unital additive magmas:
sage: H = Hom(ZZ,ZZ,Rings()) sage: H.category() Category of endsets of unital magmas and additive unital additive magmas

sage.categories.homset.
Hom
(X, Y, category=None, check=True)¶ Create the space of homomorphisms from X to Y in the category
category
.INPUT:
X
– an object of a categoryY
– an object of a categorycategory
– a category in which the morphisms must be. (default: the meet of the categories ofX
andY
) BothX
andY
must belong to that category.check
– a boolean (default:True
): whether to check the input, and in particular thatX
andY
belong tocategory
.
OUTPUT: a homset in category
EXAMPLES:
sage: V = VectorSpace(QQ,3) sage: Hom(V, V) Set of Morphisms (Linear Transformations) from Vector space of dimension 3 over Rational Field to Vector space of dimension 3 over Rational Field sage: G = AlternatingGroup(3) sage: Hom(G, G) Set of Morphisms from Alternating group of order 3!/2 as a permutation group to Alternating group of order 3!/2 as a permutation group in Category of finite enumerated permutation groups sage: Hom(ZZ, QQ, Sets()) Set of Morphisms from Integer Ring to Rational Field in Category of sets sage: Hom(FreeModule(ZZ,1), FreeModule(QQ,1)) Set of Morphisms from Ambient free module of rank 1 over the principal ideal domain Integer Ring to Vector space of dimension 1 over Rational Field in Category of commutative additive groups sage: Hom(FreeModule(QQ,1), FreeModule(ZZ,1)) Set of Morphisms from Vector space of dimension 1 over Rational Field to Ambient free module of rank 1 over the principal ideal domain Integer Ring in Category of commutative additive groups
Here, we test against a memory leak that has been fixed at trac ticket #11521 by using a weak cache:
sage: for p in prime_range(10^3): ....: K = GF(p) ....: a = K(0) sage: import gc sage: gc.collect() # random 624 sage: from sage.rings.finite_rings.finite_field_prime_modn import FiniteField_prime_modn as FF sage: L = [x for x in gc.get_objects() if isinstance(x, FF)] sage: len(L), L[0] (1, Finite Field of size 997)
To illustrate the choice of the category, we consider the following parents as running examples:
sage: X = ZZ; X Integer Ring sage: Y = SymmetricGroup(3); Y Symmetric group of order 3! as a permutation group
By default, the smallest category containing both
X
andY
, is used:sage: Hom(X, Y) Set of Morphisms from Integer Ring to Symmetric group of order 3! as a permutation group in Category of enumerated monoids
Otherwise, if
category
is specified, thencategory
is used, after checking thatX
andY
are indeed incategory
:sage: Hom(X, Y, Magmas()) Set of Morphisms from Integer Ring to Symmetric group of order 3! as a permutation group in Category of magmas sage: Hom(X, Y, Groups()) Traceback (most recent call last): ... ValueError: Integer Ring is not in Category of groups
A parent (or a parent class of a category) may specify how to construct certain homsets by implementing a method
_Hom_(self, codomain, category)
. This method should either construct the requested homset or raise aTypeError
. This hook is currently mostly used to create homsets in some specific subclass ofHomset
(e.g.sage.rings.homset.RingHomset
):sage: Hom(QQ,QQ).__class__ <class 'sage.rings.homset.RingHomset_generic_with_category'>
Do not call this hook directly to create homsets, as it does not handle unique representation:
sage: Hom(QQ,QQ) == QQ._Hom_(QQ, category=QQ.category()) True sage: Hom(QQ,QQ) is QQ._Hom_(QQ, category=QQ.category()) False
Todo
 Design decision: how much of the homset comes from the
category of
X
andY
, and how much from the specificX
andY
. In particular, do we need several parent classes depending onX
andY
, or does the difference only lie in the elements (i.e. the morphism), and of course how the parent calls their constructors.  Specify the protocol for the
_Hom_
hook in case of ambiguity (e.g. if both a parent and some category thereof provide one).

class
sage.categories.homset.
Homset
(X, Y, category=None, base=None, check=True)¶ Bases:
sage.structure.parent.Set_generic
The class for collections of morphisms in a category.
EXAMPLES:
sage: H = Hom(QQ^2, QQ^3) sage: loads(H.dumps()) is H True
Homsets of unique parents are unique as well:
sage: H = End(AffineSpace(2, names='x,y')) sage: loads(dumps(AffineSpace(2, names='x,y'))) is AffineSpace(2, names='x,y') True sage: loads(dumps(H)) is H True
Conversely, homsets of nonunique parents are nonunique:
sage: H = End(ProjectiveSpace(2, names=’x,y,z’)) sage: loads(dumps(ProjectiveSpace(2, names=’x,y,z’))) is ProjectiveSpace(2, names=’x,y,z’) False sage: loads(dumps(ProjectiveSpace(2, names=’x,y,z’))) == ProjectiveSpace(2, names=’x,y,z’) True sage: loads(dumps(H)) is H False sage: loads(dumps(H)) == H True
codomain
()¶ Return the codomain of this homset.
EXAMPLES:
sage: P.<t> = ZZ[] sage: f = P.hom([1/2*t]) sage: f.parent().codomain() Univariate Polynomial Ring in t over Rational Field sage: f.codomain() is f.parent().codomain() True

domain
()¶ Return the domain of this homset.
EXAMPLES:
sage: P.<t> = ZZ[] sage: f = P.hom([1/2*t]) sage: f.parent().domain() Univariate Polynomial Ring in t over Integer Ring sage: f.domain() is f.parent().domain() True

element_class_set_morphism
()¶ A base class for elements of this homset which are also
SetMorphism
, i.e. implemented by mean of a Python function.This is currently plain
SetMorphism
, without inheritance from categories.Todo
Refactor during the upcoming homset cleanup.
EXAMPLES:
sage: H = Hom(ZZ, ZZ) sage: H.element_class_set_morphism <type 'sage.categories.morphism.SetMorphism'>

homset_category
()¶ Return the category that this is a Hom in, i.e., this is typically the category of the domain or codomain object.
EXAMPLES:
sage: H = Hom(AlternatingGroup(4), AlternatingGroup(7)) sage: H.homset_category() Category of finite enumerated permutation groups

identity
()¶ The identity map of this homset.
Note
Of course, this only exists for sets of endomorphisms.
EXAMPLES:
sage: H = Hom(QQ,QQ) sage: H.identity() Identity endomorphism of Rational Field sage: H = Hom(ZZ,QQ) sage: H.identity() Traceback (most recent call last): ... TypeError: Identity map only defined for endomorphisms. Try natural_map() instead. sage: H.natural_map() Natural morphism: From: Integer Ring To: Rational Field

natural_map
()¶ Return the “natural map” of this homset.
Note
By default, a formal coercion morphism is returned.
EXAMPLES:
sage: H = Hom(ZZ['t'],QQ['t'], CommutativeAdditiveGroups()) sage: H.natural_map() Coercion morphism: From: Univariate Polynomial Ring in t over Integer Ring To: Univariate Polynomial Ring in t over Rational Field sage: H = Hom(QQ['t'],GF(3)['t']) sage: H.natural_map() Traceback (most recent call last): ... TypeError: natural coercion morphism from Univariate Polynomial Ring in t over Rational Field to Univariate Polynomial Ring in t over Finite Field of size 3 not defined

one
()¶ The identity map of this homset.
Note
Of course, this only exists for sets of endomorphisms.
EXAMPLES:
sage: K = GaussianIntegers() sage: End(K).one() Identity endomorphism of Gaussian Integers in Number Field in I with defining polynomial x^2 + 1

reversed
()¶ Return the corresponding homset, but with the domain and codomain reversed.
EXAMPLES:
sage: H = Hom(ZZ^2, ZZ^3); H Set of Morphisms from Ambient free module of rank 2 over the principal ideal domain Integer Ring to Ambient free module of rank 3 over the principal ideal domain Integer Ring in Category of finite dimensional modules with basis over (euclidean domains and infinite enumerated sets and metric spaces) sage: type(H) <class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'> sage: H.reversed() Set of Morphisms from Ambient free module of rank 3 over the principal ideal domain Integer Ring to Ambient free module of rank 2 over the principal ideal domain Integer Ring in Category of finite dimensional modules with basis over (euclidean domains and infinite enumerated sets and metric spaces) sage: type(H.reversed()) <class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'>


class
sage.categories.homset.
HomsetWithBase
(X, Y, category=None, check=True, base=None)¶

sage.categories.homset.
end
(X, f)¶ Return
End(X)(f)
, wheref
is data that defines an element ofEnd(X)
.EXAMPLES:
sage: R.<x> = QQ[] sage: phi = end(R, [x + 1]) sage: phi Ring endomorphism of Univariate Polynomial Ring in x over Rational Field Defn: x > x + 1 sage: phi(x^2 + 5) x^2 + 2*x + 6

sage.categories.homset.
hom
(X, Y, f)¶ Return
Hom(X,Y)(f)
, wheref
is data that defines an element ofHom(X,Y)
.EXAMPLES:
sage: phi = hom(QQ['x'], QQ, [2]) sage: phi(x^2 + 3) 7

sage.categories.homset.
is_Endset
(x)¶ Return
True
ifx
is a set of endomorphisms in a category.EXAMPLES:
sage: from sage.categories.homset import is_Endset sage: P.<t> = ZZ[] sage: f = P.hom([1/2*t]) sage: is_Endset(f.parent()) False sage: g = P.hom([2*t]) sage: is_Endset(g.parent()) True

sage.categories.homset.
is_Homset
(x)¶ Return
True
ifx
is a set of homomorphisms in a category.EXAMPLES:
sage: from sage.categories.homset import is_Homset sage: P.<t> = ZZ[] sage: f = P.hom([1/2*t]) sage: is_Homset(f) False sage: is_Homset(f.category()) False sage: is_Homset(f.parent()) True