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

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
__getstate__()
__init__()
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
static __new__()
T.__new__(S, ...) -> a new object with type S, a subtype of T
__setstate__()
__temporarily_change_names()
This is used by the variable names context manager.
_assign_names()

Set the names of the generator of this object.

This can only be done once because objects with generators are immutable, and is typically done during creation of the object.

EXAMPLES: When we create this polynomial ring, self._assign_names is called by the constructor:

sage: R = QQ['x,y,abc']; R
Multivariate Polynomial Ring in x, y, abc over Rational Field
sage: R.2
abc

We can’t rename the variables:

sage: R._assign_names(['a','b','c'])
...
ValueError: variable names cannot be changed after object creation.        
_base
_cdata
_certify_names()
_factory_data
_first_ngens()
Used by the preparser for R.<x> = ...
_names
_ngens_()
_populate_generators_()
_temporarily_change_names()
base()
base_ring()
category()
gens_dict()
Return a dictionary whose entries are {var_name:variable,...}.
has_base()
inject_variables()

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()
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()
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.guess_category()
class sage.structure.category_object.localvars

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.

If you’re writing Python library code, you currently have to put from __future__ import with_statement in your file in order to use the with statement. This restriction will disappear in Python 2.6.

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)

__enter__()
__exit__()
__init__()

Previous topic

Abstract base class for Sage objects

Next topic

Base class for parent objects with generators.

This Page