# HG changeset patch
# User Volker Braun <vbraun@stp.dias.ie>
# Date 1308366050 25200
# Node ID 2e244ed82263ad4c616f67d41ac20b5dfcf33cb0
# Parent c170907c5514354976c7c5fea3bfe779b9bf375b
Trac #9880: Fix doctests in expression.pyx and random_tests.py
diff git a/sage/symbolic/expression.pyx b/sage/symbolic/expression.pyx
a

b


15  15  :: 
16  16  
17  17  sage: eqn^2 
18   (x  1)^4 <= (x^2  2*x + 3)^2 
 18  (x  1)^4 <= (x^2 + 2*x  3)^2 
19  19  sage: eqn.expand() 
20  20  x^2  2*x + 1 <= x^2  2*x + 3 
21  21  
… 
… 

78  78  sage: var('a,b,c') 
79  79  (a, b, c) 
80  80  sage: expand((u + v + a + b + c)^2) 
81   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 
 81  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 
82  82  
83  83  TESTS: 
84  84  
… 
… 

108  108  sage: t*u 
109  109  1 
110  110  sage: t + u 
111   e^sqrt(x) + e^(sqrt(x)) 
 111  e^(sqrt(x)) + e^sqrt(x) 
112  112  sage: t 
113  113  e^sqrt(x) 
114  114  
… 
… 

365  365  sage: t = 2*x*y^z+3 
366  366  sage: u = loads(dumps(t)) # indirect doctest 
367  367  sage: u 
368   2*y^z*x + 3 
 368  2*x*y^z + 3 
369  369  sage: bool(t == u) 
370  370  True 
371  371  sage: u.subs(x=z) 
… 
… 

567  567  EXAMPLES:: 
568  568  
569  569  sage: gap(e + pi^2 + x^3) 
570   pi^2 + x^3 + e 
 570  x^3 + pi^2 + e 
571  571  """ 
572  572  return '"%s"'%repr(self) 
573  573  
… 
… 

579  579  EXAMPLES:: 
580  580  
581  581  sage: singular(e + pi^2 + x^3) 
582   pi^2 + x^3 + e 
 582  x^3 + pi^2 + e 
583  583  """ 
584  584  return '"%s"'%repr(self) 
585  585  
… 
… 

595  595  sage: x = var('x') 
596  596  sage: f = sin(cos(x^2) + log(x)) 
597  597  sage: f._magma_init_(magma) 
598   '"sin(log(x) + cos(x^2))"' 
 598  '"sin(cos(x^2) + log(x))"' 
599  599  sage: magma(f) # optional  magma 
600  600  sin(log(x) + cos(x^2)) 
601  601  sage: magma(f).Type() # optional  magma 
… 
… 

614  614  sage: latex(y + 3*(x^(1))) 
615  615  y + \frac{3}{x} 
616  616  sage: latex(x^(y+z^(1/y))) 
617   x^{z^{\left(\frac{1}{y}\right)} + y} 
 617  x^{y + z^{\left(\frac{1}{y}\right)}} 
618  618  sage: latex(1/sqrt(x+y)) 
619  619  \frac{1}{\sqrt{x + y}} 
620  620  sage: latex(sin(x*(z+y)^x)) 
621   \sin\left({\left(y + z\right)}^{x} x\right) 
 621  \sin\left(x {\left(y + z\right)}^{x}\right) 
622  622  sage: latex(3/2*(x+y)/z/y) 
623  623  \frac{3 \, {\left(x + y\right)}}{2 \, y z} 
624  624  sage: latex((2^(x^y))) 
… 
… 

628  628  sage: latex((x*y).conjugate()) 
629  629  \overline{x} \overline{y} 
630  630  sage: latex(x*(1/(x^2)+sqrt(x^7))) 
631   {\left(\sqrt{x^{7}} + \frac{1}{x^{2}}\right)} x 
 631  x {\left(\sqrt{x^{7}} + \frac{1}{x^{2}}\right)} 
