Base class for objects of a category


Many category objects in Sage are equipped with generators, which are usually special elements of the object. For example, the polynomial ring \(\ZZ[x,y,z]\) is generated by \(x\), \(y\), and \(z\). In Sage the i th generator of an object X is obtained using the notation X.gen(i). From the Sage interactive prompt, the shorthand notation X.i is also allowed.

The following examples illustrate these functions in the context of multivariate polynomial rings and free modules.


sage: R = PolynomialRing(ZZ, 3, 'x')
sage: R.ngens()
sage: R.gen(0)
sage: R.gens()
(x0, x1, x2)
sage: R.variable_names()
('x0', 'x1', 'x2')

This example illustrates generators for a free module over \(\ZZ\).

sage: M = FreeModule(ZZ, 4)
sage: M
Ambient free module of rank 4 over the principal ideal domain Integer Ring
sage: M.ngens()
sage: M.gen(0)
(1, 0, 0, 0)
sage: M.gens()
((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1))
class sage.structure.category_object.CategoryObject

Bases: sage.structure.sage_object.SageObject

An object in some category.

Hom(codomain, cat=None)

Return the homspace Hom(self, codomain, cat) of all homomorphisms from self to codomain in the category cat. The default category is determined by self.category() and codomain.category().


sage: R.<x,y> = PolynomialRing(QQ, 2)
sage: R.Hom(QQ)
Set of Homomorphisms from Multivariate Polynomial Ring in x, y over Rational Field to Rational Field

Homspaces are defined for very general Sage objects, even elements of familiar rings.

sage: n = 5; Hom(n,7)
Set of Morphisms from 5 to 7 in Category of elements of Integer Ring
sage: z=(2/3); Hom(z,8/1)
Set of Morphisms from 2/3 to 8 in Category of elements of Rational Field

This example illustrates the optional third argument:

sage: QQ.Hom(ZZ, Sets())
Set of Morphisms from Rational Field to Integer Ring in Category of sets

Return the base ring of self.


  • self – an object over a base ring; typically a module


sage: from sage.modules.module import Module
sage: Module(ZZ).base_ring()
Integer Ring

sage: F = FreeModule(ZZ,3)
sage: F.base_ring()
Integer Ring
sage: F.__class__.base_ring
<method 'base_ring' of 'sage.structure.category_object.CategoryObject' objects>

sage: F = FreeAlgebra(QQ, 'x')
sage: F.base_ring()
Rational Field
sage: F.__class__.base_ring
<method 'base_ring' of 'sage.structure.category_object.CategoryObject' objects>

sage: E = CombinatorialFreeModule(ZZ, [1,2,3])
sage: F = CombinatorialFreeModule(ZZ, [2,3,4])
sage: H = Hom(E, F)
sage: H.base_ring()
Integer Ring
sage: H.__class__.base_ring
<method 'base_ring' of 'sage.structure.category_object.CategoryObject' objects>


Move this method elsewhere (typically in the Modules category) so as not to pollute the namespace of all category objects.


Return the categories of self.


sage: ZZ.categories()
[Join of Category of euclidean domains
     and Category of infinite enumerated sets,
 Category of euclidean domains,
 Category of principal ideal domains,
 Category of unique factorization domains,
 Category of gcd domains,
 Category of integral domains,
 Category of domains,
 Category of commutative rings, ...
 Category of monoids, ...,
 Category of commutative additive groups, ...,
 Category of sets, ...,
 Category of objects]

Return a dictionary whose entries are {var_name:variable,...}.


Return the dictionary of generators of self and its base rings.


  • a dictionary with string names of generators as keys and generators of self and its base rings as values.


sage: R = QQ['x,y']['z,w']
sage: sorted(R.gens_dict_recursive().items())
[('w', w), ('x', x), ('y', y), ('z', z)]
inject_variables(scope=None, verbose=True)

Inject the generators of self with their names into the namespace of the Python code from which this function is called. Thus, e.g., if the generators of self are labeled ‘a’, ‘b’, and ‘c’, then after calling this method the variables a, b, and c in the current scope will be set equal to the generators of self.

NOTE: If Foo is a constructor for a Sage object with generators, and Foo is defined in Cython, then it would typically call inject_variables() on the object it creates. E.g., PolynomialRing(QQ, 'y') does this so that the variable y is the generator of the polynomial ring.

injvar(scope=None, verbose=True)

This is a deprecated synonym for inject_variables().


Returns the list of variable names suitable for latex output.

All _SOMETHING substrings are replaced by _{SOMETHING} recursively so that subscripts of subscripts work.


sage: R, x = PolynomialRing(QQ,'x',12).objgens()
sage: x
(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11)
sage: print R.latex_variable_names ()
['x_{0}', 'x_{1}', 'x_{2}', 'x_{3}', 'x_{4}', 'x_{5}', 'x_{6}', 'x_{7}', 'x_{8}', 'x_{9}', 'x_{10}', 'x_{11}']
sage: f = x[0]^3 + 15/3 * x[1]^10
sage: print latex(f)
5 x_{1}^{10} + x_{0}^{3}
normalize_names(ngens, names=None)

Return the tuple (self, self.gen()).


sage: R, x = PolynomialRing(QQ,'x').objgen()
sage: R
Univariate Polynomial Ring in x over Rational Field
sage: x

Return the tuple (self, self.gens()).


sage: R = PolynomialRing(QQ, 3, 'x'); R
Multivariate Polynomial Ring in x0, x1, x2 over Rational Field
sage: R.objgens()
(Multivariate Polynomial Ring in x0, x1, x2 over Rational Field, (x0, x1, x2))
sage.structure.category_object.check_default_category(default_category, category)
class sage.structure.category_object.localvars(obj, names, latex_names=None, normalize=True)

Context manager for safely temporarily changing the variables names of an object with generators.

Objects with named generators are globally unique in Sage. Sometimes, though, it is very useful to be able to temporarily display the generators differently. The new Python with statement and the localvars context manager make this easy and safe (and fun!)

Suppose X is any object with generators. Write

with localvars(X, names[, latex_names] [,normalize=False]):
    some code

and the indented code will be run as if the names in X are changed to the new names. If you give normalize=True, then the names are assumed to be a tuple of the correct number of strings.


sage: R.<x,y> = PolynomialRing(QQ,2)
sage: with localvars(R, 'z,w'):
...       print x^3 + y^3 - x*y
z^3 + w^3 - z*w

NOTES: I wrote this because it was needed to print elements of the quotient of a ring \(R\) by an ideal \(I\) using the print function for elements of \(R\). See the code in sage.rings.quotient_ring_element.

AUTHOR: William Stein (2006-10-31)

Previous topic

Abstract base class for Sage objects

Next topic

Base class for old-style parent objects

This Page