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 b  
    468468            sage: f(*[2]) # unpacking the list to call normally
    469469            3*x + 4
    470470       
    471         The following results in an element of the symbolic ring. ::
     471        The following results in an element of the polynomial ring
     472        over the symbolic ring in y. ::
    472473       
    473474            sage: f(x=sqrt(2))
    474             (y + sqrt(2))*y + sqrt(2)
     475            y^2 + sqrt(2)*y + sqrt(2)
    475476
    476477        ::
    477478       
     
    937938
    938939    def _symbolic_(self, R):
    939940        """
     941        Sage does not allow direct coercion from a polynomial
     942        ring to the symbolic ring. This is because the variable
     943        name of a polynomial ring should only be a display name.
     944       
     945        One can, however, always obtain a symbolic expression
     946        corresponding to the polynomial by substituting a symbolic
     947        variable into the polynomial.
     948
     949        As an exception, this method does return a value if the
     950        polynomial is constant (i.e. when it is of degree zero)
     951        and the constant coefficient can be coerced to the
     952        symbolic ring. This is because it should be possible to
     953        coerce constant polynomials to the base field, and the
     954        base field may be the symbolic ring.
     955
    940956        EXAMPLES::
    941957
    942             sage: R.<x> = QQ[]
    943             sage: f = x^3 + x
    944             sage: g = f._symbolic_(SR); g
     958            sage: R.<t> = QQ[]
     959            sage: f = t^3 + t
     960            sage: g = f._symbolic_(SR)
     961            Traceback (most recent call last):
     962            ...
     963            TypeError: not a constant polynomial
     964            sage: x = var('x')
     965            sage: g = f(x); g
    945966            (x^2 + 1)*x
    946967            sage: g(x=2)
    947968            10
    948969
    949             sage: g = SR(f)
    950             sage: g(x=2)
    951             10
    952 
    953         The polynomial does not have to be over a field of
    954         characteristic 0::
    955 
    956             sage: R.<w> = GF(7)[]
    957             sage: f = SR(2*w^3 + 1); f
    958             2*w^3 + 1
    959             sage: f.variables()
    960             (w,)
    961         """
    962         d = dict([(repr(g), R.var(g)) for g in self.parent().gens()])
    963         return self.subs(**d)
     970            sage: R.<t> = SR[]
     971            sage: h = (x^2 + 1)*t + 13*x^2; h
     972            (x^2 + 1)*t + 13*x^2
     973            sage: SR(h)
     974            Traceback (most recent call last):
     975            ...
     976            TypeError: not a constant polynomial
     977            sage: SR(h - (x^2 + 1)*t)
     978            13*x^2
     979        """
     980        if self.degree() > 0:
     981            raise TypeError("not a constant polynomial")
     982        return R(self[0])
    964983
    965984    def __invert__(self):
    966985        """
  • sage/symbolic/ring.pyx

    diff --git a/sage/symbolic/ring.pyx b/sage/symbolic/ring.pyx
    a b  
    104104            sage: SR.coerce(-infinity)
    105105            -Infinity
    106106            sage: SR.has_coerce_map_from(ZZ['t'])
    107             True
     107            False
    108108            sage: SR.has_coerce_map_from(ZZ['t,u,v'])
    109             True
     109            False
    110110            sage: SR.has_coerce_map_from(Frac(ZZ['t,u,v']))
    111             True
     111            False
    112112            sage: SR.has_coerce_map_from(GF(5)['t'])
    113             True
     113            False
    114114            sage: SR.has_coerce_map_from(SR['t'])
    115115            False
    116116            sage: SR.has_coerce_map_from(Integers(8))
     
    173173                # sage: QQbar(sqrt(2)) + sqrt(3)
    174174                # 3.146264369941973?
    175175                return R not in (RLF, CLF, AA, QQbar)
    176             elif is_PolynomialRing(R) or is_MPolynomialRing(R) or is_FractionField(R):
     176            elif is_PolynomialRing(R) or is_MPolynomialRing(R):
     177                return False
     178            elif is_FractionField(R):
    177179                base = R.base_ring()
    178180                return base is not self and self.has_coerce_map_from(base)
    179181            elif (R is InfinityRing