632  632  
633  633  Check spacing of coefficients of mul expressions (#3202):: 
634  634  
… 
… 

693  693  sage: latex((x+2)*(x+1)/(x^3+1)) 
694  694  \frac{{\left(x + 1\right)} {\left(x + 2\right)}}{x^{3} + 1} 
695  695  sage: latex((x+2)/(x^3+1)/(x+1)) 
696   \frac{x + 2}{{\left(x + 1\right)} {\left(x^{3} + 1\right)}} 
 696  \frac{x + 2}{{\left(x^{3} + 1\right)} {\left(x + 1\right)}} 
697  697  
698  698  Check that the sign is correct (#9086):: 
699  699  
… 
… 

1274  1274  sage: x^3 y == y + x 
1275  1275  x^3  y == x + y 
1276  1276  sage: x^3  y^10 >= y + x^10 
1277   x^3  y^10 >= x^10 + y 
 1277  y^10 + x^3 >= x^10 + y 
1278  1278  sage: x^2 > x 
1279  1279  x^2 > x 
1280  1280  
… 
… 

1429  1429  sage: v,c = var('v,c') 
1430  1430  sage: assume(c != 0) 
1431  1431  sage: integral((1+v^2/c^2)^3/(1v^2/c^2)^(3/2),v) 
1432   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*arcsin(v/(c^2*sqrt(c^(2))))/sqrt(c^(2)) 
 1432  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)  75/8*arcsin(v/(c^2*sqrt(c^(2))))/sqrt(c^(2)) 
1433  1433  sage: forget() 
1434  1434  """ 
1435  1435  from sage.symbolic.assumptions import _assumptions 
… 
… 

2832  2832  sage: a = sqrt(3) 
2833  2833  sage: b = x^2+1 
2834  2834  sage: a.__cmp__(b) # indirect doctest 
2835   1 
 2835  1 
2836  2836  """ 
2837  2837  return print_order_compare(left._gobj, (<Expression>right)._gobj) 
2838  2838  
… 
… 

3076  3076  (x, y) 
3077  3077  sage: u = (sin(x) + cos(y))*(cos(x)  sin(y)) 
3078  3078  sage: derivative(u,x,y) 
3079   sin(x)*sin(y)  cos(x)*cos(y) 
 3079  cos(x)*cos(y) + sin(x)*sin(y) 
3080  3080  sage: f = ((x^2+1)/(x^21))^(1/4) 
3081  3081  sage: g = derivative(f, x); g # this is a complex expression 
3082  3082  1/2*(x/(x^2  1)  (x^2 + 1)*x/(x^2  1)^2)/((x^2 + 1)/(x^2  1))^(3/4) 
… 
… 

3088  3088  sage: y = var('y') 
3089  3089  sage: f = y^(sin(x)) 
3090  3090  sage: derivative(f, x) 
3091   y^sin(x)*log(y)*cos(x) 
 3091  y^sin(x)*cos(x)*log(y) 
3092  3092  
3093  3093  :: 
3094  3094  
… 
… 

3106  3106  
3107  3107  sage: g = 1/(sqrt((x^21)*(x+5)^6)) 
3108  3108  sage: derivative(g, x) 
3109   ((x + 5)^6*x + 3*(x + 5)^5*(x^2  1))/((x + 5)^6*(x^2  1))^(3/2) 
 3109  ((x + 5)^6*x + 3*(x^2  1)*(x + 5)^5)/((x^2  1)*(x + 5)^6)^(3/2) 
3110  3110  
3111  3111  TESTS:: 
3112  3112  
… 
… 

3280  3280  sage: g = f.series(x==1, 4); g 
3281  3281  (sin(y)  1) + (2*sin(y)  2)*(x  1) + (sin(y) + 3)*(x  1)^2 + 1*(x  1)^3 
3282  3282  sage: h = g.truncate(); h 
3283   (sin(y)  3)*(x  1)^2 + (x  1)^3  2*(sin(y) + 1)*(x  1)  sin(y)  1 
 3283  (x  1)^3  (x  1)^2*(sin(y)  3)  2*(x  1)*(2*sin(y) + 1)  sin(y)  1 
3284  3284  sage: h.expand() 
3285  3285  x^3  x^2*sin(y)  5*x + 3 
3286  3286  
… 
… 

3290  3290  sage: f.series(x,7) 
3291  3291  1*x^(1) + (1/6)*x + 1/120*x^3 + (1/5040)*x^5 + Order(x^7) 
3292  3292  sage: f.series(x==1,3) 
3293   (sin(1)) + (2*sin(1) + cos(1))*(x  1) + (5/2*sin(1)  2*cos(1))*(x  1)^2 + Order((x  1)^3) 
 3293  (sin(1)) + (cos(1)  2*sin(1))*(x  1) + (2*cos(1) + 5/2*sin(1))*(x  1)^2 + Order((x  1)^3) 
3294  3294  sage: f.series(x==1,3).truncate().expand() 
3295   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) 
 3295  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) 
3296  3296  
3297  3297  Following the GiNaC tutorial, we use John Machin's amazing 
3298  3298  formula `\pi = 16 \tan^{1}(1/5)  4 \tan^{1}(1/239)` to compute 
… 
… 

3343  3343  sage: var('a, x, z') 
3344  3344  (a, x, z) 
3345  3345  sage: taylor(a*log(z), z, 2, 3) 
3346   1/24*(z  2)^3*a  1/8*(z  2)^2*a + 1/2*(z  2)*a + a*log(2) 
 3346  1/24*a*(z  2)^3  1/8*a*(z  2)^2 + 1/2*a*(z  2) + a*log(2) 
