# Ticket #9880: trac_9880-fix_doctests_symbolic.take2.patch

File trac_9880-fix_doctests_symbolic.take2.patch, 35.3 KB (added by burcin, 9 years ago)
• ## sage/symbolic/expression.pyx

# HG changeset patch
# User Volker Braun <vbraun@stp.dias.ie>
# Date 1308366050 25200
diff --git a/sage/symbolic/expression.pyx b/sage/symbolic/expression.pyx
 a :: sage: eqn^2 (x - 1)^4 <= (x^2 - 2*x + 3)^2 (x - 1)^4 <= (-x^2 + 2*x - 3)^2 sage: eqn.expand() x^2 - 2*x + 1 <= x^2 - 2*x + 3 sage: var('a,b,c') (a, b, c) sage: expand((u + v + a + b + c)^2) a^2 + 2*a*b + 2*a*c + 2*a*u + 2*a*v + b^2 + 2*b*c + 2*b*u + 2*b*v + c^2 + 2*c*u + 2*c*v + u^2 + 2*u*v + v^2 a^2 + b^2 + 2*a*b + c^2 + 2*a*c + 2*b*c + u^2 + 2*a*u + 2*b*u + 2*c*u + v^2 + 2*a*v + 2*b*v + 2*c*v + 2*u*v TESTS: sage: t*u 1 sage: t + u e^sqrt(x) + e^(-sqrt(x)) e^(-sqrt(x)) + e^sqrt(x) sage: t e^sqrt(x) OUTPUT: The python object corresponding to this expression, assuming this expression is a single numerical value. Otherwise, a TypeError is raised. The Python object corresponding to this expression, assuming this expression is a single numerical value or an infinity representable in Python. Otherwise, a TypeError is raised. EXAMPLES:: sage: SR(I*oo).pyobject() Traceback (most recent call last): ... ValueError: Python infinity cannot have complex phase. TypeError: Python infinity cannot have complex phase. """ cdef GConstant* c if is_a_constant(self._gobj): sage: t = 2*x*y^z+3 sage: u = loads(dumps(t)) # indirect doctest sage: u 2*y^z*x + 3 2*x*y^z + 3 sage: bool(t == u) True sage: u.subs(x=z) EXAMPLES:: sage: (1+x)._dbgprint() sage: (1+x)._dbgprint()   # random output x + 1 """ self._gobj.dbgprint(); EXAMPLES:: sage: gap(e + pi^2 + x^3) pi^2 + x^3 + e x^3 + pi^2 + e """ return '"%s"'%repr(self) EXAMPLES:: sage: singular(e + pi^2 + x^3) pi^2 + x^3 + e x^3 + pi^2 + e """ return '"%s"'%repr(self) sage: x = var('x') sage: f = sin(cos(x^2) + log(x)) sage: f._magma_init_(magma) '"sin(log(x) + cos(x^2))"' '"sin(cos(x^2) + log(x))"' sage: magma(f)                         # optional - magma sin(log(x) + cos(x^2)) sage: magma(f).Type()                  # optional - magma sage: latex(y + 3*(x^(-1))) y + \frac{3}{x} sage: latex(x^(y+z^(1/y))) x^{z^{\left(\frac{1}{y}\right)} + y} x^{y + z^{\left(\frac{1}{y}\right)}} sage: latex(1/sqrt(x+y)) \frac{1}{\sqrt{x + y}} sage: latex(sin(x*(z+y)^x)) \sin\left({\left(y + z\right)}^{x} x\right) \sin\left(x {\left(y + z\right)}^{x}\right) sage: latex(3/2*(x+y)/z/y) \frac{3 \, {\left(x + y\right)}}{2 \, y z} sage: latex((2^(x^y))) sage: latex((x*y).conjugate()) \overline{x} \overline{y} sage: latex(x*(1/(x^2)+sqrt(x^7))) {\left(\sqrt{x^{7}} + \frac{1}{x^{2}}\right)} x x {\left(\sqrt{x^{7}} + \frac{1}{x^{2}}\right)} Check spacing of coefficients of mul expressions (#3202):: sage: latex((x+2)*(x+1)/(x^3+1)) \frac{{\left(x + 1\right)} {\left(x + 2\right)}}{x^{3} + 1} sage: latex((x+2)/(x^3+1)/(x+1)) \frac{x + 2}{{\left(x + 1\right)} {\left(x^{3} + 1\right)}} \frac{x + 2}{{\left(x^{3} + 1\right)} {\left(x + 1\right)}} Check that the sign is correct (#9086):: sage: x^3 -y == y + x x^3 - y == x + y sage: x^3 - y^10 >= y + x^10 x^3 - y^10 >= x^10 + y -y^10 + x^3 >= x^10 + y sage: x^2 > x x^2 > x """ sage: v,c = var('v,c') sage: assume(c != 0) sage: integral((1+v^2/c^2)^3/(1-v^2/c^2)^(3/2),v) -75/8*sqrt(c^2)*arcsin(sqrt(c^2)*v/c^2) - 17/8*v^3/(sqrt(-v^2/c^2 + 1)*c^2) - 1/4*v^5/(sqrt(-v^2/c^2 + 1)*c^4) + 83/8*v/sqrt(-v^2/c^2 + 1) -75/8*sqrt(c^2)*arcsin(sqrt(c^2)*v/c^2) - 1/4*v^5/(c^4*sqrt(-v^2/c^2 + 1)) - 17/8*v^3/(c^2*sqrt(-v^2/c^2 + 1)) + 83/8*v/sqrt(-v^2/c^2 + 1) sage: forget() """ from sage.symbolic.assumptions import _assumptions sage: nsr(oo) - nsr(oo) Traceback (most recent call last): ... RuntimeError: indeterminate expression: Infinity - Infinity encountered. RuntimeError: indeterminate expression: infinity - infinity encountered. sage: nsr(-oo) - nsr(-oo) Traceback (most recent call last): ... RuntimeError: indeterminate expression: Infinity - Infinity encountered. RuntimeError: indeterminate expression: infinity - infinity encountered. sage: nsr(unsigned_infinity) + nsr(oo) Traceback (most recent call last): ... RuntimeError: indeterminate expression: unsigned_infinity + x where x is Infinity, -Infinity or unsigned infinity encountered. RuntimeError: indeterminate expression: unsigned_infinity +- infinity encountered. sage: nsr(unsigned_infinity) - nsr(oo) Traceback (most recent call last): ... RuntimeError: indeterminate expression: unsigned_infinity + x where x is Infinity, -Infinity or unsigned infinity encountered. RuntimeError: indeterminate expression: unsigned_infinity +- infinity encountered. sage: nsr(oo) + nsr(unsigned_infinity) Traceback (most recent call last): ... RuntimeError: indeterminate expression: unsigned_infinity + x where x is Infinity, -Infinity or unsigned infinity encountered. RuntimeError: indeterminate expression: unsigned_infinity +- infinity encountered. sage: nsr(oo) - nsr(unsigned_infinity) Traceback (most recent call last): ... RuntimeError: indeterminate expression: unsigned_infinity + x where x is Infinity, -Infinity or unsigned infinity encountered. RuntimeError: indeterminate expression: unsigned_infinity +- infinity encountered. sage: nsr(unsigned_infinity) + nsr(unsigned_infinity) Traceback (most recent call last): ... RuntimeError: indeterminate expression: unsigned_infinity + x where x is Infinity, -Infinity or unsigned infinity encountered. Infinity """ cdef GEx x cdef Expression _right = right :: sage: x*oo +Infinity sage: -x*oo -Infinity Traceback (most recent call last): ... ArithmeticError: indeterminate expression: infinity * f(x) encountered. sage: x*unsigned_infinity Traceback (most recent call last): ... sage: x/oo 0 sage: oo/x +Infinity Traceback (most recent call last): ... RuntimeError: indeterminate expression: infinity * f(x) encountered. sage: SR(oo)/SR(oo) Traceback (most recent call last): ... RuntimeError: indeterminate expression: 0*infinity encountered. RuntimeError: indeterminate expression: 0 * infinity encountered. sage: SR(-oo)/SR(oo) Traceback (most recent call last): ... RuntimeError: indeterminate expression: 0*infinity encountered. RuntimeError: indeterminate expression: 0 * infinity encountered. sage: SR(oo)/SR(-oo) Traceback (most recent call last): ... RuntimeError: indeterminate expression: 0*infinity encountered. RuntimeError: indeterminate expression: 0 * infinity encountered. sage: SR(oo)/SR(unsigned_infinity) Traceback (most recent call last): ... RuntimeError: indeterminate expression: 0*infinity encountered. RuntimeError: indeterminate expression: 0 * infinity encountered. sage: SR(unsigned_infinity)/SR(oo) Traceback (most recent call last): ... RuntimeError: indeterminate expression: 0*infinity encountered. RuntimeError: indeterminate expression: 0 * infinity encountered. sage: SR(0)/SR(oo) 0 sage: a = sqrt(3) sage: b = x^2+1 sage: a.__cmp__(b)   # indirect doctest -1 1 """ return print_order_compare(left._gobj, (right)._gobj) sage: x^oo Traceback (most recent call last): ... RuntimeError: power::eval(): pow(x, Infinity) for non numeric x is not defined. RuntimeError: power::eval(): pow(f(x), infinity) is not defined. sage: SR(oo)^2 +Infinity sage: SR(-oo)^2 (x, y) sage: u = (sin(x) + cos(y))*(cos(x) - sin(y)) sage: derivative(u,x,y) sin(x)*sin(y) - cos(x)*cos(y) -cos(x)*cos(y) + sin(x)*sin(y) sage: f = ((x^2+1)/(x^2-1))^(1/4) sage: g = derivative(f, x); g # this is a complex expression 1/2*(x/(x^2 - 1) - (x^2 + 1)*x/(x^2 - 1)^2)/((x^2 + 1)/(x^2 - 1))^(3/4) sage: y = var('y') sage: f = y^(sin(x)) sage: derivative(f, x) y^sin(x)*log(y)*cos(x) y^sin(x)*cos(x)*log(y) :: sage: g = 1/(sqrt((x^2-1)*(x+5)^6)) sage: derivative(g, x) -((x + 5)^6*x + 3*(x + 5)^5*(x^2 - 1))/((x + 5)^6*(x^2 - 1))^(3/2) -((x + 5)^6*x + 3*(x^2 - 1)*(x + 5)^5)/((x^2 - 1)*(x + 5)^6)^(3/2) TESTS:: sage: g = f.series(x==1, 4); g (-sin(y) - 1) + (-2*sin(y) - 2)*(x - 1) + (-sin(y) + 3)*(x - 1)^2 + 1*(x - 1)^3 sage: h = g.truncate(); h -(sin(y) - 3)*(x - 1)^2 + (x - 1)^3 - 2*(sin(y) + 1)*(x - 1) - sin(y) - 1 (x - 1)^3 - (x - 1)^2*(-sin(y) - 3) - 2*(x - 1)*(-2*sin(y) + 1) - sin(y) - 1 sage: h.expand() x^3 - x^2*sin(y) - 5*x + 3 sage: f.series(x,7) 1*x^(-1) + (-1/6)*x + 1/120*x^3 + (-1/5040)*x^5 + Order(x^7) sage: f.series(x==1,3) (sin(1)) + (-2*sin(1) + cos(1))*(x - 1) + (5/2*sin(1) - 2*cos(1))*(x - 1)^2 + Order((x - 1)^3) (sin(1)) + (cos(1) - 2*sin(1))*(x - 1) + (-2*cos(1) + 5/2*sin(1))*(x - 1)^2 + Order((x - 1)^3) sage: f.series(x==1,3).truncate().expand() 5/2*x^2*sin(1) - 2*x^2*cos(1) - 7*x*sin(1) + 5*x*cos(1) + 11/2*sin(1) - 3*cos(1) -2*x^2*cos(1) + 5/2*x^2*sin(1) + 5*x*cos(1) - 7*x*sin(1) - 3*cos(1) + 11/2*sin(1) Following the GiNaC tutorial, we use John Machin's amazing formula \pi = 16 \tan^{-1}(1/5) - 4 \tan^{-1}(1/239) to compute sage: var('a, x, z') (a, x, z) sage: taylor(a*log(z), z, 2, 3) 1/24*(z - 2)^3*a - 1/8*(z - 2)^2*a + 1/2*(z - 2)*a + a*log(2) 1/24*a*(z - 2)^3 - 1/8*a*(z - 2)^2 + 1/2*a*(z - 2) + a*log(2) :: Ticket #7472 fixed (Taylor polynomial in more variables) :: sage: x,y=var('x y'); taylor(x*y^3,(x,1),(y,1),4) (y - 1)^3*(x - 1) + (y - 1)^3 + 3*(y - 1)^2*(x - 1) + 3*(y - 1)^2 + 3*(y - 1)*(x - 1) + x + 3*y - 3 (x - 1)*(y - 1)^3 + (y - 1)^3 + 3*(x - 1)*(y - 1)^2 + 3*(y - 1)^2 + 3*(x - 1)*(y - 1) + x + 3*y - 3 sage: expand(_) x*y^3 sage: f.series(x,7).truncate() -1/5040*x^5 + 1/120*x^3 - 1/6*x + 1/x sage: f.series(x==1,3).truncate().expand() 5/2*x^2*sin(1) - 2*x^2*cos(1) - 7*x*sin(1) + 5*x*cos(1) + 11/2*sin(1) - 3*cos(1) -2*x^2*cos(1) + 5/2*x^2*sin(1) + 5*x*cos(1) - 7*x*sin(1) - 3*cos(1) + 11/2*sin(1) """ if not is_a_series(self._gobj): return self sage: x,y = var('x,y') sage: a = (x-y)^5 sage: a.expand() x^5 - 5*x^4*y + 10*x^3*y^2 - 10*x^2*y^3 + 5*x*y^4 - y^5 x^5 - y^5 + 5*x*y^4 - 10*x^2*y^3 + 10*x^3*y^2 - 5*x^4*y sage: expand(a) x^5 - 5*x^4*y + 10*x^3*y^2 - 10*x^2*y^3 + 5*x*y^4 - y^5 x^5 - y^5 + 5*x*y^4 - 10*x^2*y^3 + 10*x^3*y^2 - 5*x^4*y We expand some other expressions:: sage: expand((x-1)^3/(y-1)) x^3/(y - 1) - 3*x^2/(y - 1) + 3*x/(y - 1) - 1/(y - 1) sage: expand((x+sin((x+y)^2))^2) x^2 + 2*x*sin((x + y)^2) + sin((x + y)^2)^2 x^2 + sin((x + y)^2)^2 + 2*x*sin((x + y)^2) We can expand individual sides of a relation:: sage: var('x,y') (x, y) sage: ((x + (2/3)*y)^3).expand() x^3 + 2*x^2*y + 4/3*x*y^2 + 8/27*y^3 x^3 + 8/27*y^3 + 4/3*x*y^2 + 2*x^2*y sage: expand( (x*sin(x) - cos(y)/x)^2 ) x^2*sin(x)^2 - 2*sin(x)*cos(y) + cos(y)^2/x^2 x^2*sin(x)^2 - 2*cos(y)*sin(x) + cos(y)^2/x^2 sage: f = (x-y)*(x+y); f (x - y)*(x + y) sage: f.expand() EXAMPLES:: sage: sin(5*x).expand_trig() sin(x)^5 - 10*sin(x)^3*cos(x)^2 + 5*sin(x)*cos(x)^4 sin(x)^5 - 10*cos(x)^2*sin(x)^3 + 5*cos(x)^4*sin(x) sage: cos(2*x + var('y')).expand_trig() -sin(2*x)*sin(y) + cos(2*x)*cos(y) cos(2*x)*cos(y) - sin(2*x)*sin(y) We illustrate various options to this function:: sage: f = sin(sin(3*cos(2*x))*x) sage: f.expand_trig() sin(-(sin(cos(2*x))^3 - 3*sin(cos(2*x))*cos(cos(2*x))^2)*x) sin(-(sin(cos(2*x))^3 - 3*cos(cos(2*x))^2*sin(cos(2*x)))*x) sage: f.expand_trig(full=True) sin(((sin(sin(x)^2)*cos(cos(x)^2) - sin(cos(x)^2)*cos(sin(x)^2))^3 - 3*(sin(sin(x)^2)*cos(cos(x)^2) - sin(cos(x)^2)*cos(sin(x)^2))*(sin(sin(x)^2)*sin(cos(x)^2) + cos(sin(x)^2)*cos(cos(x)^2))^2)*x) sin(-((cos(sin(x)^2)*sin(cos(x)^2) - cos(cos(x)^2)*sin(sin(x)^2))^3 - 3*(cos(cos(x)^2)*cos(sin(x)^2) + sin(cos(x)^2)*sin(sin(x)^2))^2*(cos(sin(x)^2)*sin(cos(x)^2) - cos(cos(x)^2)*sin(sin(x)^2)))*x) sage: sin(2*x).expand_trig(times=False) sin(2*x) sage: sin(2*x).expand_trig(times=True) 2*sin(x)*cos(x) 2*cos(x)*sin(x) sage: sin(2 + x).expand_trig(plus=False) sin(x + 2) sage: sin(2 + x).expand_trig(plus=True) sin(2)*cos(x) + sin(x)*cos(2) cos(x)*sin(2) + cos(2)*sin(x) sage: sin(x/2).expand_trig(half_angles=False) sin(1/2*x) sage: sin(x/2).expand_trig(half_angles=True) 1/2*sqrt(-cos(x) + 1)*sqrt(2)*(-1)^floor(1/2*x/pi) 1/2*sqrt(2)*(-1)^floor(1/2*x/pi)*sqrt(-cos(x) + 1) ALIASES: sage: y=var('y') sage: f=sin(x)*cos(x)^3+sin(y)^2 sage: f.reduce_trig() 1/4*sin(2*x) + 1/8*sin(4*x) - 1/2*cos(2*y) + 1/2 -1/2*cos(2*y) + 1/4*sin(2*x) + 1/8*sin(4*x) + 1/2 To reduce only the expressions involving x we use optional parameter:: {$0: x + y} sage: t = ((a+b)*(a+c)).match((a+w0)*(a+w1)) sage: t[w0], t[w1] (b, c) (c, b) sage: ((a+b)*(a+c)).match((w0+b)*(w0+c)) {$0: a} sage: print ((a+b)*(a+c)).match((w0+w1)*(w0+w2))    # surprising? None sage: print ((a+b)*(a+c)).match((w0+w1)*(w0+w2)) {$2: b,$0: a, \$1: c} sage: t = (a*(x+y)+a*z+b).match(a*w0+w1) sage: t[w0], t[w1] (x + y, a*z + b) (z, a*(x + y) + b) sage: print (a+b+c+d+e+f).match(c) None sage: (a+b+c+d+e+f).has(c) (x + y)^3 + b^2 + c sage: t.subs({w0^2: w0^3}) (x + y)^3 + a^3 + b^3 a^3 + b^3 + (x + y)^3 # substitute with a relational expression sage: t.subs(w0^2 == w0^3) (x + y)^3 + a^3 + b^3 a^3 + b^3 + (x + y)^3 sage: t.subs(w0==w0^2) (x^2 + y^2)^18 + a^16 + b^16 sage: (x/y).subs(y=oo) 0 sage: (x/y).subs(x=oo) +Infinity sage: (x*y).subs(x=oo) +Infinity Traceback (most recent call last): ... RuntimeError: indeterminate expression: infinity * f(x) encountered. sage: (x^y).subs(x=oo) Traceback (most recent call last): ... RuntimeError: power::eval(): pow(Infinity, x) for non numeric x is not defined. RuntimeError: power::eval(): pow(Infinity, f(x)) is not defined. sage: (x^y).subs(y=oo) Traceback (most recent call last): ... RuntimeError: power::eval(): pow(x, Infinity) for non numeric x is not defined. RuntimeError: power::eval(): pow(f(x), infinity) is not defined. sage: (x+y).subs(x=oo) +Infinity sage: (x-y).subs(y=oo) (x, y, z, a, b, c, d, e, f) sage: w0 = SR.wild(0); w1 = SR.wild(1) sage: (a^2 + b^2 + (x+y)^2)._subs_expr(w0^2 == w0^3) (x + y)^3 + a^3 + b^3 a^3 + b^3 + (x + y)^3 sage: (a^4 + b^4 + (x+y)^4)._subs_expr(w0^2 == w0^3) (x + y)^4 + a^4 + b^4 a^4 + b^4 + (x + y)^4 sage: (a^2 + b^4 + (x+y)^4)._subs_expr(w0^2 == w0^3) (x + y)^4 + a^3 + b^4 b^4 + (x + y)^4 + a^3 sage: ((a+b+c)^2)._subs_expr(a+b == x) (a + b + c)^2 sage: ((a+b+c)^2)._subs_expr(a+b+w0 == x+w0) sage: (sin(x)^2 + cos(x)^2)._subs_expr(sin(w0)^2+cos(w0)^2==1) 1 sage: (1 + sin(x)^2 + cos(x)^2)._subs_expr(sin(w0)^2+cos(w0)^2==1) sin(x)^2 + cos(x)^2 + 1 cos(x)^2 + sin(x)^2 + 1 sage: (17*x + sin(x)^2 + cos(x)^2)._subs_expr(w1 + sin(w0)^2+cos(w0)^2 == w1 + 1) 17*x + 1 sage: ((x-1)*(sin(x)^2 + cos(x)^2)^2)._subs_expr(sin(w0)^2+cos(w0)^2 == 1) x^4 + x sage: f = cos(x^2) + sin(x^2) sage: f.subs_expr(x^2 == x) sin(x) + cos(x) cos(x) + sin(x) :: sage: f(x,y,t) = cos(x) + sin(y) + x^2 + y^2 + t sage: f.subs_expr(y^2 == t) (x, y, t) |--> x^2 + 2*t + sin(y) + cos(x) (x, y, t) |--> x^2 + 2*t + cos(x) + sin(y) The following seems really weird, but it *is* what Maple does:: sage: f.subs_expr(x^2 + y^2 == t) (x, y, t) |--> x^2 + y^2 + t + sin(y) + cos(x) (x, y, t) |--> x^2 + y^2 + t + cos(x) + sin(y) sage: maple.eval('subs(x^2 + y^2 = t, cos(x) + sin(y) + x^2 + y^2 + t)')          # optional requires maple 'cos(x)+sin(y)+x^2+y^2+t' sage: maxima.quit() sage: var('x,y,z') (x, y, z) sage: (x+y)(x=z^2, y=x^y) x^y + z^2 z^2 + x^y """ return self._parent._call_element_(self, *args, **kwds) sage: var('a,b,c,x,y') (a, b, c, x, y) sage: (a^2 + b^2 + (x+y)^2).operands() [(x + y)^2, a^2, b^2] [a^2, b^2, (x + y)^2] sage: (a^2).operands() [a, 2] sage: (a*b^2*c).operands() sage: x.add(x, hold=True) x + x sage: x.add(x, (2+x), hold=True) x + x + (x + 2) (x + 2) + x + x sage: x.add(x, (2+x), x, hold=True) x + x + (x + 2) + x (x + 2) + x + x + x sage: x.add(x, (2+x), x, 2*x, hold=True) x + x + (x + 2) + x + 2*x (x + 2) + 2*x + x + x + x To then evaluate again, we currently must use Maxima via :meth:simplify:: sage: x.mul(x, hold=True) x*x sage: x.mul(x, (2+x), hold=True) x*x*(x + 2) (x + 2)*x*x sage: x.mul(x, (2+x), x, hold=True) x*x*(x + 2)*x (x + 2)*x*x*x sage: x.mul(x, (2+x), x, 2*x, hold=True) x*x*(x + 2)*x*(2*x) (2*x)*(x + 2)*x*x*x To then evaluate again, we currently must use Maxima via :meth:simplify:: sage: f.coefficient(sin(x*y)) x^3 + 2/x sage: f.collect(sin(x*y)) (x^3 + 2/x)*sin(x*y) + a*x + x*y + x/y + 100 a*x + x*y + (x^3 + 2/x)*sin(x*y) + x/y + 100 sage: var('a, x, y, z') (a, x, y, z) sage: bool(p.poly(a) == (x-a*sqrt(2))^2 + x + 1) True sage: p.poly(x) -(2*sqrt(2)*a - 1)*x + 2*a^2 + x^2 + 1 2*a^2 + x^2 - (-2*sqrt(2)*a - 1)*x + 1 """ from sage.symbolic.ring import SR f = self._maxima_() sage: R = SR[x] sage: a = R(sqrt(2) + x^3 + y) sage: a y + sqrt(2) + x^3 x^3 + y + sqrt(2) sage: type(a) sage: a.degree() (x, y, z) sage: f = 4*x*y + x*z + 20*y^2 + 21*y*z + 4*z^2 + x^2*y^2*z^2 sage: f.collect(x) x^2*y^2*z^2 + (4*y + z)*x + 20*y^2 + 21*y*z + 4*z^2 x^2*y^2*z^2 + 20*y^2 + x*(4*y + z) + 4*z^2 + 21*y*z sage: f.collect(y) (x^2*z^2 + 20)*y^2 + (4*x + 21*z)*y + x*z + 4*z^2 (x^2*z^2 + 20)*y^2 + (4*x + 21*z)*y + 4*z^2 + x*z sage: f.collect(z) (x^2*y^2 + 4)*z^2 + (x + 21*y)*z + 4*x*y + 20*y^2 (x^2*y^2 + 4)*z^2 + 20*y^2 + 4*x*y + (x + 21*y)*z """ cdef Expression s0 = self.coerce_in(s) cdef GEx x (a, b) sage: f = log(a + b*I) sage: f.imag_part() arctan2(real_part(b) + imag_part(a), real_part(a) - imag_part(b)) arctan2(imag_part(a) + real_part(b), -imag_part(b) + real_part(a)) Using the hold parameter it is possible to prevent automatic evaluation:: sage: SR(1).arctan2(CDF(0,1)) arctan2(1, 1.0*I) sage: arctan2(0,oo) 0 sage: SR(oo).arctan2(oo) Traceback (most recent call last): ... RuntimeError: arctan2_eval(): arctan2(infinity, infinity) encountered 1/4*pi sage: SR(oo).arctan2(0) 0 1/2*pi sage: SR(-oo).arctan2(0) -1/2*pi sage: SR(-oo).arctan2(-2) pi sage: SR(-oo).arctan2(-2) -pi sage: SR(unsigned_infinity).arctan2(2) Traceback (most recent call last): ... RuntimeError: arctan2_eval(): arctan2(unsigned_infinity, x) encountered RuntimeError: arctan2_eval(): arctan2(x, unsigned_infinity) encountered sage: SR(2).arctan2(oo) 1/2*pi sage: SR(2).arctan2(-oo) sage: SR(2).arctan2(SR(unsigned_infinity)) Traceback (most recent call last): ... RuntimeError: arctan2_eval(): arctan2(x, unsigned_infinity) encountered RuntimeError: arctan2_eval(): arctan2(unsigned_infinity, x) encountered """ cdef Expression nexp = self.coerce_in(x) return new_Expression_from_GEx(self._parent, sage: x.factorial() factorial(x) sage: (x^2+y^3).factorial() factorial(x^2 + y^3) factorial(y^3 + x^2) To prevent automatic evaluation use the hold argument:: EXAMPLES:: sage: f = sin(x)^2 + cos(x)^2; f sin(x)^2 + cos(x)^2 cos(x)^2 + sin(x)^2 sage: f.simplify() sin(x)^2 + cos(x)^2 cos(x)^2 + sin(x)^2 sage: f.simplify_trig() 1 sage: h = sin(x)*csc(x) sage: f=tan(3*x) sage: f.simplify_trig() (4*cos(x)^2 - 1)*sin(x)/(4*cos(x)^3 - 3*cos(x)) -(4*cos(x)^2 - 1)*sin(x)/(-4*cos(x)^3 + 3*cos(x)) sage: f.simplify_trig(False) sin(3*x)/cos(3*x) sage: y = var('y') sage: g = (x^(y/2) + 1)^2*(x^(y/2) - 1)^2/(x^y - 1) sage: g.simplify_rational(algorithm='simple') -(2*x^y - x^(2*y) - 1)/(x^y - 1) (x^(2*y) - 2*x^y + 1)/(x^y - 1) sage: g.simplify_rational() x^y - 1 sage: f.simplify_rational() (2*x^2 + 5*x + 4)/(x^3 + 5*x^2 + 8*x + 4) sage: f.simplify_rational(algorithm='noexpand') ((x + 1)*x + (x + 2)^2)/((x + 1)*(x + 2)^2) ((x + 2)^2 + (x + 1)*x)/((x + 1)*(x + 2)^2) """ self_m = self._maxima_() if algorithm == 'full': :: sage: f = binomial(n, k)*factorial(k)*factorial(n-k); f factorial(-k + n)*factorial(k)*binomial(n, k) binomial(n, k)*factorial(-k + n)*factorial(k) sage: f.simplify_factorial() factorial(n) sage: f = log(x)+log(y)-1/3*log((x+1)) sage: f.simplify_log() -1/3*log(x + 1) + log(x*y) log(x*y) - 1/3*log(x + 1) sage: f.simplify_log('ratios') log(x*y/(x + 1)^(1/3)) sage: x,y,z = var('x, y, z') sage: (x^3-y^3).factor() (x - y)*(x^2 + x*y + y^2) (x^2 + y^2 + x*y)*(x - y) sage: factor(-8*y - 4*x + z^2*(2*y + x)) (z - 2)*(z + 2)*(x + 2*y) (x + 2*y)*(z - 2)*(z + 2) sage: f = -1 - 2*x - x^2 + y^2 + 2*x*y^2 + x^2*y^2 sage: F = factor(f/(36*(1 + 2*y + y^2)), dontfactor=[x]); F 1/36*(y - 1)*(x^2 + 2*x + 1)/(y + 1) 1/36*(x^2 + 2*x + 1)*(y - 1)/(y + 1) If you are factoring a polynomial with rational coefficients (and dontfactor is empty) the factorization is done using Singular sage: var('x,y') (x, y) sage: (x^99 + y^99).factor() (x + y)*(x^2 - x*y + y^2)*(x^6 - x^3*y^3 + y^6)*... (x^60 + y^60 + x^3*y^57 - x^9*y^51 - x^12*y^48 + x^18*y^42 + x^21*y^39 - x^27*y^33 - x^30*y^30 - x^33*y^27 + x^39*y^21 + x^42*y^18 - x^48*y^12 - x^51*y^9 + x^57*y^3)*(x^20 + y^20 + x*y^19 - x^3*y^17 - x^4*y^16 + x^6*y^14 + x^7*y^13 - x^9*y^11 - x^10*y^10 - x^11*y^9 + x^13*y^7 + x^14*y^6 - x^16*y^4 - x^17*y^3 + x^19*y)*(x^10 + y^10 - x*y^9 + x^2*y^8 - x^3*y^7 + x^4*y^6 - x^5*y^5 + x^6*y^4 - x^7*y^3 + x^8*y^2 - x^9*y)*(x^6 + y^6 - x^3*y^3)*(x^2 + y^2 - x*y)*(x + y) """ from sage.calculus.calculus import symbolic_expression_from_maxima_string, symbolic_expression_from_string if len(dontfactor) > 0: (x, y, z) sage: f = x^3-y^3 sage: f.factor() (x - y)*(x^2 + x*y + y^2) (x^2 + y^2 + x*y)*(x - y) Notice that the -1 factor is separated out:: sage: f.factor_list() [(x - y, 1), (x^2 + x*y + y^2, 1)] [(x^2 + y^2 + x*y, 1), (x - y, 1)] We factor a fairly straightforward expression:: sage: factor(-8*y - 4*x + z^2*(2*y + x)).factor_list() [(z - 2, 1), (z + 2, 1), (x + 2*y, 1)] [(x + 2*y, 1), (z - 2, 1), (z + 2, 1)] A more complicated example:: (x, u, v) sage: f = expand((2*u*v^2-v^2-4*u^3)^2 * (-u)^3 * (x-sin(x))^3) sage: f.factor() -(x - sin(x))^3*(4*u^3 - 2*u*v^2 + v^2)^2*u^3 (-4*u^3 + 2*u*v^2 - v^2)^2*u^3*(-x + sin(x))^3 sage: g = f.factor_list(); g [(x - sin(x), 3), (4*u^3 - 2*u*v^2 + v^2, 2), (u, 3), (-1, 1)] [(4*u^3 - 2*u*v^2 + v^2, 2), (u, 3), (x - sin(x), 3), (-1, 1)] This function also works for quotients:: sage: g = f/(36*(1 + 2*y + y^2)); g 1/36*(x^2*y^2 + 2*x*y^2 - x^2 + y^2 - 2*x - 1)/(y^2 + 2*y + 1) sage: g.factor(dontfactor=[x]) 1/36*(y - 1)*(x^2 + 2*x + 1)/(y + 1) 1/36*(x^2 + 2*x + 1)*(y - 1)/(y + 1) sage: g.factor_list(dontfactor=[x]) [(y - 1, 1), (y + 1, -1), (x^2 + 2*x + 1, 1), (1/36, 1)] [(x^2 + 2*x + 1, 1), (y - 1, 1), (y + 1, -1), (1/36, 1)] This example also illustrates that the exponents do not have to be integers:: EXAMPLES:: sage: g = factor(x^3 - 1); g (x - 1)*(x^2 + x + 1) (x^2 + x + 1)*(x - 1) sage: v = g._factor_list(); v [(x - 1, 1), (x^2 + x + 1, 1)] [(x^2 + x + 1, 1), (x - 1, 1)] sage: type(v) """ A complicated example:: sage: f = expand((x^2 - 1)^3*(x^2 + 1)*(x-a)); f -a*x^8 + x^9 + 2*a*x^6 - 2*x^7 - 2*a*x^2 + 2*x^3 + a - x x^9 - a*x^8 - 2*x^7 + 2*a*x^6 + 2*x^3 - 2*a*x^2 + a - x The default variable is a, since it is the first in alphabetical order:: sage: var('a,b,c,x') (a, b, c, x) sage: (a*x^2 + b*x + c).roots(x) [(-1/2*(b + sqrt(-4*a*c + b^2))/a, 1), (-1/2*(b - sqrt(-4*a*c + b^2))/a, 1)] [(-1/2*(b + sqrt(b^2 - 4*a*c))/a, 1), (-1/2*(b - sqrt(b^2 - 4*a*c))/a, 1)] By default, all the roots are required to be explicit rather than implicit. To get implicit roots, pass explicit_solutions=False ... RuntimeError: no explicit roots found sage: f.roots(explicit_solutions=False) [((2^(8/9) - 2^(1/9) + x^(8/9) - x^(1/9))/(2^(8/9) - 2^(1/9)), 1)] [((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 get computed explicitly:: (f6, f5, f4, x) sage: e=15*f6*x^2 + 5*f5*x + f4 sage: res = e.roots(x); res [(-1/30*(sqrt(-12*f4*f6 + 5*f5^2)*sqrt(5) + 5*f5)/f6, 1), (1/30*(sqrt(-12*f4*f6 + 5*f5^2)*sqrt(5) - 5*f5)/f6, 1)] [(-1/30*(sqrt(5)*sqrt(5*f5^2 - 12*f4*f6) + 5*f5)/f6, 1), (1/30*(sqrt(5)*sqrt(5*f5^2 - 12*f4*f6) - 5*f5)/f6, 1)] sage: e.subs(x=res[0][0]).is_zero() True """ :: sage: (k * binomial(n, k)).sum(k, 1, n) n*2^(n - 1) 2^(n - 1)*n :: sage: f*(-2/3) -2/3*x - 2 < -2/3*y + 4/3 sage: f*(-pi) -(x + 3)*pi < -(y - 2)*pi -pi*(x + 3) < -pi*(y - 2) Since the direction of the inequality never changes when doing arithmetic with equations, you can multiply or divide the
diff --git a/sage/symbolic/random_tests.py b/sage/symbolic/random_tests.py