CLASS HIERARCHY:
SageObject
CategoryObject
Parent
TESTS: This came up in some subtle bug once.
sage: gp(2) + gap(3)
5
Parents are the Sage/mathematical analogues of container objects in computer science.
Return the homspace Hom(self, codomain, cat) of all homomorphisms from self to codomain in the category cat. The default category is 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
This is the generic call method for all parents.
When called, it will find a map based on the Parent (or type) of x. If a coercion exists, it will always be chosen. This map will then be called (with the arguments and keywords if any).
By default this will dispatch as quickly as possible to _element_constructor_() though faster pathways are possible if so desired.
TESTS:
We check that the invariant:
self._element_init_pass_parent == guess_pass_parent(self, self._element_constructor)
is preserved (see #5979):
sage: class MyParent(Parent):
... def _element_constructor_(self, x):
... print self, x
... return sage.structure.element.Element(parent = self)
... def _repr_(self):
... return "my_parent"
...
sage: my_parent = MyParent()
sage: x = my_parent("bla")
my_parent bla
sage: x.parent() # indirect doctest
my_parent
sage: x = my_parent() # shouldn't this one raise an error?
my_parent 0
sage: x = my_parent(3) # todo: not implemented why does this one fail???
my_parent 3
Returns the item of self, by getting self as a list.
EXAMPLES:
sage: MatrixSpace(GF(3), 2, 2)[9]
[0 2]
[0 0]
sage: MatrixSpace(GF(3), 2, 2)[0]
[0 0]
[0 0]
x.__getslice__(i, j) <==> x[i:j]
Use of negative indices is not supported.
Used for pickling.
TESTS:
sage: loads(dumps(RR['x'])) == RR['x']
True
Used for pickling.
TESTS:
sage: loads(dumps(CDF['x'])) == CDF['x']
True
Returns an element of self. Want it in sufficient generality that poorly-written functions won’t work when they’re not supposed to. This is cached so doesn’t have to be super fast.
EXAMPLES:
sage: QQ._an_element_()
1/2
sage: ZZ['x,y,z']._an_element_()
x
Override this method to specify coercions beyond those specified in coerce_list.
If no such coercion exists, return None or False. Otherwise, it may return either an actual Map to use for the coercion, a callable (in which case it will be wrapped in a Map), or True (in which case a generic map will be provided).
This attempts to construct a morphism from S to self by passing through one of the items in v (tried in order).
S may appear in the list, in which case algorithm will never progress beyond that point.
This is similar in spirit to the old {{{_coerce_try}}}, and useful when defining _coerce_map_from_
INPUT:
EXAMPLES:
sage: CDF._coerce_map_via([ZZ, RR, CC], int)
Composite map:
From: Set of Python objects of type 'int'
To: Complex Double Field
Defn: Native morphism:
From: Set of Python objects of type 'int'
To: Integer Ring
then
Native morphism:
From: Integer Ring
To: Complex Double Field
sage: CDF._coerce_map_via([ZZ, RR, CC], QQ)
Composite map:
From: Rational Field
To: Complex Double Field
Defn: Generic map:
From: Rational Field
To: Real Field with 53 bits of precision
then
Native morphism:
From: Real Field with 53 bits of precision
To: Complex Double Field
sage: CDF._coerce_map_via([ZZ, RR, CC], CC)
Generic map:
From: Complex Field with 53 bits of precision
To: Complex Double Field
Override this method to provide additional conversions beyond those given in convert_list.
This function is called after coercions are attempted. If there is a coercion morphism in the opposite direction, one should consider adding a section method to that.
This MUST return a Map from S to self, or None. If None is returned then a generic map will be provided.
Returns the default conversion map based on the data provided to _populate_coercion_lists_().
This called when _coerce_map_from_() returns True.
If a convert_method_name is provided, it creates a NamedConvertMap, otherwise it creates a DefaultConvertMap or DefaultConvertMap_unique depending on whether or not init_no_parent is set.
EXAMPLES:
Override this method to provide an action of self on S or S on self beyond what was specified in action_list.
This must return an action which accepts an element of self and an element of S (in the order specified by self_on_left).
Used for debugging the coercion model.
EXAMPLES:
sage: sorted(QQ._introspect_coerce().items())
[('_action_hash', {...}),
('_action_list', []),
('_coerce_from_hash', {...}),
('_coerce_from_list', []),
('_convert_from_hash', {...}),
('_convert_from_list', [...]),
('_element_init_pass_parent', False),
('_embedding', None),
('_initial_action_list', []),
('_initial_coerce_list', []),
('_initial_convert_list', [])]
Return True if im_gens defines a valid homomorphism from self to codomain; otherwise return False.
If determining whether or not a homomorphism is valid has not been implemented for this ring, then a NotImplementedError exception is raised.
This function allows one to specify coercions, actions, conversions and embeddings involving this parent.
IT SHOULD ONLY BE CALLED DURING THE __INIT__ method, often at the end.
INPUT:
coerce_list - a list of coercion Morphisms to self and parents with canonical coercions to self
action_list - a list of actions on and by self
parents with conversions to self
embedding - a single Morphism from self
convert_method_name - a name to look for that other elements can implement to create elements of self (e.g. _integer_)
element_constructor - A callable object used by the __call__ method to construct new elements. Typically the element class or a bound method (defaults to self._element_constructor_).
init_no_parent - if True omit passing self in as the first argument of element_constructor for conversion. This is useful if parents are unique, or element_constructor is a bound method (this latter case can be detected automatically).
Compare left and right.
EXAMPLES:
sage: ZZ < QQ
True
Implementation of a function that returns an element (often non-trivial) of a parent object. This is cached. Parent structures that are should override _an_element_() instead.
EXAMPLES:
sage: CDF.an_element()
1.0*I
sage: ZZ[['t']].an_element()
t
Return x as an element of self, if and only if there is a canonical coercion from the parent of x to self.
EXAMPLES:
sage: QQ.coerce(ZZ(2))
2
sage: ZZ.coerce(QQ(2))
...
TypeError: no canonical coercion from Rational Field to Integer Ring
We make an exception for zero:
sage: V = GF(7)^7
sage: V.coerce(0)
(0, 0, 0, 0, 0, 0, 0)
Returns the embedding of self into some other parent, if such a parent exists.
This does not mean that there are no coercion maps from self into other fields, this is simply a specific morphism specified out of self and usually denotes a special relationship (e.g. sub-objects, choice of completion, etc.)
EXAMPLES:
This returns a Map object to coerce from S to self if one exists, or None if no such coercion exists.
EXAMPLES:
sage: ZZ.coerce_map_from(int)
Native morphism:
From: Set of Python objects of type 'int'
To: Integer Ring
sage: QQ.coerce_map_from(ZZ)
Natural morphism:
From: Integer Ring
To: Rational Field
Returns a pair (functor, parent) such that functor(parent) return self. If this ring does not have a functorial construction, return None.
EXAMPLES:
sage: QQ.construction()
(FractionField, Integer Ring)
sage: f, R = QQ['x'].construction()
sage: f
Poly[x]
sage: R
Rational Field
sage: f(R)
Univariate Polynomial Ring in x over Rational Field
This function returns a Map from S to self, which may or may not succeed on all inputs. If a coercion map from S to self exists, then the it will be returned. If a coercion from self to S exists, then it will attempt to return a section of that map.
Under the new coercion model, this is the fastest way to convert elements of S to elements of self (short of manually constructing the elements) and is used by __call__.
EXAMPLES:
sage: m = ZZ.convert_map_from(QQ)
sage: m(-35/7)
-5
sage: parent(m(-35/7))
Integer Ring
Returns an action of self on S or S on self.
To provide additional actions, override _get_action_().
TESTS:
sage: M = QQ['y']^3
sage: M.get_action(ZZ['x']['y'])
Right scalar multiplication by Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Integer Ring on Ambient free module of rank 3 over the principal ideal domain Univariate Polynomial Ring in y over Rational Field
sage: M.get_action(ZZ['x']) # should be None
Return True if there is a natural map from S to self. Otherwise, return False.
EXAMPLES:
sage: RDF.has_coerce_map_from(QQ)
True
sage: RDF.has_coerce_map_from(QQ['x'])
False
sage: RDF['x'].has_coerce_map_from(QQ['x'])
True
sage: RDF['x,y'].has_coerce_map_from(QQ['x'])
True
Return the unique homomorphism from self to codomain that sends self.gens() to the entries of im_gens. Raises a TypeError if there is no such homomorphism.
INPUT:
OUTPUT:
Note
As a shortcut, one can also give an object X instead of im_gens, in which case return the (if it exists) natural map to X.
EXAMPLE: Polynomial Ring We first illustrate construction of a few homomorphisms involving a polynomial ring.
sage: R.<x> = PolynomialRing(ZZ)
sage: f = R.hom([5], QQ)
sage: f(x^2 - 19)
6
sage: R.<x> = PolynomialRing(QQ)
sage: f = R.hom([5], GF(7))
...
TypeError: images do not define a valid homomorphism
sage: R.<x> = PolynomialRing(GF(7))
sage: f = R.hom([3], GF(49,'a'))
sage: f
Ring morphism:
From: Univariate Polynomial Ring in x over Finite Field of size 7
To: Finite Field in a of size 7^2
Defn: x |--> 3
sage: f(x+6)
2
sage: f(x^2+1)
3
EXAMPLE: Natural morphism
sage: f = ZZ.hom(GF(5))
sage: f(7)
2
sage: f
Ring Coercion morphism:
From: Integer Ring
To: Finite Field of size 5
There might not be a natural morphism, in which case a TypeError exception is raised.
sage: QQ.hom(ZZ)
...
TypeError: Natural coercion morphism from Rational Field to Integer Ring not defined.
Return True if elements of this ring are represented exactly, i.e., there is no precision loss when doing arithmetic.
NOTE: This defaults to true, so even if it does return True you have no guarantee (unless the ring has properly overloaded this).
Return the (unique) Parent that represents the set of Python objects of a specified type.
EXAMPLES:
sage: from sage.structure.parent import Set_PythonType
sage: Set_PythonType(list)
Set of Python objects of type 'list'
sage: Set_PythonType(list) is Set_PythonType(list)
True
sage: S = Set_PythonType(tuple)
sage: S([1,2,3])
(1, 2, 3)
By default, create a homset in the category of sets.
This doesn’t return Elements, but actual objects of the given type.
EXAMPLES:
sage: S = sage.structure.parent.Set_PythonType(float)
sage: S(5)
5.0
sage: S(9/3)
3.0
sage: S(1/3)
0.333333333333333...
Two Python type sets are considered the same if they contain the same type.
EXAMPLES:
sage: S = sage.structure.parent.Set_PythonType(int)
sage: S == S
True
sage: S == sage.structure.parent.Set_PythonType(float)
False
EXAMPLES:
sage: sage.structure.parent.Set_PythonType(tuple)
Set of Python objects of type 'tuple'
sage: sage.structure.parent.Set_PythonType(Integer)
Set of Python objects of type 'sage.rings.integer.Integer'
sage: sage.structure.parent.Set_PythonType(Parent)
Set of Python objects of type 'sage.structure.parent.Parent'
EXAMPLES:
sage: S = sage.structure.parent.Set_PythonType(bool)
sage: S.cardinality()
2
sage: S = sage.structure.parent.Set_PythonType(int)
sage: S.cardinality()
4294967296 # 32-bit
18446744073709551616 # 64-bit
sage: S = sage.structure.parent.Set_PythonType(float)
sage: S.cardinality()
18437736874454810627
sage: S = sage.structure.parent.Set_PythonType(long)
sage: S.cardinality()
+Infinity
EXAMPLES:
sage: S = sage.structure.parent.Set_PythonType(tuple)
sage: S.object()
<type 'tuple'>
Abstract base class for sets.
The category that this set belongs to, which is the category of all sets.
EXAMPLES:
sage: Set(QQ).category()
Category of sets
Return True if x is a parent object, i.e., derives from sage.structure.parent.Parent and False otherwise.
EXAMPLES:
sage: from sage.structure.parent import is_Parent
sage: is_Parent(2/3)
False
sage: is_Parent(ZZ)
True
sage: is_Parent(Primes())
True
TESTS:
sage: sage.structure.parent.normalize_names(5, 'x')
('x0', 'x1', 'x2', 'x3', 'x4')
sage: sage.structure.parent.normalize_names(2, ['x','y'])
('x', 'y')