3347  3347  
3348  3348  :: 
3349  3349  
… 
… 

3380  3380  Check that ticket #7472 is fixed (Taylor polynomial in more variables):: 
3381  3381  
3382  3382  sage: x,y=var('x y'); taylor(x*y^3,(x,1),(y,1),4) 
3383   (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 
 3383  (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 
3384  3384  sage: expand(_) 
3385  3385  x*y^3 
3386  3386  
… 
… 

3425  3425  sage: f.series(x,7).truncate() 
3426  3426  1/5040*x^5 + 1/120*x^3  1/6*x + 1/x 
3427  3427  sage: f.series(x==1,3).truncate().expand() 
3428   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) 
 3428  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) 
3429  3429  """ 
3430  3430  if not is_a_series(self._gobj): 
3431  3431  return self 
… 
… 

3448  3448  sage: x,y = var('x,y') 
3449  3449  sage: a = (xy)^5 
3450  3450  sage: a.expand() 
3451   x^5  5*x^4*y + 10*x^3*y^2  10*x^2*y^3 + 5*x*y^4  y^5 
 3451  x^5  y^5 + 5*x*y^4  10*x^2*y^3 + 10*x^3*y^2  5*x^4*y 
3452  3452  sage: expand(a) 
3453   x^5  5*x^4*y + 10*x^3*y^2  10*x^2*y^3 + 5*x*y^4  y^5 
 3453  x^5  y^5 + 5*x*y^4  10*x^2*y^3 + 10*x^3*y^2  5*x^4*y 
3454  3454  
3455  3455  We expand some other expressions:: 
3456  3456  
3457  3457  sage: expand((x1)^3/(y1)) 
3458  3458  x^3/(y  1)  3*x^2/(y  1) + 3*x/(y  1)  1/(y  1) 
3459  3459  sage: expand((x+sin((x+y)^2))^2) 
3460   x^2 + 2*x*sin((x + y)^2) + sin((x + y)^2)^2 
 3460  x^2 + sin((x + y)^2)^2 + 2*x*sin((x + y)^2) 
3461  3461  
3462  3462  We can expand individual sides of a relation:: 
3463  3463  
… 
… 

3474  3474  sage: var('x,y') 
3475  3475  (x, y) 
3476  3476  sage: ((x + (2/3)*y)^3).expand() 
3477   x^3 + 2*x^2*y + 4/3*x*y^2 + 8/27*y^3 
 3477  x^3 + 8/27*y^3 + 4/3*x*y^2 + 2*x^2*y 
3478  3478  sage: expand( (x*sin(x)  cos(y)/x)^2 ) 
3479   x^2*sin(x)^2  2*sin(x)*cos(y) + cos(y)^2/x^2 
 3479  x^2*sin(x)^2  2*cos(y)*sin(x) + cos(y)^2/x^2 
3480  3480  sage: f = (xy)*(x+y); f 
3481  3481  (x  y)*(x + y) 
3482  3482  sage: f.expand() 
… 
… 

3535  3535  EXAMPLES:: 
3536  3536  
3537  3537  sage: sin(5*x).expand_trig() 
3538   sin(x)^5  10*sin(x)^3*cos(x)^2 + 5*sin(x)*cos(x)^4 
 3538  sin(x)^5  10*cos(x)^2*sin(x)^3 + 5*cos(x)^4*sin(x) 
3539  3539  sage: cos(2*x + var('y')).expand_trig() 
3540   sin(2*x)*sin(y) + cos(2*x)*cos(y) 
 3540  cos(2*x)*cos(y)  sin(2*x)*sin(y) 
3541  3541  
3542  3542  We illustrate various options to this function:: 
3543  3543  
3544  3544  sage: f = sin(sin(3*cos(2*x))*x) 
3545  3545  sage: f.expand_trig() 
3546   sin((sin(cos(2*x))^3  3*sin(cos(2*x))*cos(cos(2*x))^2)*x) 
 3546  sin((sin(cos(2*x))^3  3*cos(cos(2*x))^2*sin(cos(2*x)))*x) 
3547  3547  sage: f.expand_trig(full=True) 
3548   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) 
 3548  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) 
3549  3549  sage: sin(2*x).expand_trig(times=False) 
3550  3550  sin(2*x) 
3551  3551  sage: sin(2*x).expand_trig(times=True) 
3552   2*sin(x)*cos(x) 
 3552  2*cos(x)*sin(x) 
3553  3553  sage: sin(2 + x).expand_trig(plus=False) 
3554  3554  sin(x + 2) 
3555  3555  sage: sin(2 + x).expand_trig(plus=True) 
3556   sin(2)*cos(x) + sin(x)*cos(2) 
 3556  cos(x)*sin(2) + cos(2)*sin(x) 
3557  3557  sage: sin(x/2).expand_trig(half_angles=False) 
3558  3558  sin(1/2*x) 
3559  3559  sage: sin(x/2).expand_trig(half_angles=True) 
… 
… 

3598  3598  sage: y=var('y') 
3599  3599  sage: f=sin(x)*cos(x)^3+sin(y)^2 
3600  3600  sage: f.reduce_trig() 
3601   1/4*sin(2*x) + 1/8*sin(4*x)  1/2*cos(2*y) + 1/2 
 3601  1/2*cos(2*y) + 1/4*sin(2*x) + 1/8*sin(4*x) + 1/2 
3602  3602  
3603  3603  To reduce only the expressions involving x we use optional parameter:: 
3604  3604  
… 
… 

3659  3659  {$0: x + y} 
3660  3660  sage: t = ((a+b)*(a+c)).match((a+w0)*(a+w1)) 
3661  3661  sage: t[w0], t[w1] 
3662   (b, c) 
 3662  (c, b) 
3663  3663  sage: ((a+b)*(a+c)).match((w0+b)*(w0+c)) 
3664  3664  {$0: a} 
3665  3665  sage: print ((a+b)*(a+c)).match((w0+w1)*(w0+w2)) # surprising? 
3666   None 
 3666  {$2: b, $0: a, $1: c} 
3667  3667  sage: t = (a*(x+y)+a*z+b).match(a*w0+w1) 
3668  3668  sage: t[w0], t[w1] 
3669   (x + y, a*z + b) 
 3669  (z, a*(x + y) + b) 
3670  3670  sage: print (a+b+c+d+f+g).match(c) 
3671  3671  None 
3672  3672  sage: (a+b+c+d+f+g).has(c) 
… 
… 

3823  3823  (x + y)^3 + b^2 + c 
3824  3824  
3825  3825  sage: t.subs({w0^2: w0^3}) 
3826   (x + y)^3 + a^3 + b^3 
 3826  a^3 + b^3 + (x + y)^3 
3827  3827  
3828  3828  # substitute with a relational expression 
3829  3829  sage: t.subs(w0^2 == w0^3) 
3830   (x + y)^3 + a^3 + b^3 
 3830  a^3 + b^3 + (x + y)^3 
3831  3831  
3832  3832  sage: t.subs(w0==w0^2) 
3833  3833  (x^2 + y^2)^18 + a^16 + b^16 
… 
… 

3945  3945  (x, y, z, a, b, c, d, f) 
3946  3946  sage: w0 = SR.wild(0); w1 = SR.wild(1) 
3947  3947  sage: (a^2 + b^2 + (x+y)^2)._subs_expr(w0^2 == w0^3) 
3948   (x + y)^3 + a^3 + b^3 
 3948  a^3 + b^3 + (x + y)^3 
3949  3949  sage: (a^4 + b^4 + (x+y)^4)._subs_expr(w0^2 == w0^3) 
3950   (x + y)^4 + a^4 + b^4 
 3950  a^4 + b^4 + (x + y)^4 
3951  3951  sage: (a^2 + b^4 + (x+y)^4)._subs_expr(w0^2 == w0^3) 
3952   (x + y)^4 + a^3 + b^4 
 3952  b^4 + (x + y)^4 + a^3 
3953  3953  sage: ((a+b+c)^2)._subs_expr(a+b == x) 
3954  3954  (a + b + c)^2 
3955  3955  sage: ((a+b+c)^2)._subs_expr(a+b+w0 == x+w0) 
… 
… 

3973  3973  sage: (sin(x)^2 + cos(x)^2)._subs_expr(sin(w0)^2+cos(w0)^2==1) 
3974  3974  1 
3975  3975  sage: (1 + sin(x)^2 + cos(x)^2)._subs_expr(sin(w0)^2+cos(w0)^2==1) 
3976   sin(x)^2 + cos(x)^2 + 1 
 3976  cos(x)^2 + sin(x)^2 + 1 
3977  3977  sage: (17*x + sin(x)^2 + cos(x)^2)._subs_expr(w1 + sin(w0)^2+cos(w0)^2 == w1 + 1) 
3978  3978  17*x + 1 
3979  3979  sage: ((x1)*(sin(x)^2 + cos(x)^2)^2)._subs_expr(sin(w0)^2+cos(w0)^2 == 1) 
… 
… 

4020  4020  x^4 + x 
4021  4021  sage: f = cos(x^2) + sin(x^2) 
4022  4022  sage: f.subs_expr(x^2 == x) 
4023   sin(x) + cos(x) 
 4023  cos(x) + sin(x) 
4024  4024  
4025  4025  :: 
4026  4026  
4027  4027  sage: f(x,y,t) = cos(x) + sin(y) + x^2 + y^2 + t 
4028  4028  sage: f.subs_expr(y^2 == t) 
4029   (x, y, t) > x^2 + 2*t + sin(y) + cos(x) 
 4029  (x, y, t) > x^2 + 2*t + cos(x) + sin(y) 
4030  4030  
4031  4031  The following seems really weird, but it *is* what Maple does:: 
4032  4032  
4033  4033  sage: f.subs_expr(x^2 + y^2 == t) 
4034   (x, y, t) > x^2 + y^2 + t + sin(y) + cos(x) 
 4034  (x, y, t) > x^2 + y^2 + t + cos(x) + sin(y) 
4035  4035  sage: maple.eval('subs(x^2 + y^2 = t, cos(x) + sin(y) + x^2 + y^2 + t)') # optional  maple 
4036  4036  'cos(x)+sin(y)+x^2+y^2+t' 
4037  4037  sage: maxima.quit() 
… 
… 

4080  4080  sage: var('x,y,z') 
4081  4081  (x, y, z) 
4082  4082  sage: (x+y)(x=z^2, y=x^y) 
4083   x^y + z^2 
 4083  z^2 + x^y 
4084  4084  """ 
4085  4085  return self._parent._call_element_(self, *args, **kwds) 
4086  4086  
… 
… 

