From eeeb29316df0b8603bd73367fb6fd527c383692f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Tue, 4 Mar 2014 12:11:46 +0100 Subject: [PATCH 1/3] trac #11840 first step, plus doc python 3 and trac role cleanup --- src/sage/symbolic/expression.pyx | 246 ++++++++++++++++--------------- 1 file changed, 130 insertions(+), 116 deletions(-) diff --git a/src/sage/symbolic/expression.pyx b/src/sage/symbolic/expression.pyx index 13c7ca8529a..7b0349e1931 100644 --- a/src/sage/symbolic/expression.pyx +++ b/src/sage/symbolic/expression.pyx @@ -58,7 +58,7 @@ the same:: sage: (x^3 + x <= x - 17) + (-x <= x - 10) x^3 <= 2*x - 27 -Here they aren't:: +Here they are not:: sage: (x^3 + x <= x - 17) + (-x >= x - 10) Traceback (most recent call last): @@ -82,7 +82,7 @@ We mix Singular variables with symbolic variables:: TESTS: -Test Jacobian on Pynac expressions. #5546 :: +Test Jacobian on Pynac expressions. (:trac:`5546`) :: sage: var('x,y') (x, y) @@ -91,7 +91,7 @@ Test Jacobian on Pynac expressions. #5546 :: [1 1] -Test if matrices work #5546 :: +Test if matrices work :trac:`5546` :: sage: var('x,y,z') (x, y, z) @@ -102,7 +102,7 @@ Test if matrices work #5546 :: sage: v*M (x^2 + y*z, 2*x*y) -Test if comparison bugs from #6256 are fixed:: +Test if comparison bugs from :trac:`6256` are fixed:: sage: t = exp(sqrt(x)); u = 1/t sage: t*u @@ -112,7 +112,7 @@ Test if comparison bugs from #6256 are fixed:: sage: t e^sqrt(x) -Test if #9947 is fixed:: +Test if :trac:`9947` is fixed:: sage: real_part(1+2*(sqrt(2)+1)*(sqrt(2)-1)) 3 @@ -152,7 +152,8 @@ from sage.misc.decorators import rename_keyword from sage.misc.superseded import deprecated_function_alias from sage.structure.dynamic_class import dynamic_class -LOG_TEN_TWO_PLUS_EPSILON = 3.321928094887363 # a small overestimate of log(10,2) +LOG_TEN_TWO_PLUS_EPSILON = 3.321928094887363 +# a small overestimate of log(10,2) cpdef bint is_Expression(x): """ @@ -248,13 +249,13 @@ cdef class Expression(CommutativeRingElement): raise TypeError('Python infinity cannot have complex phase.') if not is_a_numeric(self._gobj): - raise TypeError, "self must be a numeric expression" + raise TypeError("self must be a numeric expression") return py_object_from_numeric(self._gobj) def __init__(self, SR, x=0): """ Nearly all expressions are created by calling new_Expression_from_*, - but we need to make sure this at least doesn't leave self._gobj + but we need to make sure this at least does not leave self._gobj uninitialized and segfault. TESTS:: @@ -396,7 +397,7 @@ cdef class Expression(CommutativeRingElement): """ # check input if state[0] != 0 or len(state) != 3: - raise ValueError, "unknown state information" + raise ValueError("unknown state information") # set parent self._set_parent(ring.SR) # get variables @@ -505,7 +506,7 @@ cdef class Expression(CommutativeRingElement): sage: SR(a+1)^x (a + 1)^x - Check if #7876 is fixed:: + Check if :trac:`7876` is fixed:: sage: (1/2-1/2*I )*sqrt(2) -(1/2*I - 1/2)*sqrt(2) @@ -691,7 +692,7 @@ cdef class Expression(CommutativeRingElement): sage: latex(x*(1/(x^2)+sqrt(x^7))) x {\left(\sqrt{x^{7}} + \frac{1}{x^{2}}\right)} - Check spacing of coefficients of mul expressions (#3202):: + Check spacing of coefficients of mul expressions (:trac:`3202`):: sage: latex(2*3^x) 2 \, 3^{x} @@ -720,7 +721,7 @@ cdef class Expression(CommutativeRingElement): sage: latex(SR(a+1)^x) \left(a + 1\right)^{x} - More powers, #7406:: + More powers, :trac:`7406`:: sage: latex((x^pi)^e) {\left(x^{\pi}\right)}^{e} @@ -732,7 +733,7 @@ cdef class Expression(CommutativeRingElement): sage: latex((a^b)^c) {\left(a^{b}\right)}^{c} - Separate coefficients to numerator and denominator, #7363:: + Separate coefficients to numerator and denominator, :trac:`7363`:: sage: latex(2/(x+1)) \frac{2}{x + 1} @@ -740,14 +741,14 @@ cdef class Expression(CommutativeRingElement): \frac{1}{2 \, {\left(x + 1\right)}} Check if rational function coefficients without a ``numerator()`` method - are printed correctly. #8491:: + are printed correctly. :trac:`8491`:: sage: latex(6.5/x) \frac{6.50000000000000}{x} sage: latex(Mod(2,7)/x) \frac{2}{x} - Check if we avoid extra parenthesis in rational functions (#8688):: + Check if we avoid extra parenthesis in rational functions (:trac:`8688`):: sage: latex((x+2)/(x^3+1)) \frac{x + 2}{x^{3} + 1} @@ -756,14 +757,14 @@ cdef class Expression(CommutativeRingElement): sage: latex((x+2)/(x^3+1)/(x+1)) \frac{x + 2}{{\left(x^{3} + 1\right)} {\left(x + 1\right)}} - Check that the sign is correct (#9086):: + Check that the sign is correct (:trac:`9086`):: sage: latex(-1/x) -\frac{1}{x} sage: latex(1/-x) -\frac{1}{x} - More tests for the sign (#9314):: + More tests for the sign (:trac:`9314`):: sage: latex(-2/x) -\frac{2}{x} @@ -774,7 +775,7 @@ cdef class Expression(CommutativeRingElement): sage: latex(-x/z/y) -\frac{x}{y z} - Check if #9394 is fixed:: + Check if :trac:`9394` is fixed:: sage: var('n') n @@ -788,7 +789,7 @@ cdef class Expression(CommutativeRingElement): x - \left(2 i - 1\right) \, y Check if complex coefficients with denominators are displayed - correctly #10769:: + correctly :trac:`10769`:: sage: var('a x') (a, x) @@ -798,7 +799,7 @@ cdef class Expression(CommutativeRingElement): sage: latex(ratio) \frac{i \, x^{2}}{2 \, a} - Parenthesis in powers, #13262:: + Parenthesis in powers, :trac:`13262`:: sage: latex(1+x^(2/3)+x^(-2/3)) x^{\frac{2}{3}} + \frac{1}{x^{\frac{2}{3}}} + 1 @@ -855,7 +856,7 @@ cdef class Expression(CommutativeRingElement): try: n = self.pyobject() except TypeError: - raise TypeError, "unable to convert x (=%s) to an integer"%(self) + raise TypeError("unable to convert x (=%s) to an integer" % self) if isinstance(n, sage.rings.integer.Integer): return n return sage.rings.integer.Integer(n) @@ -887,13 +888,13 @@ cdef class Expression(CommutativeRingElement): try: rif_self = sage.rings.all.RIF(self) except TypeError: - raise ValueError, "cannot convert %s to int"%(self) + raise ValueError("cannot convert %s to int" % self) if rif_self > 0 or (rif_self.contains_zero() and self > 0): result = floor(self) else: result = ceil(self) if not isinstance(result, sage.rings.integer.Integer): - raise ValueError, "cannot convert %s to int"%(self) + raise ValueError("cannot convert %s to int" % self) else: return int(result) @@ -931,7 +932,7 @@ cdef class Expression(CommutativeRingElement): try: n = self.pyobject() except TypeError: - raise TypeError, "unable to convert %s to a rational"%self + raise TypeError("unable to convert %s to a rational" % self) if isinstance(n, sage.rings.rational.Rational): return n return sage.rings.rational.Rational(n) @@ -983,7 +984,7 @@ cdef class Expression(CommutativeRingElement): if is_a_numeric(res): return R(py_object_from_numeric(res)) else: - raise TypeError, "Cannot evaluate symbolic expression to a numeric value." + raise TypeError("Cannot evaluate symbolic expression to a numeric value.") cpdef _convert(self, R): """ @@ -1063,7 +1064,7 @@ cdef class Expression(CommutativeRingElement): try: return self._eval_self(R) except TypeError: - raise TypeError, "unable to simplify to a real interval approximation" + raise TypeError("unable to simplify to a real interval approximation") def _complex_mpfi_(self, R): """ @@ -1077,7 +1078,7 @@ cdef class Expression(CommutativeRingElement): try: return self._eval_self(R) except TypeError: - raise TypeError, "unable to simplify to a complex interval approximation" + raise TypeError("unable to simplify to a complex interval approximation") def _real_double_(self, R): """ @@ -1171,7 +1172,7 @@ cdef class Expression(CommutativeRingElement): try: return float(self._eval_self(float)) except TypeError: - raise TypeError, "unable to simplify to float approximation" + raise TypeError("unable to simplify to float approximation") def __complex__(self): """ @@ -1185,7 +1186,7 @@ cdef class Expression(CommutativeRingElement): try: return self._eval_self(complex) except TypeError: - raise TypeError, "unable to simplify to complex approximation" + raise TypeError("unable to simplify to complex approximation") def _sympy_(self): """ @@ -1284,7 +1285,7 @@ cdef class Expression(CommutativeRingElement): TESTS: Test if hashes for fderivatives with different parameters collide. - #6243:: + :trac:`6243`:: sage: f = function('f'); t = f(x,y) sage: u = t.derivative(x); v = t.derivative(y) @@ -1293,7 +1294,7 @@ cdef class Expression(CommutativeRingElement): sage: d = {u: 3, v: 5}; sorted(d.values()) [3, 5] - More checks for fderivative hashes #6851 :: + More checks for fderivative hashes :trac:`6851` :: sage: hash(f(x).derivative(x)) == hash(f(x).derivative(x,2)) False @@ -1301,8 +1302,9 @@ cdef class Expression(CommutativeRingElement): sage: len(d.keys()) 5 - We create a function with 10 arguments and test if there are hash - collisions between any of its derivatives of order at most 7. #7508:: + We create a function with 10 arguments and test if there are + hash collisions between any of its derivatives of order at + most 7. :trac:`7508`:: sage: num_vars = 10; max_order=7 sage: X = var(' '.join(['x'+str(i) for i in range(num_vars)])) @@ -1339,7 +1341,7 @@ cdef class Expression(CommutativeRingElement): sage: x^2 > x x^2 > x - Testing trac #11309 which changes the behavior of comparison of + Testing :trac:`11309` which changes the behavior of comparison of comparisons:: sage: (-x + y < 0) in [x - y < 0] @@ -1496,12 +1498,12 @@ cdef class Expression(CommutativeRingElement): from sage.symbolic.assumptions import _assumptions from sage.calculus.calculus import maxima if not self.is_relational(): - raise TypeError, "self (=%s) must be a relational expression"%self + raise TypeError("self (=%s) must be a relational expression" % self) if not self in _assumptions: m = self._maxima_init_assume_() s = maxima.assume(m) if str(s._sage_()[0]) in ['meaningless','inconsistent','redundant']: - raise ValueError, "Assumption is %s"%str(s._sage_()[0]) + raise ValueError("Assumption is %s" % str(s._sage_()[0])) _assumptions.append(self) def forget(self): @@ -1522,7 +1524,7 @@ cdef class Expression(CommutativeRingElement): TESTS: - Check if #7507 is fixed:: + Check if :trac:`7507` is fixed:: sage: forget() sage: n = var('n') @@ -1543,7 +1545,7 @@ cdef class Expression(CommutativeRingElement): from sage.symbolic.assumptions import _assumptions from sage.calculus.calculus import maxima if not self.is_relational(): - raise TypeError, "self (=%s) must be a relational expression"%self + raise TypeError("self (=%s) must be a relational expression" % self) m = self._maxima_init_assume_() maxima.forget(m) try: @@ -1629,7 +1631,7 @@ cdef class Expression(CommutativeRingElement): sage: (t0*x).is_real() False - The following is real, but we can't deduce that.:: + The following is real, but we cannot deduce that.:: sage: (x*x.conjugate()).is_real() False @@ -1724,7 +1726,7 @@ cdef class Expression(CommutativeRingElement): This function is intended to provide an interface to query the internal representation of the expression. In this sense, the word ``constant`` - doesn't reflect the mathematical properties of the expression. + does not reflect the mathematical properties of the expression. Expressions which have no variables may return ``False``. EXAMPLES:: @@ -1869,13 +1871,13 @@ cdef class Expression(CommutativeRingElement): TESTS: - Check if we can handle derivatives. #6523:: + Check if we can handle derivatives. :trac:`6523`:: sage: f(x) = function('f',x) sage: f(x).diff(x).is_zero() False - Check if #11352 is fixed:: + Check if :trac:`11352` is fixed:: sage: el = -1/2*(2*x^2 - sqrt(2*x - 1)*sqrt(2*x + 1) - 1) sage: el.is_polynomial(x) @@ -1929,7 +1931,7 @@ cdef class Expression(CommutativeRingElement): (x - 1)^2 """ if not self.is_relational(): - raise ValueError, "self must be a relational expression" + raise ValueError("self must be a relational expression") return new_Expression_from_GEx(self._parent, self._gobj.lhs()) lhs = left = left_hand_side @@ -1951,7 +1953,7 @@ cdef class Expression(CommutativeRingElement): x^2 - 2*x + 3 """ if not self.is_relational(): - raise ValueError, "self must be a relation" + raise ValueError("self must be a relation") return new_Expression_from_GEx(self._parent, self._gobj.rhs()) rhs = right = right_hand_side @@ -2093,8 +2095,8 @@ cdef class Expression(CommutativeRingElement): sage: bool(x == 1) False - The following must be true, even though we don't - know for sure that x isn't 1, as symbolic comparisons + The following must be true, even though we do not + know for sure that x is not 1, as symbolic comparisons elsewhere rely on x!=y unless we are sure it is not true; there is no equivalent of Maxima's ``unknown``. Since it is False that x==1, it is True that x != 1. @@ -2256,7 +2258,7 @@ cdef class Expression(CommutativeRingElement): cdef int k, eq_count = 0 cdef bint is_interval if not self.is_relational(): - raise ValueError, "self must be a relation" + raise ValueError("self must be a relation") cdef operators op = relational_operator(self._gobj) from sage.rings.real_mpfi import is_RealIntervalField from sage.rings.complex_interval_field import is_ComplexIntervalField @@ -2352,7 +2354,7 @@ cdef class Expression(CommutativeRingElement): 2*x < sqrt(2) """ if not self.is_relational(): - raise ValueError, "self must be a relation" + raise ValueError("self must be a relation") cdef operators op = relational_operator(self._gobj) if op == equal: falsify = operator.ne @@ -2823,7 +2825,7 @@ cdef class Expression(CommutativeRingElement): # TODO: change this to maybe cleverly do something involving Cython C++ exception handling. # See http://docs.cython.org/docs/wrapping_CPlusPlus.html if 'division by zero' in str(msg): - raise ZeroDivisionError, "Symbolic division by zero" + raise ZeroDivisionError("Symbolic division by zero") else: raise @@ -2852,7 +2854,7 @@ cdef class Expression(CommutativeRingElement): expressions when you do not want to get a formal inequality back. IMPORTANT: Both self and right *must* have the same type, or - this function won't be called. + this function will not be called. EXAMPLES:: @@ -3138,7 +3140,7 @@ cdef class Expression(CommutativeRingElement): sage: (2*I)^(1/2) sqrt(2*I) - Test if we can take powers of elements of Q(i) #8659:: + Test if we can take powers of elements of Q(i) :trac:`8659`:: sage: t = I.pyobject().parent()(8) sage: t^(1/2) @@ -3310,7 +3312,7 @@ cdef class Expression(CommutativeRingElement): ... ValueError: No differentiation variable specified. - Check if #6524 is fixed:: + Check if :trac:`6524` is fixed:: sage: f = function('f') sage: f(x)*f(x).derivative(x)*f(x).derivative(x,2) @@ -3332,13 +3334,13 @@ cdef class Expression(CommutativeRingElement): elif sage.symbolic.callable.is_CallableSymbolicExpression(self): return self.gradient() else: - raise ValueError, "No differentiation variable specified." + raise ValueError("No differentiation variable specified.") if not isinstance(deg, (int, long, sage.rings.integer.Integer)) \ or deg < 1: - raise TypeError, "argument deg should be an integer >= 1." + raise TypeError("argument deg should be an integer >= 1.") cdef Expression symbol = self.coerce_in(symb) if not is_a_symbol(symbol._gobj): - raise TypeError, "argument symb must be a symbol" + raise TypeError("argument symb must be a symbol") cdef GEx x sig_on() try: @@ -3464,7 +3466,7 @@ cdef class Expression(CommutativeRingElement): TESTS: - Check if #8943 is fixed:: + Check if :trac:`8943` is fixed:: sage: ((1+arctan(x))**(1/x)).series(x==0, 3) (e) + (-1/2*e)*x + (1/8*e)*x^2 + Order(x^3) @@ -3585,7 +3587,8 @@ cdef class Expression(CommutativeRingElement): TESTS: - Check that ticket #7472 is fixed (Taylor polynomial in more variables):: + Check that ticket :trac:`7472` is fixed (Taylor polynomial in + more variables):: sage: x,y=var('x y'); taylor(x*y^3,(x,1),(y,1),4) (x - 1)*(y - 1)^3 + 3*(x - 1)*(y - 1)^2 + (y - 1)^3 + 3*(x - 1)*(y - 1) + 3*(y - 1)^2 + x + 3*y - 3 @@ -3604,7 +3607,7 @@ cdef class Expression(CommutativeRingElement): B=[A[0],SR(A[1])] B.append(Integer(A[len(A)-1])) except Exception: - raise NotImplementedError, "Wrong arguments passed to taylor. See taylor? for more details." + raise NotImplementedError("Wrong arguments passed to taylor. See taylor? for more details.") l = self._maxima_().taylor(B) return self.parent()(l) @@ -3692,13 +3695,13 @@ cdef class Expression(CommutativeRingElement): """ if side is not None: if not is_a_relational(self._gobj): - raise ValueError, "expansion on sides only makes sense for relations" + raise ValueError("expansion on sides only makes sense for relations") if side == 'left': return self.operator()(self.lhs().expand(), self.rhs()) elif side == 'right': return self.operator()(self.lhs(), self.rhs().expand()) else: - raise ValueError, "side must be 'left', 'right', or None" + raise ValueError("side must be 'left', 'right', or None") cdef GEx x sig_on() @@ -3929,8 +3932,8 @@ cdef class Expression(CommutativeRingElement): """ Find all occurrences of the given pattern in this expression. - Note that once a subexpression matches the pattern, the search doesn't - extend to subexpressions of it. + Note that once a subexpression matches the pattern, the search does + not extend to subexpressions of it. EXAMPLES:: @@ -4103,7 +4106,7 @@ cdef class Expression(CommutativeRingElement): sage: 1/gamma(x).subs(x=-1) 0 - # verify that this operation does not modify the passed dictionary (#6622) + # verify that this operation does not modify the passed dictionary (:trac:`6622`) sage: var('v t') (v, t) sage: f = v*t @@ -4113,7 +4116,7 @@ cdef class Expression(CommutativeRingElement): sage: D {v: 2} - Check if #9891 is fixed:: + Check if :trac:`9891` is fixed:: sage: exp(x).subs(x=log(x)) x @@ -4132,7 +4135,7 @@ cdef class Expression(CommutativeRingElement): if isinstance(in_dict, Expression): return self._subs_expr(in_dict) if not isinstance(in_dict, dict): - raise TypeError, "subs takes either a set of keyword arguments, a dictionary, or a symbolic relational expression" + raise TypeError("subs takes either a set of keyword arguments, a dictionary, or a symbolic relational expression") sdict.update(in_dict) if kwds: @@ -4260,7 +4263,7 @@ cdef class Expression(CommutativeRingElement): equations = [ x == eq_dict[x] for x in eq_dict.keys() ] if not all([is_SymbolicEquation(eq) for eq in equations]): - raise TypeError, "each expression must be an equation" + raise TypeError("each expression must be an equation") d = dict([(eq.lhs(), eq.rhs()) for eq in equations]) return self.subs(d) @@ -4518,7 +4521,7 @@ cdef class Expression(CommutativeRingElement): elif o == greater_or_equal: return operator.ge else: - raise RuntimeError, "operator type not known, please report this as a bug" + raise RuntimeError("operator type not known, please report this as a bug") elif is_a_function(self._gobj): # get function id serial = ex_to_function(self._gobj).get_serial() @@ -4527,7 +4530,7 @@ cdef class Expression(CommutativeRingElement): # find the python equivalent and return it res = get_sfunction_from_serial(serial) if res is None: - raise RuntimeError, "cannot find SFunction in table" + raise RuntimeError("cannot find SFunction in table") if is_a_fderivative(self._gobj): from sage.symbolic.pynac import paramset_from_Expression @@ -4564,7 +4567,7 @@ cdef class Expression(CommutativeRingElement): sage: list((x^y*z*(x+y)).iterator()) [x + y, x^y, z] - Note that symbols, constants and numeric objects don't have operands, + Note that symbols, constants and numeric objects do not have operands, so the iterator function raises an error in these cases:: sage: x.iterator() @@ -4582,7 +4585,7 @@ cdef class Expression(CommutativeRingElement): """ if is_a_symbol(self._gobj) or is_a_constant(self._gobj) or \ is_a_numeric(self._gobj): - raise ValueError, "expressions containing only a numeric coefficient, constant or symbol have no operands" + raise ValueError("expressions containing only a numeric coefficient, constant or symbol have no operands") return new_ExpIter_from_Expression(self) property op: @@ -4612,7 +4615,7 @@ cdef class Expression(CommutativeRingElement): """ if is_a_symbol(self._gobj) or is_a_constant(self._gobj) or \ is_a_numeric(self._gobj): - raise TypeError, "expressions containing only a numeric coefficient, constant or symbol have no operands" + raise TypeError("expressions containing only a numeric coefficient, constant or symbol have no operands") cdef OperandsWrapper res = OperandsWrapper.__new__(OperandsWrapper) res._expr = self return res @@ -4662,7 +4665,7 @@ cdef class Expression(CommutativeRingElement): sage: t.n() +infinity - Some expressions can't be evaluated numerically:: + Some expressions cannot be evaluated numerically:: sage: n(sin(x)) Traceback (most recent call last): @@ -4675,7 +4678,7 @@ cdef class Expression(CommutativeRingElement): TypeError: cannot evaluate symbolic expression numerically Make sure we've rounded up log(10,2) enough to guarantee - sufficient precision (trac #10164):: + sufficient precision (:trac:`10164`):: sage: ks = 4*10**5, 10**6 sage: all(len(str(e.n(digits=k)))-1 >= k for k in ks) @@ -4745,14 +4748,14 @@ cdef class Expression(CommutativeRingElement): try: rif_self = sage.rings.all.RIF(self) except TypeError: - raise ValueError, "could not convert %s to a real number"%(self) + raise ValueError("could not convert %s to a real number" % self) half = 1 / sage.rings.integer.Integer(2) if rif_self < 0 or (rif_self.contains_zero() and self < 0): result = ceil(self - half) else: result = floor(self + half) if not isinstance(result, sage.rings.integer.Integer): - raise ValueError, "could not convert %s to a real number"%(self) + raise ValueError("could not convert %s to a real number" % self) else: return result @@ -4819,7 +4822,7 @@ cdef class Expression(CommutativeRingElement): else: R = CallableSymbolicExpressionRing(args, check=False) return R(self) - raise TypeError, "Must construct a function with a tuple (or list) of symbolic variables." + raise TypeError("Must construct a function with a tuple (or list) of symbolic variables.") ############################################################################ # Basic arithmetic wrappers @@ -4988,11 +4991,11 @@ cdef class Expression(CommutativeRingElement): sage: f.coeff(x*y, 2) Traceback (most recent call last): ... - TypeError: n <> 1 only allowed for s being a variable + TypeError: n != 1 only allowed for s being a variable """ cdef Expression ss = self.coerce_in(s) - if n <> 1 and not is_a_symbol(ss._gobj): - raise TypeError, "n <> 1 only allowed for s being a variable" + if n != 1 and not is_a_symbol(ss._gobj): + raise TypeError("n != 1 only allowed for s being a variable") # the following is a temporary fix for GiNaC bug #9505 if is_a_mul(ss._gobj): # necessarily n=1 here res = self @@ -5478,7 +5481,7 @@ cdef class Expression(CommutativeRingElement): TESTS: - This shows that the issue at trac #5755 is fixed (attempting to + This shows that the issue at :trac:`5755` is fixed (attempting to coerce a symbolic expression to a non-symbolic polynomial ring caused an error:: @@ -5492,7 +5495,7 @@ cdef class Expression(CommutativeRingElement): sage: RR['xx'](2.0*xx) 2.00000000000000*xx - This shows that the issue at trac #4246 is fixed (attempting to + This shows that the issue at :trac:`4246` is fixed (attempting to coerce an expression containing at least one variable that's not in `R` raises an error):: @@ -5544,7 +5547,7 @@ cdef class Expression(CommutativeRingElement): """ v = self.variables() if len(v) != 1: - raise ValueError, "self must be a polynomial in one variable but it is in the variables %s"%tuple([v]) + raise ValueError("self must be a polynomial in one variable but it is in the variables %s" % tuple([v])) f = self.polynomial(base_ring) from sage.rings.all import PowerSeriesRing R = PowerSeriesRing(base_ring, names=f.parent().variable_names()) @@ -5684,7 +5687,7 @@ cdef class Expression(CommutativeRingElement): x^2*y^2*z^2 + x*(4*y + z) + 20*y^2 + 21*y*z + 4*z^2 Here we do the same thing for `y` and `z`; however, note that - we don't factor the `y^{2}` and `z^{2}` terms before + we do not factor the `y^{2}` and `z^{2}` terms before collecting coefficients:: sage: f.collect(y) @@ -5742,12 +5745,23 @@ cdef class Expression(CommutativeRingElement): def collect_common_factors(self): """ + This function does not perform a full factorization but only + looks for factors which are already explicitly present. + EXAMPLES:: sage: var('x') x sage: (x/(x^2 + x)).collect_common_factors() 1/(x + 1) + + sage: var('a,b,c,x,y') + sage: (a*x+a*y).collect_common_factors() + a*(x + y) + sage: (a*x^2+2*a*x*y+a*y^2).collect_common_factors() + (x^2 + 2*x*y + y^2)*a + sage: (a*(b*(a+c)*x+b*((a+c)*x+(a+c)*y)*y)).collect_common_factors() + ((x + y)*y + x)*(a + c)*a*b """ cdef GEx x sig_on() @@ -6962,7 +6976,7 @@ cdef class Expression(CommutativeRingElement): TESTS: - Test if #6377 is fixed:: + Test if :trac:`6377` is fixed:: sage: SR(oo).exp() +Infinity @@ -7171,7 +7185,7 @@ cdef class Expression(CommutativeRingElement): TESTS: - Check if we handle zero correctly (#8561):: + Check if we handle zero correctly (:trac:`8561`):: sage: x.binomial(0) 1 @@ -7482,7 +7496,7 @@ cdef class Expression(CommutativeRingElement): else: power = oper.op(1) if not is_a_numeric(power): - raise TypeError, "self is not a rational expression" + raise TypeError("self is not a rational expression") elif ex_to_numeric(power).is_positive(): vec.push_back(oper) return new_Expression_from_GEx(self._parent, @@ -7566,7 +7580,7 @@ cdef class Expression(CommutativeRingElement): ex = oper.op(0) power = oper.op(1) if not is_a_numeric(power): - raise TypeError, "self is not a rational expression" + raise TypeError("self is not a rational expression") elif ex_to_numeric(power).is_negative(): vec.push_back(g_pow(ex, g_abs(power))) return new_Expression_from_GEx(self._parent, @@ -7655,7 +7669,7 @@ cdef class Expression(CommutativeRingElement): ex = oper.op(0) power = oper.op(1) if not is_a_numeric(power): - raise TypeError, "self is not a rational expression" + raise TypeError("self is not a rational expression") elif is_a_numeric(power): power_num = ex_to_numeric(power) if power_num.is_positive(): @@ -8141,7 +8155,7 @@ cdef class Expression(CommutativeRingElement): elif algorithm == 'noexpand': maxima_method = 'xthru' else: - raise NotImplementedError, "unknown algorithm, see the help for available algorithms" + raise NotImplementedError("unknown algorithm, see the help for available algorithms") P = self_m.parent() self_str=self_m.str() if map: @@ -8199,8 +8213,8 @@ cdef class Expression(CommutativeRingElement): TESTS: - Check that the problem with applying full_simplify() to gamma functions (Trac 9240) - has been fixed:: + Check that the problem with applying `full_simplify()` to gamma + functions (:trac:`9240`) has been fixed:: sage: gamma(1/3) gamma(1/3) @@ -8416,8 +8430,8 @@ cdef class Expression(CommutativeRingElement): TESTS: - This shows that the issue at trac #7334 is fixed. Maxima intentionally - keeps the expression inside the log factored:: + This shows that the issue at :trac:`7334` is fixed. Maxima + intentionally keeps the expression inside the log factored:: sage: log_expr = (log(sqrt(2)-1)+log(sqrt(2)+1)) sage: log_expr.simplify_log('all') @@ -8458,7 +8472,7 @@ cdef class Expression(CommutativeRingElement): elif algorithm == 'all': maxima.eval('logconfun(m):= true$') elif algorithm is not None: - raise NotImplementedError, "unknown algorithm, see the help for available algorithms" + raise NotImplementedError("unknown algorithm, see the help for available algorithms") res = self.parent()(self._maxima_().logcontract()) if algorithm is not None: maxima.eval('logconcoeffp:false$') @@ -8578,7 +8592,7 @@ cdef class Expression(CommutativeRingElement): elif algorithm == 'all': maxima_method='super' else: - raise NotImplementedError, "unknown algorithm, see the help for available algorithms" + raise NotImplementedError("unknown algorithm, see the help for available algorithms") maxima.eval('logexpand:%s'%maxima_method) res = self._maxima_() res = res.sage() @@ -8766,7 +8780,7 @@ cdef class Expression(CommutativeRingElement): sage: units.mass.kilogram.convert(units.mass.pound) 100000000/45359237*pound - We don't get anything new by converting an ordinary symbolic variable:: + We do not get anything new by converting an ordinary symbolic variable:: sage: a = var('a') sage: a - a.convert() @@ -8927,7 +8941,7 @@ cdef class Expression(CommutativeRingElement): sage: f.roots(explicit_solutions=False) [((2^(8/9) + x^(8/9) - 2^(1/9) - x^(1/9))/(2^(8/9) - 2^(1/9)), 1)] - Another example, but involving a degree 5 poly whose roots don't + Another example, but involving a degree 5 poly whose roots do not get computed explicitly:: sage: f = x^5 + x^3 + 17*x + 1 @@ -8940,7 +8954,7 @@ cdef class Expression(CommutativeRingElement): sage: f.roots(explicit_solutions=False, multiplicities=False) [x^5 + x^3 + 17*x + 1] - Now let's find some roots over different rings:: + Now let us find some roots over different rings:: sage: f.roots(ring=CC) [(-0.0588115223184..., 1), (-1.331099917875... - 1.52241655183732*I, 1), (-1.331099917875... + 1.52241655183732*I, 1), (1.36050567903502 - 1.51880872209965*I, 1), (1.36050567903502 + 1.51880872209965*I, 1)] @@ -8965,7 +8979,7 @@ cdef class Expression(CommutativeRingElement): ... TypeError: unable to convert sqrt(3) to a rational - Check if #9538 is fixed:: + Check if :trac:`9538` is fixed:: sage: var('f6,f5,f4,x') (f6, f5, f4, x) @@ -8983,7 +8997,7 @@ cdef class Expression(CommutativeRingElement): S, mul = self.solve(x, multiplicities=True, explicit_solutions=explicit_solutions) if len(mul) == 0 and explicit_solutions: - raise RuntimeError, "no explicit roots found" + raise RuntimeError("no explicit roots found") else: rt_muls = [(S[i].rhs(), mul[i]) for i in range(len(mul))] if multiplicities: @@ -9217,13 +9231,13 @@ cdef class Expression(CommutativeRingElement): try: return(solve_ineq([self])) # trying solve_ineq_fourier except Exception: - raise NotImplementedError, "solving only implemented for equalities and few special inequalities, see solve_ineq" + raise NotImplementedError("solving only implemented for equalities and few special inequalities, see solve_ineq") ex = self else: ex = (self == 0) if multiplicities and to_poly_solve: - raise NotImplementedError, "to_poly_solve does not return multiplicities" + raise NotImplementedError("to_poly_solve does not return multiplicities") # Take care of cases like solve([x^2-1], [x]) for consistency with # multiple variable input in sage.symbolic.relation.solve(). @@ -9243,7 +9257,7 @@ cdef class Expression(CommutativeRingElement): x = v[0] if not isinstance(x, Expression): - raise TypeError("%s is not a valid variable."%repr(x)) + raise TypeError("%s is not a valid variable." % repr(x)) m = ex._maxima_() P = m.parent() @@ -9448,7 +9462,7 @@ cdef class Expression(CommutativeRingElement): TESTS: Test the special case that failed for the first attempt to fix - #3980:: + :trac:`3980`:: sage: t = var('t') sage: find_root(1/t - x,0,2) @@ -9457,20 +9471,20 @@ cdef class Expression(CommutativeRingElement): NotImplementedError: root finding currently only implemented in 1 dimension. """ if is_a_relational(self._gobj) and self.operator() is not operator.eq: - raise ValueError, "Symbolic equation must be an equality." + raise ValueError("Symbolic equation must be an equality.") from sage.numerical.optimize import find_root if self.number_of_arguments() == 0: if bool(self == 0): return a else: - raise RuntimeError, "no zero in the interval, since constant expression is not 0." + raise RuntimeError("no zero in the interval, since constant expression is not 0.") elif self.number_of_arguments() == 1: f = self._fast_float_(self.default_variable()) return find_root(f, a=a, b=b, xtol=xtol, rtol=rtol,maxiter=maxiter, full_output=full_output) else: - raise NotImplementedError, "root finding currently only implemented in 1 dimension." + raise NotImplementedError("root finding currently only implemented in 1 dimension.") def find_local_maximum(self, a, b, var=None, tol=1.48e-08, maxfun=500): r""" @@ -9678,7 +9692,7 @@ cdef class Expression(CommutativeRingElement): if is_CallableSymbolicExpression(self): A = self.arguments() if len(A) == 0: - raise ValueError, "function has no input arguments" + raise ValueError("function has no input arguments") else: param = A[0] @@ -9842,7 +9856,7 @@ cdef class Expression(CommutativeRingElement): sage: (a*q^k).sum(k, 0, oo) -a/(q - 1) - A divergent geometric series. Don't forget + A divergent geometric series. Do not forget to forget your assumptions:: sage: forget() @@ -9867,7 +9881,7 @@ cdef class Expression(CommutativeRingElement): sage: (binomial(n,k)*x^k).sum(k, 0, n, algorithm = 'maple') # optional - maple (x + 1)^n - Check that the sum in #10682 is done right:: + Check that the sum in :trac:`10682` is done right:: sage: sum(binomial(n,k)*k^2, k, 2, n) 1/4*(n^2 + n)*2^n - n @@ -10036,7 +10050,7 @@ cdef class Expression(CommutativeRingElement): x^2 + y^2 + z^2 + I <= (I + 1) """ if not is_a_relational(self._gobj): - raise TypeError, "this expression must be a relation" + raise TypeError("this expression must be a relation") return self + x def subtract_from_both_sides(self, x): @@ -10053,7 +10067,7 @@ cdef class Expression(CommutativeRingElement): sqrt(3)*x*sin(x) + sqrt(2) - cos(sin(x)) > 0 """ if not is_a_relational(self._gobj): - raise TypeError, "this expression must be a relation" + raise TypeError("this expression must be a relation") return self - x def multiply_both_sides(self, x, checksign=None): @@ -10102,7 +10116,7 @@ cdef class Expression(CommutativeRingElement): -x^3 - 1 <= -2*sqrt(3) """ if not is_a_relational(self._gobj): - raise TypeError, "this expression must be a relation" + raise TypeError("this expression must be a relation") return self * x def divide_both_sides(self, x, checksign=None): @@ -10127,7 +10141,7 @@ cdef class Expression(CommutativeRingElement): (x^3 + theta)/theta < sin(theta*x)/theta """ if not is_a_relational(self._gobj): - raise TypeError, "this expression must be a relation" + raise TypeError("this expression must be a relation") return self / x @@ -10346,7 +10360,7 @@ cdef operators compatible_relation(operators lop, operators rop) except Date: Tue, 4 Mar 2014 13:10:20 +0100 Subject: [PATCH 2/3] trac #11840 details, making sure that tests pass --- src/sage/symbolic/expression.pyx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/sage/symbolic/expression.pyx b/src/sage/symbolic/expression.pyx index 7b0349e1931..25e90720841 100644 --- a/src/sage/symbolic/expression.pyx +++ b/src/sage/symbolic/expression.pyx @@ -5748,6 +5748,10 @@ cdef class Expression(CommutativeRingElement): This function does not perform a full factorization but only looks for factors which are already explicitly present. + Polynomials can often be brought into a more compact form by + collecting common factors from the terms of sums. This is + accomplished by this function. + EXAMPLES:: sage: var('x') @@ -5756,6 +5760,7 @@ cdef class Expression(CommutativeRingElement): 1/(x + 1) sage: var('a,b,c,x,y') + (a, b, c, x, y) sage: (a*x+a*y).collect_common_factors() a*(x + y) sage: (a*x^2+2*a*x*y+a*y^2).collect_common_factors() From 2b111097f96020a082f669ef426e999bd68597e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Tue, 4 Mar 2014 17:45:41 +0100 Subject: [PATCH 3/3] trac #11840 swapped two lines --- src/sage/symbolic/expression.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/symbolic/expression.pyx b/src/sage/symbolic/expression.pyx index 25e90720841..63441380001 100644 --- a/src/sage/symbolic/expression.pyx +++ b/src/sage/symbolic/expression.pyx @@ -152,8 +152,8 @@ from sage.misc.decorators import rename_keyword from sage.misc.superseded import deprecated_function_alias from sage.structure.dynamic_class import dynamic_class -LOG_TEN_TWO_PLUS_EPSILON = 3.321928094887363 # a small overestimate of log(10,2) +LOG_TEN_TWO_PLUS_EPSILON = 3.321928094887363 cpdef bint is_Expression(x): """