# Ticket #13360: trac_13360_no_coercion_from_polynomials_to_expressions.patch

File trac_13360_no_coercion_from_polynomials_to_expressions.patch, 4.2 KB (added by tkluck, 9 years ago)
• ## sage/rings/polynomial/polynomial_element.pyx

```# HG changeset patch
# User Timo Kluck <tkluck@infty.nl>
# Date 1344711608 -7200
# Node ID 6fefb2e776d7bb17611b0771bacff4ef24b34b42
# Parent  76a1123da8101f514b3c5cde6939cd666ab18ca9
Don't allow coercing for polynomial rings to the symbolic ring

diff --git a/sage/rings/polynomial/polynomial_element.pyx b/sage/rings/polynomial/polynomial_element.pyx```
 a sage: f(*[2]) # unpacking the list to call normally 3*x + 4 The following results in an element of the symbolic ring. :: The following results in an element of the polynomial ring over the symbolic ring in y. :: sage: f(x=sqrt(2)) (y + sqrt(2))*y + sqrt(2) y^2 + sqrt(2)*y + sqrt(2) :: def _symbolic_(self, R): """ Sage does not allow direct coercion from a polynomial ring to the symbolic ring. This is because the variable name of a polynomial ring should only be a display name. One can, however, always obtain a symbolic expression corresponding to the polynomial by substituting a symbolic variable into the polynomial. As an exception, this method does return a value if the polynomial is constant (i.e. when it is of degree zero) and the constant coefficient can be coerced to the symbolic ring. This is because it should be possible to coerce constant polynomials to the base field, and the base field may be the symbolic ring. EXAMPLES:: sage: R. = QQ[] sage: f = x^3 + x sage: g = f._symbolic_(SR); g sage: R. = QQ[] sage: f = t^3 + t sage: g = f._symbolic_(SR) Traceback (most recent call last): ... TypeError: not a constant polynomial sage: x = var('x') sage: g = f(x); g (x^2 + 1)*x sage: g(x=2) 10 sage: g = SR(f) sage: g(x=2) 10 The polynomial does not have to be over a field of characteristic 0:: sage: R. = GF(7)[] sage: f = SR(2*w^3 + 1); f 2*w^3 + 1 sage: f.variables() (w,) """ d = dict([(repr(g), R.var(g)) for g in self.parent().gens()]) return self.subs(**d) sage: R. = SR[] sage: h = (x^2 + 1)*t + 13*x^2; h (x^2 + 1)*t + 13*x^2 sage: SR(h) Traceback (most recent call last): ... TypeError: not a constant polynomial sage: SR(h - (x^2 + 1)*t) 13*x^2 """ if self.degree() > 0: raise TypeError("not a constant polynomial") return R(self[0]) def __invert__(self): """
• ## sage/symbolic/ring.pyx

`diff --git a/sage/symbolic/ring.pyx b/sage/symbolic/ring.pyx`
 a sage: SR.coerce(-infinity) -Infinity sage: SR.has_coerce_map_from(ZZ['t']) True False sage: SR.has_coerce_map_from(ZZ['t,u,v']) True False sage: SR.has_coerce_map_from(Frac(ZZ['t,u,v'])) True False sage: SR.has_coerce_map_from(GF(5)['t']) True False sage: SR.has_coerce_map_from(SR['t']) False sage: SR.has_coerce_map_from(Integers(8)) # sage: QQbar(sqrt(2)) + sqrt(3) # 3.146264369941973? return R not in (RLF, CLF, AA, QQbar) elif is_PolynomialRing(R) or is_MPolynomialRing(R) or is_FractionField(R): elif is_PolynomialRing(R) or is_MPolynomialRing(R): return False elif is_FractionField(R): base = R.base_ring() return base is not self and self.has_coerce_map_from(base) elif (R is InfinityRing