Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Move _is_coercion from DefaultConvertMap to Map and make it accurate
Browse files Browse the repository at this point in the history
  • Loading branch information
roed314 authored and GIT_AUTHOR_NAME committed Jun 11, 2017
1 parent d9e145d commit 6be53f3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/sage/categories/map.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cdef class Map(Element):
cdef object _category_for # category in which this is a morphism

cdef public _repr_type_str

cdef public bint _is_coercion

cdef class Section(Map):
cdef Map _inverse
Expand Down
8 changes: 8 additions & 0 deletions src/sage/categories/map.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ cdef class Map(Element):
self._codomain = C
self.domain = ConstantFunction(D)
self.codomain = ConstantFunction(C)
self._is_coercion = False
if D.is_exact() and C.is_exact():
self._coerce_cost = 10 # default value.
else:
Expand Down Expand Up @@ -403,6 +404,11 @@ cdef class Map(Element):
self._repr_type_str = _slots['_repr_type_str']
else:
self._repr_type_str = None
# Same for _is_coercion
if '_is_coercion' in _slots:
self._is_coercion = _slots['_is_coercion']
else:
self._is_coercion = None

def _update_slots_test(self, _slots):
"""
Expand Down Expand Up @@ -438,6 +444,7 @@ cdef class Map(Element):
"""
_slots['_domain'] = self.domain()
_slots['_codomain'] = self._codomain
_slots['_is_coercion'] = self._is_coercion
_slots['_repr_type_str'] = self._repr_type_str
return _slots

Expand All @@ -452,6 +459,7 @@ cdef class Map(Element):
sage: f._extra_slots_test({"bla": 1})
{'_codomain': Integer Ring,
'_domain': Rational Field,
'_is_coercion': False,
'_repr_type_str': None,
'bla': 1}
"""
Expand Down
1 change: 1 addition & 0 deletions src/sage/categories/morphism.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ cdef class SetMorphism(Morphism):
{'_codomain': Integer Ring,
'_domain': Integer Ring,
'_function': <built-in function __abs__>,
'_is_coercion': False,
'_repr_type_str': None,
'bla': 1}
"""
Expand Down
1 change: 0 additions & 1 deletion src/sage/structure/coerce_maps.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ from sage.categories.map cimport Map

cdef class DefaultConvertMap(Map):
cdef public bint _force_use
cdef public bint _is_coercion

cdef class DefaultConvertMap_unique(DefaultConvertMap):
pass
Expand Down
34 changes: 32 additions & 2 deletions src/sage/structure/coerce_maps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ cdef class DefaultConvertMap(Map):
"""
This morphism simply calls the codomain's element_constructor method,
passing in the codomain as the first argument.
EXAMPLES::
sage: QQ[['x']].coerce_map_from(QQ)
Conversion map:
From: Rational Field
To: Power Series Ring in x over Rational Field
"""
def __init__(self, domain, codomain, force_use=False):
"""
Expand All @@ -34,6 +41,13 @@ cdef class DefaultConvertMap(Map):
To: Finite Field of size 11
sage: f.parent()
Set of Morphisms from Finite Field of size 7 to Finite Field of size 11 in Category of sets with partial maps
Test that `trac`:23211 is resolved::
sage: f._is_coercion
False
sage: QQ[['x']].coerce_map_from(QQ)._is_coercion
True
"""
if not isinstance(domain, Parent):
domain = Set_PythonType(domain)
Expand All @@ -48,15 +62,22 @@ cdef class DefaultConvertMap(Map):

cdef dict _extra_slots(self, dict _slots):
_slots['_force_use'] = self._force_use
_slots['_is_coercion'] = self._is_coercion
return Map._extra_slots(self, _slots)

cdef _update_slots(self, dict _slots):
self._force_use = _slots['_force_use']
self._is_coercion = _slots['_is_coercion']
Map._update_slots(self, _slots)

cpdef Element _call_(self, x):
"""
Create an element of the codomain from a single element of the domain.
EXAMPLES::
sage: f = QQ[['x']].coerce_map_from(QQ)
sage: f(2/3).parent()
Power Series Ring in x over Rational Field
"""
cdef Parent C = self._codomain
try:
return C._element_constructor(C, x)
Expand All @@ -67,6 +88,15 @@ cdef class DefaultConvertMap(Map):
raise

cpdef Element _call_with_args(self, x, args=(), kwds={}):
"""
Create an element of the codomain from an element of the domain, with extra arguments.
EXAMPLES::
sage: f = QQ[['x']].coerce_map_from(QQ)
sage: f(2/3, 4)
2/3 + O(x^4)
"""
cdef Parent C = self._codomain
try:
if len(args) == 0:
Expand Down
4 changes: 4 additions & 0 deletions src/sage/structure/parent.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,7 @@ cdef class Parent(category_object.CategoryObject):
D = mor.domain()

assert not (self._coercions_used and D in self._coerce_from_hash), "coercion from {} to {} already registered or discovered".format(D, self)
mor._is_coercion = True
self._coerce_from_list.append(mor)
self._registered_domains.append(D)
self._coerce_from_hash.set(D,mor)
Expand Down Expand Up @@ -2046,6 +2047,7 @@ cdef class Parent(category_object.CategoryObject):
if S is self:
from sage.categories.homset import Hom
mor = Hom(self, self).identity()
mor._is_coercion = True
self._coerce_from_hash.set(S, mor)
return mor

Expand All @@ -2054,6 +2056,7 @@ cdef class Parent(category_object.CategoryObject):
if debug.unique_parent_warnings:
print("Warning: non-unique parents %s" % (type(S)))
mor = self._generic_convert_map(S)
mor._is_coercion = True
self._coerce_from_hash.set(S, mor)
mor._make_weak_references()
return mor
Expand Down Expand Up @@ -2081,6 +2084,7 @@ cdef class Parent(category_object.CategoryObject):
if (mor is not None) or _may_cache_none(self, S, "coerce"):
self._coerce_from_hash.set(S,mor)
if mor is not None:
mor._is_coercion = True
mor._make_weak_references()
return mor
except CoercionException as ex:
Expand Down

0 comments on commit 6be53f3

Please sign in to comment.