Skip to content

Commit

Permalink
fixing doctests, simplify coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
fchapoton committed Feb 19, 2023
1 parent 8650c0c commit bb38198
Showing 1 changed file with 21 additions and 87 deletions.
108 changes: 21 additions & 87 deletions src/sage/rings/polynomial/integer_valued_polynomials.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def _element_constructor_(self, x):
return self.monomial(W(x))

P = x.parent()
if isinstance(P, IntegerValuedPolynomialRing):
if isinstance(P, IntegerValuedPolynomialRing.Shifted):
if P is self:
return x
if P is not self.base_ring():
Expand All @@ -497,20 +497,21 @@ def _element_constructor_(self, x):
return self.element_class(self, {})
return self.from_base_ring_from_one_basis(x)

def _coerce_impl(self, x):
def _coerce_map_from_(self, R):
r"""
Canonical coercion of ``x`` into ``self``.
Return ``True`` if there is a coercion from ``R`` into ``self``
and ``False`` otherwise.
Here is what canonically coerces to ``self``:
INPUT:
- this integer-valued polynomial ring,
- ``R`` -- a commutative ring
The things that coerce into ``self`` are
- anything that coerces to the base ring of this
integer-valued polynomial ring,
- Integer-Valued Polynomial Rings over a base
with a coercion map into ``self.base_ring()``.
- any integer-valued polynomial ring,
whose base ring coerces to the base ring of this
integer-valued polynomial ring.
- Anything with a coercion into ``self.base_ring()``.
EXAMPLES::
Expand Down Expand Up @@ -565,40 +566,6 @@ def _coerce_impl(self, x):
Ring over Finite Field of size 7 in the shifted basis
to Integer-Valued Polynomial
Ring over Integer Ring in the shifted basis
"""
try:
R = x.parent()

# integer-valued polynomial rings
# over any base that coerces in:
if isinstance(R, IntegerValuedPolynomialRing):
if self.has_coerce_map_from(R.base_ring()):
return self(x)
raise TypeError("no natural map between bases of"
" integer-valued polynomial rings")

except AttributeError:
pass

# any ring that coerces to the base ring of this integer-valued
# polynomial ring.
return self._coerce_try(x, [self.base_ring()])

def _coerce_map_from_(self, R):
r"""
Return ``True`` if there is a coercion from ``R`` into ``self``
and ``False`` otherwise.
INPUT:
- ``R`` -- a commutative ring
The things that coerce into ``self`` are
- Integer-Valued Polynomial Rings over a base
with a coercion map into ``self.base_ring()``.
- Anything with a coercion into ``self.base_ring()``.
TESTS::
Expand All @@ -620,7 +587,7 @@ def _coerce_map_from_(self, R):
"""
# integer-valued polynomial rings over any base
# that coerces in:
if isinstance(R, IntegerValuedPolynomialRing):
if isinstance(R, IntegerValuedPolynomialRing.Shifted):
return self.base_ring().has_coerce_map_from(R.base_ring())
return self.base_ring().has_coerce_map_from(R)

Expand Down Expand Up @@ -1035,7 +1002,7 @@ def _element_constructor_(self, x):
return self.monomial(W(x))

P = x.parent()
if isinstance(P, IntegerValuedPolynomialRing):
if isinstance(P, IntegerValuedPolynomialRing.Binomial):
if P is self:
return x
if P is not self.base_ring():
Expand All @@ -1049,20 +1016,17 @@ def _element_constructor_(self, x):
return self.element_class(self, {})
return self.from_base_ring_from_one_basis(x)

def _coerce_impl(self, x):
def _coerce_map_from_(self, R):
r"""
Canonical coercion of ``x`` into ``self``.
Here is what canonically coerces to ``self``:
Return ``True`` if there is a coercion from ``R`` into ``self``
and ``False`` otherwise.
- this integer-valued polynomial ring,
The things that coerce into ``self`` are
- anything that coerces to the base ring of this
integer-valued polynomial ring,
- Integer-Valued Polynomial Rings over a base
with a coercion map into ``self.base_ring()``.
- any integer-valued polynomial ring,
whose base ring coerces to the base ring of this
integer-valued polynomial ring.
- Anything with a coercion into ``self.base_ring()``.
EXAMPLES::
Expand Down Expand Up @@ -1117,36 +1081,6 @@ def _coerce_impl(self, x):
Ring over Finite Field of size 7 in the binomial basis to
Integer-Valued Polynomial Ring over Integer Ring
in the binomial basis
"""
try:
R = x.parent()

# integer-valued polynomial rings
# over any base that coerces in:
if isinstance(R, IntegerValuedPolynomialRing):
if self.has_coerce_map_from(R.base_ring()):
return self(x)
raise TypeError("no natural map between bases of"
" integer-valued polynomial rings")

except AttributeError:
pass

# any ring that coerces to the base ring of this integer-valued
# polynomial ring.
return self._coerce_try(x, [self.base_ring()])

def _coerce_map_from_(self, R):
r"""
Return ``True`` if there is a coercion from ``R`` into ``self``
and ``False`` otherwise.
The things that coerce into ``self`` are
- Integer-Valued Polynomial Rings over a base
with a coercion map into ``self.base_ring()``.
- Anything with a coercion into ``self.base_ring()``.
TESTS::
Expand All @@ -1168,7 +1102,7 @@ def _coerce_map_from_(self, R):
"""
# integer-valued polynomial rings over any base
# that coerces in:
if isinstance(R, IntegerValuedPolynomialRing):
if isinstance(R, IntegerValuedPolynomialRing.Binomial):
return self.base_ring().has_coerce_map_from(R.base_ring())
return self.base_ring().has_coerce_map_from(R)

Expand Down

0 comments on commit bb38198

Please sign in to comment.