4218  4218  sage: var('a,b,c,x,y') 
4219  4219  (a, b, c, x, y) 
4220  4220  sage: (a^2 + b^2 + (x+y)^2).operands() 
4221   [(x + y)^2, a^2, b^2] 
 4221  [a^2, b^2, (x + y)^2] 
4222  4222  sage: (a^2).operands() 
4223  4223  [a, 2] 
4224  4224  sage: (a*b^2*c).operands() 
… 
… 

4651  4651  sage: x.add(x, hold=True) 
4652  4652  x + x 
4653  4653  sage: x.add(x, (2+x), hold=True) 
4654   x + x + (x + 2) 
 4654  (x + 2) + x + x 
4655  4655  sage: x.add(x, (2+x), x, hold=True) 
4656   x + x + (x + 2) + x 
 4656  (x + 2) + x + x + x 
4657  4657  sage: x.add(x, (2+x), x, 2*x, hold=True) 
4658   x + x + (x + 2) + x + 2*x 
 4658  (x + 2) + 2*x + x + x + x 
4659  4659  
4660  4660  To then evaluate again, we currently must use Maxima via 
4661  4661  :meth:`simplify`:: 
… 
… 

4684  4684  sage: x.mul(x, hold=True) 
4685  4685  x*x 
4686  4686  sage: x.mul(x, (2+x), hold=True) 
4687   x*x*(x + 2) 
 4687  (x + 2)*x*x 
