# Base class for objects of a category¶

Base class for objects of a category

CLASS HIERARCHY:

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.

EXAMPLES:

sage: R = PolynomialRing(ZZ, 3, 'x')
sage: R.ngens()
3
sage: R.gen(0)
x0
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()
4
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

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().

EXAMPLES:

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

base()
base_ring()
categories()

EXAMPLES:

sage: ZZ.categories()
[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 rings,
Category of rngs,
Category of semirings,
Category of monoids,
Category of semigroups,
Category of magmas,
Category of sets,
Category of sets with partial maps,
Category of objects]

category()
gens_dict()

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

gens_dict_recursive()

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

OUTPUT:

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

EXAMPLES:

sage: R = QQ['x,y']['z,w']
sage: sorted(R.gens_dict_recursive().items())
[('w', w), ('x', x), ('y', y), ('z', z)]

has_base(category=None)
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().

latex_name()
latex_variable_names()

Returns the list of variable names suitable for latex output.

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

EXAMPLES:

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)
objgen()

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

EXAMPLES:

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

objgens()

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

EXAMPLES:

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))

variable_name()
variable_names()
sage.structure.category_object.check_default_category(default_category, category)
sage.structure.category_object.guess_category(obj)
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.

EXAMPLES:

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