Ticket #14403: trac_14403_expression_polynomial.patch

File trac_14403_expression_polynomial.patch, 2.2 KB (added by gagern, 9 years ago)
  • sage/symbolic/expression_conversions.py

    # HG changeset patch
    # User Martin von Gagern <Martin.vGagern@gmx.net>
    # Date 1374783452 -7200
    #      Thu Jul 25 22:17:32 2013 +0200
    # Node ID 28b146354e661098b884374d353b9c6641a6ea50
    # Parent  d16ba836e9c3dc2c089786027f3e93e9f4844aca
    Trac 14403: expression to polynomial only for selected variables
    
    diff --git a/sage/symbolic/expression_conversions.py b/sage/symbolic/expression_conversions.py
    a b  
    898898
    899899        if ring is not None:
    900900            base_ring = ring.base_ring()
    901             G = ring.variable_names_recursive()
     901            self.varnames = ring.variable_names_recursive()
    902902            for v in ex.variables():
    903                 if repr(v) not in G and v not in base_ring:
     903                if repr(v) not in self.varnames and v not in base_ring:
    904904                    raise TypeError, "%s is not a variable of %s" %(v, ring)
    905905            self.ring = ring
    906906            self.base_ring = base_ring
     
    911911                vars = ['x']
    912912            from sage.rings.all import PolynomialRing
    913913            self.ring = PolynomialRing(self.base_ring, names=vars)
     914            self.varnames = self.ring.variable_names()
    914915        else:
    915916            raise TypeError, "either a ring or base ring must be specified"
    916917
     
    9991000            sage: p = PolynomialConverter(x+y, base_ring=RR)
    10001001            sage: p.arithmetic(x*y+y^2, operator.add)
    10011002            x*y + y^2
     1003
     1004            sage: p = PolynomialConverter(y^(3/2), ring=SR['x'])
     1005            sage: p.arithmetic(y^(3/2), operator.pow)
     1006            y^(3/2)
     1007            sage: _.parent()
     1008            Symbolic Ring
    10021009        """
    1003         if len(ex.variables()) == 0:
     1010        if not any(repr(v) in self.varnames for v in ex.variables()):
    10041011            return self.base_ring(ex)
    10051012        elif operator == _operator.pow:
    10061013            from sage.rings.all import Integer
     
    10441051         sage: _.parent()
    10451052         Univariate Polynomial Ring in t over Symbolic Ring
    10461053
    1047 
     1054         sage: polynomial(y - sqrt(x), ring=SR[y])
     1055         y - sqrt(x)
     1056         sage: _.list()
     1057         [-sqrt(x), 1]
    10481058
    10491059    The polynomials can have arbitrary (constant) coefficients so long as
    10501060    they coerce into the base ring::