4688  4688  sage: x.mul(x, (2+x), x, hold=True) 
4689   x*x*(x + 2)*x 
 4689  (x + 2)*x*x*x 
4690  4690  sage: x.mul(x, (2+x), x, 2*x, hold=True) 
4691   x*x*(x + 2)*x*(2*x) 
 4691  (2*x)*(x + 2)*x*x*x 
4692  4692  
4693  4693  To then evaluate again, we currently must use Maxima via 
4694  4694  :meth:`simplify`:: 
… 
… 

4748  4748  sage: f.coefficient(sin(x*y)) 
4749  4749  x^3 + 2/x 
4750  4750  sage: f.collect(sin(x*y)) 
4751   (x^3 + 2/x)*sin(x*y) + a*x + x*y + x/y + 100 
 4751  a*x + x*y + (x^3 + 2/x)*sin(x*y) + x/y + 100 
4752  4752  
4753  4753  sage: var('a, x, y, z') 
4754  4754  (a, x, y, z) 
… 
… 

4935  4935  sage: bool(p.poly(a) == (xa*sqrt(2))^2 + x + 1) 
4936  4936  True 
4937  4937  sage: p.poly(x) 
4938   (2*sqrt(2)*a  1)*x + 2*a^2 + x^2 + 1 
 4938  2*a^2 + x^2  (2*sqrt(2)*a  1)*x + 1 
