Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/structure/parent_base.pyx
4047 views
1
r"""
2
Base class for old-style parent objects with a base ring
3
"""
4
###############################################################################
5
# Sage: System for Algebra and Geometry Experimentation
6
# Copyright (C) 2006 William Stein <[email protected]>
7
# Distributed under the terms of the GNU General Public License (GPL)
8
# The full text of the GPL is available at:
9
# http://www.gnu.org/licenses/
10
###############################################################################
11
12
include "../ext/stdsage.pxi"
13
14
cimport parent
15
16
from coerce_exceptions import CoercionException
17
18
cdef inline check_old_coerce(parent.Parent p):
19
if p._element_constructor is not None:
20
raise RuntimeError, "%s still using old coercion framework" % p
21
22
23
# TODO: Unpickled parents with base sometimes have their base set to None.
24
# This causes a segfault in the module arithmetic architecture.
25
#
26
# sage: H = HomsetWithBase(QQ, RR, base=ZZ); H
27
# sage: H0 = loads(dumps(H))
28
# sage: H.base_ring(), H0.base_ring()
29
# (Integer Ring, None)
30
#
31
# Perhaps the code below would help (why was it commented out?).
32
33
## def make_parent_with_base_v0(_class, _dict, base, has_coerce_map_from):
34
## """
35
## This should work for any Python class deriving from this, as long
36
## as it doesn't implement some screwy __new__() method.
37
## """
38
## new_object = _class.__new__(_class)
39
## if base is None:
40
## (<ParentWithBase>new_object)._base = new_object
41
## else:
42
## (<ParentWithBase>new_object)._base = base
43
## (<ParentWithBase>new_object)._has_coerce_map_from = has_coerce_map_from
44
## if not _dict is None:
45
## new_object.__dict__ = _dict
46
## return new_object
47
48
def is_ParentWithBase(x):
49
"""
50
Return True if x is a parent object with base.
51
"""
52
return bool(PY_TYPE_CHECK(x, ParentWithBase))
53
54
cdef class ParentWithBase(parent_old.Parent):
55
"""
56
This class is being deprecated, see parent.Parent for the new model.
57
"""
58
def __init__(self, base, coerce_from=[], actions=[], embeddings=[], category=None):
59
# TODO: SymbolicExpressionRing has base RR, which makes this bad
60
# print type(self), "base", base, coerce_from
61
# if base != self and not base in coerce_from:
62
# coerce_from.append(base)
63
parent_old.Parent.__init__(self, coerce_from=coerce_from, actions=actions, embeddings=embeddings, category=category)
64
self._base = base
65
66
def _richcmp(left, right, int op):
67
check_old_coerce(left)
68
return (<parent_old.Parent>left)._richcmp(right, op) # the cdef method
69
70
71
cdef _coerce_c_impl(self,x):
72
check_old_coerce(self)
73
if not self._base is self:
74
return self._coerce_try(x,(self._base))
75
else:
76
raise TypeError, "No canonical coercion found."
77
78
## def x__reduce__(self):
79
## if HAS_DICTIONARY(self):
80
## _dict = self.__dict__
81
## else:
82
## _dict = None
83
## if self._base is self:
84
## return (make_parent_with_base_v0, (self.__class__, _dict, None, self._has_coerce_map_from))
85
## else:
86
## return (make_parent_with_base_v0, (self.__class__, _dict, self._base, self._has_coerce_map_from))
87
88
# Derived class *must* define base_extend.
89
def base_extend(self, X):
90
check_old_coerce(self)
91
raise CoercionException, "BUG: the base_extend method must be defined for '%s' (class '%s')"%(
92
self, type(self))
93
94
############################################################################
95
# Homomorphism --
96
############################################################################
97
def Hom(self, codomain, category = None):
98
r"""
99
self.Hom(codomain, category = None):
100
101
Returns the homspace \code{Hom(self, codomain, category)} of all
102
homomorphisms from self to codomain in the category cat. The
103
default category is \code{self.category()}.
104
105
EXAMPLES:
106
sage: R.<x,y> = PolynomialRing(QQ, 2)
107
sage: R.Hom(QQ)
108
Set of Homomorphisms from Multivariate Polynomial Ring in x, y over Rational Field to Rational Field
109
110
Homspaces are defined for very general \sage objects, even elements of familiar rings.
111
sage: n = 5; Hom(n,7)
112
Set of Morphisms from 5 to 7 in Category of elements of Integer Ring
113
sage: z=(2/3); Hom(z,8/1)
114
Set of Morphisms from 2/3 to 8 in Category of elements of Rational Field
115
116
This example illustrates the optional third argument:
117
sage: QQ.Hom(ZZ, Sets())
118
Set of Morphisms from Rational Field to Integer Ring in Category of sets
119
"""
120
# NT 01-2009: what's the difference with parent.Parent.Hom???
121
if self._element_constructor is None:
122
return parent.Parent.Hom(self, codomain, category)
123
try:
124
return self._Hom_(codomain, category)
125
except (AttributeError, TypeError):
126
pass
127
from sage.categories.all import Hom
128
return Hom(self, codomain, category)
129
130
131