r"""1Base class for old-style parent objects with a base ring2"""3###############################################################################4# Sage: System for Algebra and Geometry Experimentation5# Copyright (C) 2006 William Stein <[email protected]>6# Distributed under the terms of the GNU General Public License (GPL)7# The full text of the GPL is available at:8# http://www.gnu.org/licenses/9###############################################################################1011include "../ext/stdsage.pxi"1213cimport parent1415from coerce_exceptions import CoercionException1617cdef inline check_old_coerce(parent.Parent p):18if p._element_constructor is not None:19raise RuntimeError, "%s still using old coercion framework" % p202122# TODO: Unpickled parents with base sometimes have their base set to None.23# This causes a segfault in the module arithmetic architecture.24#25# sage: H = HomsetWithBase(QQ, RR, base=ZZ); H26# sage: H0 = loads(dumps(H))27# sage: H.base_ring(), H0.base_ring()28# (Integer Ring, None)29#30# Perhaps the code below would help (why was it commented out?).3132## def make_parent_with_base_v0(_class, _dict, base, has_coerce_map_from):33## """34## This should work for any Python class deriving from this, as long35## as it doesn't implement some screwy __new__() method.36## """37## new_object = _class.__new__(_class)38## if base is None:39## (<ParentWithBase>new_object)._base = new_object40## else:41## (<ParentWithBase>new_object)._base = base42## (<ParentWithBase>new_object)._has_coerce_map_from = has_coerce_map_from43## if not _dict is None:44## new_object.__dict__ = _dict45## return new_object4647def is_ParentWithBase(x):48"""49Return True if x is a parent object with base.50"""51return bool(PY_TYPE_CHECK(x, ParentWithBase))5253cdef class ParentWithBase(parent_old.Parent):54"""55This class is being deprecated, see parent.Parent for the new model.56"""57def __init__(self, base, coerce_from=[], actions=[], embeddings=[], category=None):58# TODO: SymbolicExpressionRing has base RR, which makes this bad59# print type(self), "base", base, coerce_from60# if base != self and not base in coerce_from:61# coerce_from.append(base)62parent_old.Parent.__init__(self, coerce_from=coerce_from, actions=actions, embeddings=embeddings, category=category)63self._base = base6465def _richcmp(left, right, int op):66check_old_coerce(left)67return (<parent_old.Parent>left)._richcmp(right, op) # the cdef method686970cdef _coerce_c_impl(self,x):71check_old_coerce(self)72if not self._base is self:73return self._coerce_try(x,(self._base))74else:75raise TypeError, "No canonical coercion found."7677## def x__reduce__(self):78## if HAS_DICTIONARY(self):79## _dict = self.__dict__80## else:81## _dict = None82## if self._base is self:83## return (make_parent_with_base_v0, (self.__class__, _dict, None, self._has_coerce_map_from))84## else:85## return (make_parent_with_base_v0, (self.__class__, _dict, self._base, self._has_coerce_map_from))8687# Derived class *must* define base_extend.88def base_extend(self, X):89check_old_coerce(self)90raise CoercionException, "BUG: the base_extend method must be defined for '%s' (class '%s')"%(91self, type(self))9293############################################################################94# Homomorphism --95############################################################################96def Hom(self, codomain, category = None):97r"""98self.Hom(codomain, category = None):99100Returns the homspace \code{Hom(self, codomain, category)} of all101homomorphisms from self to codomain in the category cat. The102default category is \code{self.category()}.103104EXAMPLES:105sage: R.<x,y> = PolynomialRing(QQ, 2)106sage: R.Hom(QQ)107Set of Homomorphisms from Multivariate Polynomial Ring in x, y over Rational Field to Rational Field108109Homspaces are defined for very general \sage objects, even elements of familiar rings.110sage: n = 5; Hom(n,7)111Set of Morphisms from 5 to 7 in Category of elements of Integer Ring112sage: z=(2/3); Hom(z,8/1)113Set of Morphisms from 2/3 to 8 in Category of elements of Rational Field114115This example illustrates the optional third argument:116sage: QQ.Hom(ZZ, Sets())117Set of Morphisms from Rational Field to Integer Ring in Category of sets118"""119# NT 01-2009: what's the difference with parent.Parent.Hom???120if self._element_constructor is None:121return parent.Parent.Hom(self, codomain, category)122try:123return self._Hom_(codomain, category)124except (AttributeError, TypeError):125pass126from sage.categories.all import Hom127return Hom(self, codomain, category)128129130131