4939  4939  """ 
4940  4940  from sage.symbolic.ring import SR 
4941  4941  f = self._maxima_() 
… 
… 

5081  5081  sage: R = SR[x] 
5082  5082  sage: a = R(sqrt(2) + x^3 + y) 
5083  5083  sage: a 
5084   y + sqrt(2) + x^3 
 5084  x^3 + y + sqrt(2) 
5085  5085  sage: type(a) 
5086  5086  <class 'sage.rings.polynomial.polynomial_element_generic.Polynomial_generic_dense_field'> 
5087  5087  sage: a.degree() 
… 
… 

5306  5306  sage: x,y,z = var('x,y,z') 
5307  5307  sage: f = 4*x*y + x*z + 20*y^2 + 21*y*z + 4*z^2 + x^2*y^2*z^2 
5308  5308  sage: f.collect(x) 
5309   x^2*y^2*z^2 + (4*y + z)*x + 20*y^2 + 21*y*z + 4*z^2 
 5309  x^2*y^2*z^2 + 20*y^2 + x*(4*y + z) + 4*z^2 + 21*y*z 
5310  5310  
5311  5311  Here we do the same thing for `y` and `z`; however, note that 
5312  5312  we don't factor the `y^{2}` and `z^{2}` terms before 
5313  5313  collecting coefficients:: 
5314  5314  
5315  5315  sage: f.collect(y) 
5316   (x^2*z^2 + 20)*y^2 + (4*x + 21*z)*y + x*z + 4*z^2 
 5316  (x^2*z^2 + 20)*y^2 + (4*x + 21*z)*y + 4*z^2 + x*z 
5317  5317  sage: f.collect(z) 
5318   (x^2*y^2 + 4)*z^2 + (x + 21*y)*z + 4*x*y + 20*y^2 
 5318  (x^2*y^2 + 4)*z^2 + 20*y^2 + 4*x*y + (x + 21*y)*z 
5319  5319  
5320  5320  Sometimes, we do have to call :meth:`expand()` on the 
5321  5321  expression first to achieve the desired result:: 
… 
… 

5705  5705  (a, b) 
5706  5706  sage: f = log(a + b*I) 
5707  5707  sage: f.imag_part() 
5708   arctan2(real_part(b) + imag_part(a), real_part(a)  imag_part(b)) 
 5708  arctan2(imag_part(a) + real_part(b), imag_part(b) + real_part(a)) 
5709  5709  
5710  5710  Using the ``hold`` parameter it is possible to prevent automatic 
5711  5711  evaluation:: 
… 
… 

6730  6730  sage: x.factorial() 
6731  6731  factorial(x) 
6732  6732  sage: (x^2+y^3).factorial() 
6733   factorial(x^2 + y^3) 
 6733  factorial(y^3 + x^2) 
6734  6734  
6735  6735  To prevent automatic evaluation use the ``hold`` argument:: 
6736  6736  
… 
… 

7548  7548  EXAMPLES:: 
7549  7549  
7550  7550  sage: f = sin(x)^2 + cos(x)^2; f 
7551   sin(x)^2 + cos(x)^2 
 7551  cos(x)^2 + sin(x)^2 
7552  7552  sage: f.simplify() 
7553   sin(x)^2 + cos(x)^2 
 7553  cos(x)^2 + sin(x)^2 
7554  7554  sage: f.simplify_trig() 
7555  7555  1 
7556  7556  sage: h = sin(x)*csc(x) 
… 
… 

7564  7564  
7565  7565  sage: f=tan(3*x) 
7566  7566  sage: f.simplify_trig() 
7567   (4*cos(x)^2  1)*sin(x)/(4*cos(x)^3  3*cos(x)) 
 7567  (4*cos(x)^2  1)*sin(x)/(4*cos(x)^3 + 3*cos(x)) 
7568  7568  sage: f.simplify_trig(False) 
7569  7569  sin(3*x)/cos(3*x) 
7570  7570  
… 
… 

7645  7645  sage: y = var('y') 
7646  7646  sage: g = (x^(y/2) + 1)^2*(x^(y/2)  1)^2/(x^y  1) 
7647  7647  sage: g.simplify_rational(algorithm='simple') 
7648   (2*x^y  x^(2*y)  1)/(x^y  1) 
 7648  (x^(2*y)  2*x^y + 1)/(x^y  1) 
7649  7649  sage: g.simplify_rational() 
7650  7650  x^y  1 
7651  7651  
… 
… 

7656  7656  sage: f.simplify_rational() 
7657  7657  (2*x^2 + 5*x + 4)/(x^3 + 5*x^2 + 8*x + 4) 
7658  7658  sage: f.simplify_rational(algorithm='noexpand') 
7659   ((x + 1)*x + (x + 2)^2)/((x + 1)*(x + 2)^2) 
7660   
 7659  ((x + 2)^2 + (x + 1)*x)/((x + 1)*(x + 2)^2) 
7661  7660  """ 
7662  7661  self_m = self._maxima_() 
7663  7662  if algorithm == 'full': 
… 
… 

7709  7708  :: 
7710  7709  
7711  7710  sage: f = binomial(n, k)*factorial(k)*factorial(nk); f 
7712   factorial(k + n)*factorial(k)*binomial(n, k) 
 7711  binomial(n, k)*factorial(k + n)*factorial(k) 
7713  7712  sage: f.simplify_factorial() 
7714  7713  factorial(n) 
7715  7714  
… 
… 

7902  7901  
7903  7902  sage: f = log(x)+log(y)1/3*log((x+1)) 
7904  7903  sage: f.simplify_log() 
7905   1/3*log(x + 1) + log(x*y) 
 7904  log(x*y)  1/3*log(x + 1) 
7906  7905  
7907  7906  sage: f.simplify_log('ratios') 
7908  7907  log(x*y/(x + 1)^(1/3)) 
… 
… 

8117  8116  
8118  8117  sage: x,y,z = var('x, y, z') 
8119  8118  sage: (x^3y^3).factor() 
8120   (x  y)*(x^2 + x*y + y^2) 
 8119  (x^2 + y^2 + x*y)*(x  y) 
8121  8120  sage: factor(8*y  4*x + z^2*(2*y + x)) 
8122   (z  2)*(z + 2)*(x + 2*y) 
 8121  (x + 2*y)*(z  2)*(z + 2) 
8123  8122  sage: f = 1  2*x  x^2 + y^2 + 2*x*y^2 + x^2*y^2 
8124  8123  sage: F = factor(f/(36*(1 + 2*y + y^2)), dontfactor=[x]); F 
8125   1/36*(y  1)*(x^2 + 2*x + 1)/(y + 1) 
 8124  1/36*(x^2 + 2*x + 1)*(y  1)/(y + 1) 
8126  8125  
8127  8126  If you are factoring a polynomial with rational coefficients (and 
8128  8127  dontfactor is empty) the factorization is done using Singular 
… 
… 

8132  8131  sage: var('x,y') 
8133  8132  (x, y) 
8134  8133  sage: (x^99 + y^99).factor() 
8135   (x + y)*(x^2  x*y + y^2)*(x^6  x^3*y^3 + y^6)*... 
 8134  (x^60 + y^60 + x^3*y^57  x^9*y^51  x^12*y^48 + x^18*y^42 
 8135  + x^21*y^39  x^27*y^33  x^30*y^30  x^33*y^27 + x^39*y^21 
 8136  + x^42*y^18  x^48*y^12  x^51*y^9 + x^57*y^3)*(x^20 + y^20 
 8137  + x*y^19  x^3*y^17  x^4*y^16 + x^6*y^14 + x^7*y^13  x^9*y^11 
 8138   x^10*y^10  x^11*y^9 + x^13*y^7 + x^14*y^6  x^16*y^4 
 8139   x^17*y^3 + x^19*y)*(x^10 + y^10  x*y^9 + x^2*y^8  x^3*y^7 
 8140  + 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 
 8141  + y^6  x^3*y^3)*(x^2 + y^2  x*y)*(x + y) 
8136  8142  """ 
8137  8143  from sage.calculus.calculus import symbolic_expression_from_maxima_string, symbolic_expression_from_string 
8138  8144  if len(dontfactor) > 0: 
… 
… 

8173  8179  (x, y, z) 
8174  8180  sage: f = x^3y^3 
8175  8181  sage: f.factor() 
8176   (x  y)*(x^2 + x*y + y^2) 
 8182  (x^2 + y^2 + x*y)*(x  y) 
8177  8183  
8178  8184  Notice that the 1 factor is separated out:: 
8179  8185  
8180  8186  sage: f.factor_list() 
8181   [(x  y, 1), (x^2 + x*y + y^2, 1)] 
 8187  [(x^2 + y^2 + x*y, 1), (x  y, 1)] 
8182  8188  
8183  8189  We factor a fairly straightforward expression:: 
8184  8190  
8185  8191  sage: factor(8*y  4*x + z^2*(2*y + x)).factor_list() 
8186   [(z  2, 1), (z + 2, 1), (x + 2*y, 1)] 
 8192  [(x + 2*y, 1), (z  2, 1), (z + 2, 1)] 
8187  8193  
8188  8194  A more complicated example:: 
8189  8195  
… 
… 

8191  8197  (x, u, v) 
8192  8198  sage: f = expand((2*u*v^2v^24*u^3)^2 * (u)^3 * (xsin(x))^3) 
8193  8199  sage: f.factor() 
8194   (x  sin(x))^3*(4*u^3  2*u*v^2 + v^2)^2*u^3 
 8200  (4*u^3 + 2*u*v^2  v^2)^2*u^3*(x + sin(x))^3 
8195  8201  sage: g = f.factor_list(); g 
8196   [(x  sin(x), 3), (4*u^3  2*u*v^2 + v^2, 2), (u, 3), (1, 1)] 
 8202  [(4*u^3  2*u*v^2 + v^2, 2), (u, 3), (x  sin(x), 3), (1, 1)] 
8197  8203  
8198  8204  This function also works for quotients:: 
8199  8205  
… 
… 

8201  8207  sage: g = f/(36*(1 + 2*y + y^2)); g 
8202  8208  1/36*(x^2*y^2 + 2*x*y^2  x^2 + y^2  2*x  1)/(y^2 + 2*y + 1) 
8203  8209  sage: g.factor(dontfactor=[x]) 
8204   1/36*(y  1)*(x^2 + 2*x + 1)/(y + 1) 
 8210  1/36*(x^2 + 2*x + 1)*(y  1)/(y + 1) 
8205  8211  sage: g.factor_list(dontfactor=[x]) 
8206   [(y  1, 1), (y + 1, 1), (x^2 + 2*x + 1, 1), (1/36, 1)] 
 8212  [(x^2 + 2*x + 1, 1), (y  1, 1), (y + 1, 1), (1/36, 1)] 
8207  8213  
8208  8214  This example also illustrates that the exponents do not have to be 
8209  8215  integers:: 
… 
… 

8226  8232  EXAMPLES:: 
8227  8233  
8228  8234  sage: g = factor(x^3  1); g 
8229   (x  1)*(x^2 + x + 1) 
 8235  (x^2 + x + 1)*(x  1) 
8230  8236  sage: v = g._factor_list(); v 
8231   [(x  1, 1), (x^2 + x + 1, 1)] 
 8237  [(x^2 + x + 1, 1), (x  1, 1)] 
8232  8238  sage: type(v) 
8233  8239  <type 'list'> 
8234  8240  """ 
… 
… 

8372  8378  A complicated example:: 
8373  8379  
8374  8380  sage: f = expand((x^2  1)^3*(x^2 + 1)*(xa)); f 
8375   a*x^8 + x^9 + 2*a*x^6  2*x^7  2*a*x^2 + 2*x^3 + a  x 
 8381  x^9  a*x^8  2*x^7 + 2*a*x^6 + 2*x^3  2*a*x^2 + a  x 
8376  8382  
8377  8383  The default variable is `a`, since it is the first in 
8378  8384  alphabetical order:: 
… 
… 

8411  8417  sage: var('a,b,c,x') 
8412  8418  (a, b, c, x) 
8413  8419  sage: (a*x^2 + b*x + c).roots(x) 
8414   [(1/2*(b + sqrt(4*a*c + b^2))/a, 1), (1/2*(b  sqrt(4*a*c + b^2))/a, 1)] 
 8420  [(1/2*(b + sqrt(b^2  4*a*c))/a, 1), (1/2*(b  sqrt(b^2  4*a*c))/a, 1)] 
8415  8421  
8416  8422  By default, all the roots are required to be explicit rather than 
8417  8423  implicit. To get implicit roots, pass ``explicit_solutions=False`` 
… 
… 

8425  8431  ... 
8426  8432  RuntimeError: no explicit roots found 
8427  8433  sage: f.roots(explicit_solutions=False) 
8428   [((2^(8/9)  2^(1/9) + x^(8/9)  x^(1/9))/(2^(8/9)  2^(1/9)), 1)] 
 8434  [((2^(8/9) + x^(8/9)  2^(1/9)  x^(1/9))/(2^(8/9)  2^(1/9)), 1)] 
8429  8435  
8430  8436  Another example, but involving a degree 5 poly whose roots don't 
8431  8437  get computed explicitly:: 
… 
… 

9299  9305  :: 
9300  9306  
9301  9307  sage: (k * binomial(n, k)).sum(k, 1, n) 
9302   n*2^(n  1) 
 9308  2^(n  1)*n 
9303  9309  
9304  9310  :: 
9305  9311  
… 
… 

9568  9574  sage: f*(2/3) 
9569  9575  2/3*x  2 < 2/3*y + 4/3 
9570  9576  sage: f*(pi) 
9571   (x + 3)*pi < (y  2)*pi 
 9577  pi*(x + 3) < pi*(y  2) 
9572  9578  
9573  9579  Since the direction of the inequality never changes when doing 
9574  9580  arithmetic with equations, you can multiply or divide the 
diff git a/sage/symbolic/random_tests.py b/sage/symbolic/random_tests.py
a

b


339  339  sage: for i,j in CartesianProduct(range(0,3),range(0,3)): 
340  340  ... cmp[i,j] = x[i].__cmp__(x[j]) 
341  341  sage: cmp 
342   [ 0 1 1] 
343   [1 0 1] 
344   [1 1 0] 
 342  [ 0 1 1] 
 343  [ 1 0 1] 
 344  [ 1 1 0] 
345  345  """ 
346  346  from sage.matrix.constructor import matrix 
347  347  from sage.combinat.cartesian_product import CartesianProduct 