test_simplify.py 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. from sympy.concrete.summations import Sum
  2. from sympy.core.add import Add
  3. from sympy.core.basic import Basic
  4. from sympy.core.expr import unchanged
  5. from sympy.core.function import (count_ops, diff, expand, expand_multinomial, Function, Derivative)
  6. from sympy.core.mul import Mul, _keep_coeff
  7. from sympy.core import GoldenRatio
  8. from sympy.core.numbers import (E, Float, I, oo, pi, Rational, zoo)
  9. from sympy.core.relational import (Eq, Lt, Gt, Ge, Le)
  10. from sympy.core.singleton import S
  11. from sympy.core.symbol import (Symbol, symbols)
  12. from sympy.core.sympify import sympify
  13. from sympy.functions.combinatorial.factorials import (binomial, factorial)
  14. from sympy.functions.elementary.complexes import (Abs, sign)
  15. from sympy.functions.elementary.exponential import (exp, exp_polar, log)
  16. from sympy.functions.elementary.hyperbolic import (cosh, csch, sinh)
  17. from sympy.functions.elementary.miscellaneous import sqrt
  18. from sympy.functions.elementary.piecewise import Piecewise
  19. from sympy.functions.elementary.trigonometric import (acos, asin, atan, cos, sin, sinc, tan)
  20. from sympy.functions.special.error_functions import erf
  21. from sympy.functions.special.gamma_functions import gamma
  22. from sympy.functions.special.hyper import hyper
  23. from sympy.functions.special.tensor_functions import KroneckerDelta
  24. from sympy.geometry.polygon import rad
  25. from sympy.integrals.integrals import (Integral, integrate)
  26. from sympy.logic.boolalg import (And, Or)
  27. from sympy.matrices.dense import (Matrix, eye)
  28. from sympy.matrices.expressions.matexpr import MatrixSymbol
  29. from sympy.polys.polytools import (factor, Poly)
  30. from sympy.simplify.simplify import (besselsimp, hypersimp, inversecombine, logcombine, nsimplify, nthroot, posify, separatevars, signsimp, simplify)
  31. from sympy.solvers.solvers import solve
  32. from sympy.testing.pytest import XFAIL, slow, _both_exp_pow
  33. from sympy.abc import x, y, z, t, a, b, c, d, e, f, g, h, i, n
  34. def test_issue_7263():
  35. assert abs((simplify(30.8**2 - 82.5**2 * sin(rad(11.6))**2)).evalf() - \
  36. 673.447451402970) < 1e-12
  37. def test_factorial_simplify():
  38. # There are more tests in test_factorials.py.
  39. x = Symbol('x')
  40. assert simplify(factorial(x)/x) == gamma(x)
  41. assert simplify(factorial(factorial(x))) == factorial(factorial(x))
  42. def test_simplify_expr():
  43. x, y, z, k, n, m, w, s, A = symbols('x,y,z,k,n,m,w,s,A')
  44. f = Function('f')
  45. assert all(simplify(tmp) == tmp for tmp in [I, E, oo, x, -x, -oo, -E, -I])
  46. e = 1/x + 1/y
  47. assert e != (x + y)/(x*y)
  48. assert simplify(e) == (x + y)/(x*y)
  49. e = A**2*s**4/(4*pi*k*m**3)
  50. assert simplify(e) == e
  51. e = (4 + 4*x - 2*(2 + 2*x))/(2 + 2*x)
  52. assert simplify(e) == 0
  53. e = (-4*x*y**2 - 2*y**3 - 2*x**2*y)/(x + y)**2
  54. assert simplify(e) == -2*y
  55. e = -x - y - (x + y)**(-1)*y**2 + (x + y)**(-1)*x**2
  56. assert simplify(e) == -2*y
  57. e = (x + x*y)/x
  58. assert simplify(e) == 1 + y
  59. e = (f(x) + y*f(x))/f(x)
  60. assert simplify(e) == 1 + y
  61. e = (2 * (1/n - cos(n * pi)/n))/pi
  62. assert simplify(e) == (-cos(pi*n) + 1)/(pi*n)*2
  63. e = integrate(1/(x**3 + 1), x).diff(x)
  64. assert simplify(e) == 1/(x**3 + 1)
  65. e = integrate(x/(x**2 + 3*x + 1), x).diff(x)
  66. assert simplify(e) == x/(x**2 + 3*x + 1)
  67. f = Symbol('f')
  68. A = Matrix([[2*k - m*w**2, -k], [-k, k - m*w**2]]).inv()
  69. assert simplify((A*Matrix([0, f]))[1] -
  70. (-f*(2*k - m*w**2)/(k**2 - (k - m*w**2)*(2*k - m*w**2)))) == 0
  71. f = -x + y/(z + t) + z*x/(z + t) + z*a/(z + t) + t*x/(z + t)
  72. assert simplify(f) == (y + a*z)/(z + t)
  73. # issue 10347
  74. expr = -x*(y**2 - 1)*(2*y**2*(x**2 - 1)/(a*(x**2 - y**2)**2) + (x**2 - 1)
  75. /(a*(x**2 - y**2)))/(a*(x**2 - y**2)) + x*(-2*x**2*sqrt(-x**2*y**2 + x**2
  76. + y**2 - 1)*sin(z)/(a*(x**2 - y**2)**2) - x**2*sqrt(-x**2*y**2 + x**2 +
  77. y**2 - 1)*sin(z)/(a*(x**2 - 1)*(x**2 - y**2)) + (x**2*sqrt((-x**2 + 1)*
  78. (y**2 - 1))*sqrt(-x**2*y**2 + x**2 + y**2 - 1)*sin(z)/(x**2 - 1) + sqrt(
  79. (-x**2 + 1)*(y**2 - 1))*(x*(-x*y**2 + x)/sqrt(-x**2*y**2 + x**2 + y**2 -
  80. 1) + sqrt(-x**2*y**2 + x**2 + y**2 - 1))*sin(z))/(a*sqrt((-x**2 + 1)*(
  81. y**2 - 1))*(x**2 - y**2)))*sqrt(-x**2*y**2 + x**2 + y**2 - 1)*sin(z)/(a*
  82. (x**2 - y**2)) + x*(-2*x**2*sqrt(-x**2*y**2 + x**2 + y**2 - 1)*cos(z)/(a*
  83. (x**2 - y**2)**2) - x**2*sqrt(-x**2*y**2 + x**2 + y**2 - 1)*cos(z)/(a*
  84. (x**2 - 1)*(x**2 - y**2)) + (x**2*sqrt((-x**2 + 1)*(y**2 - 1))*sqrt(-x**2
  85. *y**2 + x**2 + y**2 - 1)*cos(z)/(x**2 - 1) + x*sqrt((-x**2 + 1)*(y**2 -
  86. 1))*(-x*y**2 + x)*cos(z)/sqrt(-x**2*y**2 + x**2 + y**2 - 1) + sqrt((-x**2
  87. + 1)*(y**2 - 1))*sqrt(-x**2*y**2 + x**2 + y**2 - 1)*cos(z))/(a*sqrt((-x**2
  88. + 1)*(y**2 - 1))*(x**2 - y**2)))*sqrt(-x**2*y**2 + x**2 + y**2 - 1)*cos(
  89. z)/(a*(x**2 - y**2)) - y*sqrt((-x**2 + 1)*(y**2 - 1))*(-x*y*sqrt(-x**2*
  90. y**2 + x**2 + y**2 - 1)*sin(z)/(a*(x**2 - y**2)*(y**2 - 1)) + 2*x*y*sqrt(
  91. -x**2*y**2 + x**2 + y**2 - 1)*sin(z)/(a*(x**2 - y**2)**2) + (x*y*sqrt((
  92. -x**2 + 1)*(y**2 - 1))*sqrt(-x**2*y**2 + x**2 + y**2 - 1)*sin(z)/(y**2 -
  93. 1) + x*sqrt((-x**2 + 1)*(y**2 - 1))*(-x**2*y + y)*sin(z)/sqrt(-x**2*y**2
  94. + x**2 + y**2 - 1))/(a*sqrt((-x**2 + 1)*(y**2 - 1))*(x**2 - y**2)))*sin(
  95. z)/(a*(x**2 - y**2)) + y*(x**2 - 1)*(-2*x*y*(x**2 - 1)/(a*(x**2 - y**2)
  96. **2) + 2*x*y/(a*(x**2 - y**2)))/(a*(x**2 - y**2)) + y*(x**2 - 1)*(y**2 -
  97. 1)*(-x*y*sqrt(-x**2*y**2 + x**2 + y**2 - 1)*cos(z)/(a*(x**2 - y**2)*(y**2
  98. - 1)) + 2*x*y*sqrt(-x**2*y**2 + x**2 + y**2 - 1)*cos(z)/(a*(x**2 - y**2)
  99. **2) + (x*y*sqrt((-x**2 + 1)*(y**2 - 1))*sqrt(-x**2*y**2 + x**2 + y**2 -
  100. 1)*cos(z)/(y**2 - 1) + x*sqrt((-x**2 + 1)*(y**2 - 1))*(-x**2*y + y)*cos(
  101. z)/sqrt(-x**2*y**2 + x**2 + y**2 - 1))/(a*sqrt((-x**2 + 1)*(y**2 - 1)
  102. )*(x**2 - y**2)))*cos(z)/(a*sqrt((-x**2 + 1)*(y**2 - 1))*(x**2 - y**2)
  103. ) - x*sqrt((-x**2 + 1)*(y**2 - 1))*sqrt(-x**2*y**2 + x**2 + y**2 - 1)*sin(
  104. z)**2/(a**2*(x**2 - 1)*(x**2 - y**2)*(y**2 - 1)) - x*sqrt((-x**2 + 1)*(
  105. y**2 - 1))*sqrt(-x**2*y**2 + x**2 + y**2 - 1)*cos(z)**2/(a**2*(x**2 - 1)*(
  106. x**2 - y**2)*(y**2 - 1))
  107. assert simplify(expr) == 2*x/(a**2*(x**2 - y**2))
  108. #issue 17631
  109. assert simplify('((-1/2)*Boole(True)*Boole(False)-1)*Boole(True)') == \
  110. Mul(sympify('(2 + Boole(True)*Boole(False))'), sympify('-Boole(True)/2'))
  111. A, B = symbols('A,B', commutative=False)
  112. assert simplify(A*B - B*A) == A*B - B*A
  113. assert simplify(A/(1 + y/x)) == x*A/(x + y)
  114. assert simplify(A*(1/x + 1/y)) == A/x + A/y #(x + y)*A/(x*y)
  115. assert simplify(log(2) + log(3)) == log(6)
  116. assert simplify(log(2*x) - log(2)) == log(x)
  117. assert simplify(hyper([], [], x)) == exp(x)
  118. def test_issue_3557():
  119. f_1 = x*a + y*b + z*c - 1
  120. f_2 = x*d + y*e + z*f - 1
  121. f_3 = x*g + y*h + z*i - 1
  122. solutions = solve([f_1, f_2, f_3], x, y, z, simplify=False)
  123. assert simplify(solutions[y]) == \
  124. (a*i + c*d + f*g - a*f - c*g - d*i)/ \
  125. (a*e*i + b*f*g + c*d*h - a*f*h - b*d*i - c*e*g)
  126. def test_simplify_other():
  127. assert simplify(sin(x)**2 + cos(x)**2) == 1
  128. assert simplify(gamma(x + 1)/gamma(x)) == x
  129. assert simplify(sin(x)**2 + cos(x)**2 + factorial(x)/gamma(x)) == 1 + x
  130. assert simplify(
  131. Eq(sin(x)**2 + cos(x)**2, factorial(x)/gamma(x))) == Eq(x, 1)
  132. nc = symbols('nc', commutative=False)
  133. assert simplify(x + x*nc) == x*(1 + nc)
  134. # issue 6123
  135. # f = exp(-I*(k*sqrt(t) + x/(2*sqrt(t)))**2)
  136. # ans = integrate(f, (k, -oo, oo), conds='none')
  137. ans = I*(-pi*x*exp(I*pi*Rational(-3, 4) + I*x**2/(4*t))*erf(x*exp(I*pi*Rational(-3, 4))/
  138. (2*sqrt(t)))/(2*sqrt(t)) + pi*x*exp(I*pi*Rational(-3, 4) + I*x**2/(4*t))/
  139. (2*sqrt(t)))*exp(-I*x**2/(4*t))/(sqrt(pi)*x) - I*sqrt(pi) * \
  140. (-erf(x*exp(I*pi/4)/(2*sqrt(t))) + 1)*exp(I*pi/4)/(2*sqrt(t))
  141. assert simplify(ans) == -(-1)**Rational(3, 4)*sqrt(pi)/sqrt(t)
  142. # issue 6370
  143. assert simplify(2**(2 + x)/4) == 2**x
  144. @_both_exp_pow
  145. def test_simplify_complex():
  146. cosAsExp = cos(x)._eval_rewrite_as_exp(x)
  147. tanAsExp = tan(x)._eval_rewrite_as_exp(x)
  148. assert simplify(cosAsExp*tanAsExp) == sin(x) # issue 4341
  149. # issue 10124
  150. assert simplify(exp(Matrix([[0, -1], [1, 0]]))) == Matrix([[cos(1),
  151. -sin(1)], [sin(1), cos(1)]])
  152. def test_simplify_ratio():
  153. # roots of x**3-3*x+5
  154. roots = ['(1/2 - sqrt(3)*I/2)*(sqrt(21)/2 + 5/2)**(1/3) + 1/((1/2 - '
  155. 'sqrt(3)*I/2)*(sqrt(21)/2 + 5/2)**(1/3))',
  156. '1/((1/2 + sqrt(3)*I/2)*(sqrt(21)/2 + 5/2)**(1/3)) + '
  157. '(1/2 + sqrt(3)*I/2)*(sqrt(21)/2 + 5/2)**(1/3)',
  158. '-(sqrt(21)/2 + 5/2)**(1/3) - 1/(sqrt(21)/2 + 5/2)**(1/3)']
  159. for r in roots:
  160. r = S(r)
  161. assert count_ops(simplify(r, ratio=1)) <= count_ops(r)
  162. # If ratio=oo, simplify() is always applied:
  163. assert simplify(r, ratio=oo) is not r
  164. def test_simplify_measure():
  165. measure1 = lambda expr: len(str(expr))
  166. measure2 = lambda expr: -count_ops(expr)
  167. # Return the most complicated result
  168. expr = (x + 1)/(x + sin(x)**2 + cos(x)**2)
  169. assert measure1(simplify(expr, measure=measure1)) <= measure1(expr)
  170. assert measure2(simplify(expr, measure=measure2)) <= measure2(expr)
  171. expr2 = Eq(sin(x)**2 + cos(x)**2, 1)
  172. assert measure1(simplify(expr2, measure=measure1)) <= measure1(expr2)
  173. assert measure2(simplify(expr2, measure=measure2)) <= measure2(expr2)
  174. def test_simplify_rational():
  175. expr = 2**x*2.**y
  176. assert simplify(expr, rational = True) == 2**(x+y)
  177. assert simplify(expr, rational = None) == 2.0**(x+y)
  178. assert simplify(expr, rational = False) == expr
  179. assert simplify('0.9 - 0.8 - 0.1', rational = True) == 0
  180. def test_simplify_issue_1308():
  181. assert simplify(exp(Rational(-1, 2)) + exp(Rational(-3, 2))) == \
  182. (1 + E)*exp(Rational(-3, 2))
  183. def test_issue_5652():
  184. assert simplify(E + exp(-E)) == exp(-E) + E
  185. n = symbols('n', commutative=False)
  186. assert simplify(n + n**(-n)) == n + n**(-n)
  187. def test_issue_27380():
  188. assert simplify(1.0**(x+1)/1.0**x) == 1.0
  189. def test_simplify_fail1():
  190. x = Symbol('x')
  191. y = Symbol('y')
  192. e = (x + y)**2/(-4*x*y**2 - 2*y**3 - 2*x**2*y)
  193. assert simplify(e) == 1 / (-2*y)
  194. def test_nthroot():
  195. assert nthroot(90 + 34*sqrt(7), 3) == sqrt(7) + 3
  196. q = 1 + sqrt(2) - 2*sqrt(3) + sqrt(6) + sqrt(7)
  197. assert nthroot(expand_multinomial(q**3), 3) == q
  198. assert nthroot(41 + 29*sqrt(2), 5) == 1 + sqrt(2)
  199. assert nthroot(-41 - 29*sqrt(2), 5) == -1 - sqrt(2)
  200. expr = 1320*sqrt(10) + 4216 + 2576*sqrt(6) + 1640*sqrt(15)
  201. assert nthroot(expr, 5) == 1 + sqrt(6) + sqrt(15)
  202. q = 1 + sqrt(2) + sqrt(3) + sqrt(5)
  203. assert expand_multinomial(nthroot(expand_multinomial(q**5), 5)) == q
  204. q = 1 + sqrt(2) + 7*sqrt(6) + 2*sqrt(10)
  205. assert nthroot(expand_multinomial(q**5), 5, 8) == q
  206. q = 1 + sqrt(2) - 2*sqrt(3) + 1171*sqrt(6)
  207. assert nthroot(expand_multinomial(q**3), 3) == q
  208. assert nthroot(expand_multinomial(q**6), 6) == q
  209. def test_nthroot1():
  210. q = 1 + sqrt(2) + sqrt(3) + S.One/10**20
  211. p = expand_multinomial(q**5)
  212. assert nthroot(p, 5) == q
  213. q = 1 + sqrt(2) + sqrt(3) + S.One/10**30
  214. p = expand_multinomial(q**5)
  215. assert nthroot(p, 5) == q
  216. @_both_exp_pow
  217. def test_separatevars():
  218. x, y, z, n = symbols('x,y,z,n')
  219. assert separatevars(2*n*x*z + 2*x*y*z) == 2*x*z*(n + y)
  220. assert separatevars(x*z + x*y*z) == x*z*(1 + y)
  221. assert separatevars(pi*x*z + pi*x*y*z) == pi*x*z*(1 + y)
  222. assert separatevars(x*y**2*sin(x) + x*sin(x)*sin(y)) == \
  223. x*(sin(y) + y**2)*sin(x)
  224. assert separatevars(x*exp(x + y) + x*exp(x)) == x*(1 + exp(y))*exp(x)
  225. assert separatevars((x*(y + 1))**z).is_Pow # != x**z*(1 + y)**z
  226. assert separatevars(1 + x + y + x*y) == (x + 1)*(y + 1)
  227. assert separatevars(y/pi*exp(-(z - x)/cos(n))) == \
  228. y*exp(x/cos(n))*exp(-z/cos(n))/pi
  229. assert separatevars((x + y)*(x - y) + y**2 + 2*x + 1) == (x + 1)**2
  230. # issue 4858
  231. p = Symbol('p', positive=True)
  232. assert separatevars(sqrt(p**2 + x*p**2)) == p*sqrt(1 + x)
  233. assert separatevars(sqrt(y*(p**2 + x*p**2))) == p*sqrt(y*(1 + x))
  234. assert separatevars(sqrt(y*(p**2 + x*p**2)), force=True) == \
  235. p*sqrt(y)*sqrt(1 + x)
  236. # issue 4865
  237. assert separatevars(sqrt(x*y)).is_Pow
  238. assert separatevars(sqrt(x*y), force=True) == sqrt(x)*sqrt(y)
  239. # issue 4957
  240. # any type sequence for symbols is fine
  241. assert separatevars(((2*x + 2)*y), dict=True, symbols=()) == \
  242. {'coeff': 1, x: 2*x + 2, y: y}
  243. # separable
  244. assert separatevars(((2*x + 2)*y), dict=True, symbols=[x]) == \
  245. {'coeff': y, x: 2*x + 2}
  246. assert separatevars(((2*x + 2)*y), dict=True, symbols=[]) == \
  247. {'coeff': 1, x: 2*x + 2, y: y}
  248. assert separatevars(((2*x + 2)*y), dict=True) == \
  249. {'coeff': 1, x: 2*x + 2, y: y}
  250. assert separatevars(((2*x + 2)*y), dict=True, symbols=None) == \
  251. {'coeff': y*(2*x + 2)}
  252. # not separable
  253. assert separatevars(3, dict=True) is None
  254. assert separatevars(2*x + y, dict=True, symbols=()) is None
  255. assert separatevars(2*x + y, dict=True) is None
  256. assert separatevars(2*x + y, dict=True, symbols=None) == {'coeff': 2*x + y}
  257. # issue 4808
  258. n, m = symbols('n,m', commutative=False)
  259. assert separatevars(m + n*m) == (1 + n)*m
  260. assert separatevars(x + x*n) == x*(1 + n)
  261. # issue 4910
  262. f = Function('f')
  263. assert separatevars(f(x) + x*f(x)) == f(x) + x*f(x)
  264. # a noncommutable object present
  265. eq = x*(1 + hyper((), (), y*z))
  266. assert separatevars(eq) == eq
  267. s = separatevars(abs(x*y))
  268. assert s == abs(x)*abs(y) and s.is_Mul
  269. z = cos(1)**2 + sin(1)**2 - 1
  270. a = abs(x*z)
  271. s = separatevars(a)
  272. assert not a.is_Mul and s.is_Mul and s == abs(x)*abs(z)
  273. s = separatevars(abs(x*y*z))
  274. assert s == abs(x)*abs(y)*abs(z)
  275. # abs(x+y)/abs(z) would be better but we test this here to
  276. # see that it doesn't raise
  277. assert separatevars(abs((x+y)/z)) == abs((x+y)/z)
  278. def test_separatevars_advanced_factor():
  279. x, y, z = symbols('x,y,z')
  280. assert separatevars(1 + log(x)*log(y) + log(x) + log(y)) == \
  281. (log(x) + 1)*(log(y) + 1)
  282. assert separatevars(1 + x - log(z) - x*log(z) - exp(y)*log(z) -
  283. x*exp(y)*log(z) + x*exp(y) + exp(y)) == \
  284. -((x + 1)*(log(z) - 1)*(exp(y) + 1))
  285. x, y = symbols('x,y', positive=True)
  286. assert separatevars(1 + log(x**log(y)) + log(x*y)) == \
  287. (log(x) + 1)*(log(y) + 1)
  288. def test_hypersimp():
  289. n, k = symbols('n,k', integer=True)
  290. assert hypersimp(factorial(k), k) == k + 1
  291. assert hypersimp(factorial(k**2), k) is None
  292. assert hypersimp(1/factorial(k), k) == 1/(k + 1)
  293. assert hypersimp(2**k/factorial(k)**2, k) == 2/(k + 1)**2
  294. assert hypersimp(binomial(n, k), k) == (n - k)/(k + 1)
  295. assert hypersimp(binomial(n + 1, k), k) == (n - k + 1)/(k + 1)
  296. term = (4*k + 1)*factorial(k)/factorial(2*k + 1)
  297. assert hypersimp(term, k) == S.Half*((4*k + 5)/(3 + 14*k + 8*k**2))
  298. term = 1/((2*k - 1)*factorial(2*k + 1))
  299. assert hypersimp(term, k) == (k - S.Half)/((k + 1)*(2*k + 1)*(2*k + 3))
  300. term = binomial(n, k)*(-1)**k/factorial(k)
  301. assert hypersimp(term, k) == (k - n)/(k + 1)**2
  302. def test_nsimplify():
  303. x = Symbol("x")
  304. assert nsimplify(0) == 0
  305. assert nsimplify(-1) == -1
  306. assert nsimplify(1) == 1
  307. assert nsimplify(1 + x) == 1 + x
  308. assert nsimplify(2.7) == Rational(27, 10)
  309. assert nsimplify(1 - GoldenRatio) == (1 - sqrt(5))/2
  310. assert nsimplify((1 + sqrt(5))/4, [GoldenRatio]) == GoldenRatio/2
  311. assert nsimplify(2/GoldenRatio, [GoldenRatio]) == 2*GoldenRatio - 2
  312. assert nsimplify(exp(pi*I*Rational(5, 3), evaluate=False)) == \
  313. sympify('1/2 - sqrt(3)*I/2')
  314. assert nsimplify(sin(pi*Rational(3, 5), evaluate=False)) == \
  315. sympify('sqrt(sqrt(5)/8 + 5/8)')
  316. assert nsimplify(sqrt(atan('1', evaluate=False))*(2 + I), [pi]) == \
  317. sqrt(pi) + sqrt(pi)/2*I
  318. assert nsimplify(2 + exp(2*atan('1/4')*I)) == sympify('49/17 + 8*I/17')
  319. assert nsimplify(pi, tolerance=0.01) == Rational(22, 7)
  320. assert nsimplify(pi, tolerance=0.001) == Rational(355, 113)
  321. assert nsimplify(0.33333, tolerance=1e-4) == Rational(1, 3)
  322. assert nsimplify(2.0**(1/3.), tolerance=0.001) == Rational(635, 504)
  323. assert nsimplify(2.0**(1/3.), tolerance=0.001, full=True) == \
  324. 2**Rational(1, 3)
  325. assert nsimplify(x + .5, rational=True) == S.Half + x
  326. assert nsimplify(1/.3 + x, rational=True) == Rational(10, 3) + x
  327. assert nsimplify(log(3).n(), rational=True) == \
  328. sympify('109861228866811/100000000000000')
  329. assert nsimplify(Float(0.272198261287950), [pi, log(2)]) == pi*log(2)/8
  330. assert nsimplify(Float(0.272198261287950).n(3), [pi, log(2)]) == \
  331. -pi/4 - log(2) + Rational(7, 4)
  332. assert nsimplify(x/7.0) == x/7
  333. assert nsimplify(pi/1e2) == pi/100
  334. assert nsimplify(pi/1e2, rational=False) == pi/100.0
  335. assert nsimplify(pi/1e-7) == 10000000*pi
  336. assert not nsimplify(
  337. factor(-3.0*z**2*(z**2)**(-2.5) + 3*(z**2)**(-1.5))).atoms(Float)
  338. e = x**0.0
  339. assert e.is_Pow and nsimplify(x**0.0) == 1
  340. assert nsimplify(3.333333, tolerance=0.1, rational=True) == Rational(10, 3)
  341. assert nsimplify(3.333333, tolerance=0.01, rational=True) == Rational(10, 3)
  342. assert nsimplify(3.666666, tolerance=0.1, rational=True) == Rational(11, 3)
  343. assert nsimplify(3.666666, tolerance=0.01, rational=True) == Rational(11, 3)
  344. assert nsimplify(33, tolerance=10, rational=True) == Rational(33)
  345. assert nsimplify(33.33, tolerance=10, rational=True) == Rational(30)
  346. assert nsimplify(37.76, tolerance=10, rational=True) == Rational(40)
  347. assert nsimplify(-203.1) == Rational(-2031, 10)
  348. assert nsimplify(.2, tolerance=0) == Rational(1, 5)
  349. assert nsimplify(-.2, tolerance=0) == Rational(-1, 5)
  350. assert nsimplify(.2222, tolerance=0) == Rational(1111, 5000)
  351. assert nsimplify(-.2222, tolerance=0) == Rational(-1111, 5000)
  352. # issue 7211, PR 4112
  353. assert nsimplify(S(2e-8)) == Rational(1, 50000000)
  354. # issue 7322 direct test
  355. assert nsimplify(1e-42, rational=True) != 0
  356. # issue 10336
  357. inf = Float('inf')
  358. infs = (-oo, oo, inf, -inf)
  359. for zi in infs:
  360. ans = sign(zi)*oo
  361. assert nsimplify(zi) == ans
  362. assert nsimplify(zi + x) == x + ans
  363. assert nsimplify(0.33333333, rational=True, rational_conversion='exact') == Rational(0.33333333)
  364. # Make sure nsimplify on expressions uses full precision
  365. assert nsimplify(pi.evalf(100)*x, rational_conversion='exact').evalf(100) == pi.evalf(100)*x
  366. def test_issue_9448():
  367. tmp = sympify("1/(1 - (-1)**(2/3) - (-1)**(1/3)) + 1/(1 + (-1)**(2/3) + (-1)**(1/3))")
  368. assert nsimplify(tmp) == S.Half
  369. def test_extract_minus_sign():
  370. x = Symbol("x")
  371. y = Symbol("y")
  372. a = Symbol("a")
  373. b = Symbol("b")
  374. assert simplify(-x/-y) == x/y
  375. assert simplify(-x/y) == -x/y
  376. assert simplify(x/y) == x/y
  377. assert simplify(x/-y) == -x/y
  378. assert simplify(-x/0) == zoo*x
  379. assert simplify(Rational(-5, 0)) is zoo
  380. assert simplify(-a*x/(-y - b)) == a*x/(b + y)
  381. def test_diff():
  382. x = Symbol("x")
  383. y = Symbol("y")
  384. f = Function("f")
  385. g = Function("g")
  386. assert simplify(g(x).diff(x)*f(x).diff(x) - f(x).diff(x)*g(x).diff(x)) == 0
  387. assert simplify(2*f(x)*f(x).diff(x) - diff(f(x)**2, x)) == 0
  388. assert simplify(diff(1/f(x), x) + f(x).diff(x)/f(x)**2) == 0
  389. assert simplify(f(x).diff(x, y) - f(x).diff(y, x)) == 0
  390. def test_logcombine_1():
  391. x, y = symbols("x,y")
  392. a = Symbol("a")
  393. z, w = symbols("z,w", positive=True)
  394. b = Symbol("b", real=True)
  395. assert logcombine(log(x) + 2*log(y)) == log(x) + 2*log(y)
  396. assert logcombine(log(x) + 2*log(y), force=True) == log(x*y**2)
  397. assert logcombine(a*log(w) + log(z)) == a*log(w) + log(z)
  398. assert logcombine(b*log(z) + b*log(x)) == log(z**b) + b*log(x)
  399. assert logcombine(b*log(z) - log(w)) == log(z**b/w)
  400. assert logcombine(log(x)*log(z)) == log(x)*log(z)
  401. assert logcombine(log(w)*log(x)) == log(w)*log(x)
  402. assert logcombine(cos(-2*log(z) + b*log(w))) in [cos(log(w**b/z**2)),
  403. cos(log(z**2/w**b))]
  404. assert logcombine(log(log(x) - log(y)) - log(z), force=True) == \
  405. log(log(x/y)/z)
  406. assert logcombine((2 + I)*log(x), force=True) == (2 + I)*log(x)
  407. assert logcombine((x**2 + log(x) - log(y))/(x*y), force=True) == \
  408. (x**2 + log(x/y))/(x*y)
  409. # the following could also give log(z*x**log(y**2)), what we
  410. # are testing is that a canonical result is obtained
  411. assert logcombine(log(x)*2*log(y) + log(z), force=True) == \
  412. log(z*y**log(x**2))
  413. assert logcombine((x*y + sqrt(x**4 + y**4) + log(x) - log(y))/(pi*x**Rational(2, 3)*
  414. sqrt(y)**3), force=True) == (
  415. x*y + sqrt(x**4 + y**4) + log(x/y))/(pi*x**Rational(2, 3)*y**Rational(3, 2))
  416. assert logcombine(gamma(-log(x/y))*acos(-log(x/y)), force=True) == \
  417. acos(-log(x/y))*gamma(-log(x/y))
  418. assert logcombine(2*log(z)*log(w)*log(x) + log(z) + log(w)) == \
  419. log(z**log(w**2))*log(x) + log(w*z)
  420. assert logcombine(3*log(w) + 3*log(z)) == log(w**3*z**3)
  421. assert logcombine(x*(y + 1) + log(2) + log(3)) == x*(y + 1) + log(6)
  422. assert logcombine((x + y)*log(w) + (-x - y)*log(3)) == (x + y)*log(w/3)
  423. # a single unknown can combine
  424. assert logcombine(log(x) + log(2)) == log(2*x)
  425. eq = log(abs(x)) + log(abs(y))
  426. assert logcombine(eq) == eq
  427. reps = {x: 0, y: 0}
  428. assert log(abs(x)*abs(y)).subs(reps) != eq.subs(reps)
  429. def test_logcombine_complex_coeff():
  430. i = Integral((sin(x**2) + cos(x**3))/x, x)
  431. assert logcombine(i, force=True) == i
  432. assert logcombine(i + 2*log(x), force=True) == \
  433. i + log(x**2)
  434. def test_issue_5950():
  435. x, y = symbols("x,y", positive=True)
  436. assert logcombine(log(3) - log(2)) == log(Rational(3,2), evaluate=False)
  437. assert logcombine(log(x) - log(y)) == log(x/y)
  438. assert logcombine(log(Rational(3,2), evaluate=False) - log(2)) == \
  439. log(Rational(3,4), evaluate=False)
  440. def test_posify():
  441. x = symbols('x')
  442. assert str(posify(
  443. x +
  444. Symbol('p', positive=True) +
  445. Symbol('n', negative=True))) == '(_x + n + p, {_x: x})'
  446. eq, rep = posify(1/x)
  447. assert log(eq).expand().subs(rep) == -log(x)
  448. assert str(posify([x, 1 + x])) == '([_x, _x + 1], {_x: x})'
  449. p = symbols('p', positive=True)
  450. n = symbols('n', negative=True)
  451. orig = [x, n, p]
  452. modified, reps = posify(orig)
  453. assert str(modified) == '[_x, n, p]'
  454. assert [w.subs(reps) for w in modified] == orig
  455. assert str(Integral(posify(1/x + y)[0], (y, 1, 3)).expand()) == \
  456. 'Integral(1/_x, (y, 1, 3)) + Integral(_y, (y, 1, 3))'
  457. assert str(Sum(posify(1/x**n)[0], (n,1,3)).expand()) == \
  458. 'Sum(_x**(-n), (n, 1, 3))'
  459. A = Matrix([[1, 2, 3], [4, 5, 6 * Abs(x)]])
  460. Ap, rep = posify(A)
  461. assert Ap == A.subs(*reversed(rep.popitem()))
  462. # issue 16438
  463. k = Symbol('k', finite=True)
  464. eq, rep = posify(k)
  465. assert eq.assumptions0 == {'positive': True, 'zero': False, 'imaginary': False,
  466. 'nonpositive': False, 'commutative': True, 'hermitian': True, 'real': True, 'nonzero': True,
  467. 'nonnegative': True, 'negative': False, 'complex': True, 'finite': True,
  468. 'infinite': False, 'extended_real':True, 'extended_negative': False,
  469. 'extended_nonnegative': True, 'extended_nonpositive': False,
  470. 'extended_nonzero': True, 'extended_positive': True}
  471. def test_issue_4194():
  472. # simplify should call cancel
  473. f = Function('f')
  474. assert simplify((4*x + 6*f(y))/(2*x + 3*f(y))) == 2
  475. @XFAIL
  476. def test_simplify_float_vs_integer():
  477. # Test for issue 4473:
  478. # https://github.com/sympy/sympy/issues/4473
  479. assert simplify(x**2.0 - x**2) == 0
  480. assert simplify(x**2 - x**2.0) == 0
  481. def test_as_content_primitive():
  482. assert (x/2 + y).as_content_primitive() == (S.Half, x + 2*y)
  483. assert (x/2 + y).as_content_primitive(clear=False) == (S.One, x/2 + y)
  484. assert (y*(x/2 + y)).as_content_primitive() == (S.Half, y*(x + 2*y))
  485. assert (y*(x/2 + y)).as_content_primitive(clear=False) == (S.One, y*(x/2 + y))
  486. # although the _as_content_primitive methods do not alter the underlying structure,
  487. # the as_content_primitive function will touch up the expression and join
  488. # bases that would otherwise have not been joined.
  489. assert (x*(2 + 2*x)*(3*x + 3)**2).as_content_primitive() == \
  490. (18, x*(x + 1)**3)
  491. assert (2 + 2*x + 2*y*(3 + 3*y)).as_content_primitive() == \
  492. (2, x + 3*y*(y + 1) + 1)
  493. assert ((2 + 6*x)**2).as_content_primitive() == \
  494. (4, (3*x + 1)**2)
  495. assert ((2 + 6*x)**(2*y)).as_content_primitive() == \
  496. (1, (_keep_coeff(S(2), (3*x + 1)))**(2*y))
  497. assert (5 + 10*x + 2*y*(3 + 3*y)).as_content_primitive() == \
  498. (1, 10*x + 6*y*(y + 1) + 5)
  499. assert (5*(x*(1 + y)) + 2*x*(3 + 3*y)).as_content_primitive() == \
  500. (11, x*(y + 1))
  501. assert ((5*(x*(1 + y)) + 2*x*(3 + 3*y))**2).as_content_primitive() == \
  502. (121, x**2*(y + 1)**2)
  503. assert (y**2).as_content_primitive() == \
  504. (1, y**2)
  505. assert (S.Infinity).as_content_primitive() == (1, oo)
  506. eq = x**(2 + y)
  507. assert (eq).as_content_primitive() == (1, eq)
  508. assert (S.Half**(2 + x)).as_content_primitive() == (Rational(1, 4), 2**-x)
  509. assert (Rational(-1, 2)**(2 + x)).as_content_primitive() == \
  510. (Rational(1, 4), (Rational(-1, 2))**x)
  511. assert (Rational(-1, 2)**(2 + x)).as_content_primitive() == \
  512. (Rational(1, 4), Rational(-1, 2)**x)
  513. assert (4**((1 + y)/2)).as_content_primitive() == (2, 4**(y/2))
  514. assert (3**((1 + y)/2)).as_content_primitive() == \
  515. (1, 3**(Mul(S.Half, 1 + y, evaluate=False)))
  516. assert (5**Rational(3, 4)).as_content_primitive() == (1, 5**Rational(3, 4))
  517. assert (5**Rational(7, 4)).as_content_primitive() == (5, 5**Rational(3, 4))
  518. assert Add(z*Rational(5, 7), 0.5*x, y*Rational(3, 2), evaluate=False).as_content_primitive() == \
  519. (Rational(1, 14), 7.0*x + 21*y + 10*z)
  520. assert (2**Rational(3, 4) + 2**Rational(1, 4)*sqrt(3)).as_content_primitive(radical=True) == \
  521. (1, 2**Rational(1, 4)*(sqrt(2) + sqrt(3)))
  522. def test_signsimp():
  523. e = x*(-x + 1) + x*(x - 1)
  524. assert signsimp(Eq(e, 0)) is S.true
  525. assert Abs(x - 1) == Abs(1 - x)
  526. assert signsimp(y - x) == y - x
  527. assert signsimp(y - x, evaluate=False) == Mul(-1, x - y, evaluate=False)
  528. def test_besselsimp():
  529. from sympy.functions.special.bessel import (besseli, besselj, bessely)
  530. from sympy.integrals.transforms import cosine_transform
  531. assert besselsimp(exp(-I*pi*y/2)*besseli(y, z*exp_polar(I*pi/2))) == \
  532. besselj(y, z)
  533. assert besselsimp(exp(-I*pi*a/2)*besseli(a, 2*sqrt(x)*exp_polar(I*pi/2))) == \
  534. besselj(a, 2*sqrt(x))
  535. assert besselsimp(sqrt(2)*sqrt(pi)*x**Rational(1, 4)*exp(I*pi/4)*exp(-I*pi*a/2) *
  536. besseli(Rational(-1, 2), sqrt(x)*exp_polar(I*pi/2)) *
  537. besseli(a, sqrt(x)*exp_polar(I*pi/2))/2) == \
  538. besselj(a, sqrt(x)) * cos(sqrt(x))
  539. assert besselsimp(besseli(Rational(-1, 2), z)) == \
  540. sqrt(2)*cosh(z)/(sqrt(pi)*sqrt(z))
  541. assert besselsimp(besseli(a, z*exp_polar(-I*pi/2))) == \
  542. exp(-I*pi*a/2)*besselj(a, z)
  543. assert cosine_transform(1/t*sin(a/t), t, y) == \
  544. sqrt(2)*sqrt(pi)*besselj(0, 2*sqrt(a)*sqrt(y))/2
  545. assert besselsimp(x**2*(a*(-2*besselj(5*I, x) + besselj(-2 + 5*I, x) +
  546. besselj(2 + 5*I, x)) + b*(-2*bessely(5*I, x) + bessely(-2 + 5*I, x) +
  547. bessely(2 + 5*I, x)))/4 + x*(a*(besselj(-1 + 5*I, x)/2 - besselj(1 + 5*I, x)/2)
  548. + b*(bessely(-1 + 5*I, x)/2 - bessely(1 + 5*I, x)/2)) + (x**2 + 25)*(a*besselj(5*I, x)
  549. + b*bessely(5*I, x))) == 0
  550. assert besselsimp(81*x**2*(a*(besselj(Rational(-5, 3), 9*x) - 2*besselj(Rational(1, 3), 9*x) + besselj(Rational(7, 3), 9*x))
  551. + b*(bessely(Rational(-5, 3), 9*x) - 2*bessely(Rational(1, 3), 9*x) + bessely(Rational(7, 3), 9*x)))/4 + x*(a*(9*besselj(Rational(-2, 3), 9*x)/2
  552. - 9*besselj(Rational(4, 3), 9*x)/2) + b*(9*bessely(Rational(-2, 3), 9*x)/2 - 9*bessely(Rational(4, 3), 9*x)/2)) +
  553. (81*x**2 - Rational(1, 9))*(a*besselj(Rational(1, 3), 9*x) + b*bessely(Rational(1, 3), 9*x))) == 0
  554. assert besselsimp(besselj(a-1,x) + besselj(a+1, x) - 2*a*besselj(a, x)/x) == 0
  555. assert besselsimp(besselj(a-1,x) + besselj(a+1, x) + besselj(a, x)) == (2*a + x)*besselj(a, x)/x
  556. assert besselsimp(x**2* besselj(a,x) + x**3*besselj(a+1, x) + besselj(a+2, x)) == \
  557. 2*a*x*besselj(a + 1, x) + x**3*besselj(a + 1, x) - x**2*besselj(a + 2, x) + 2*x*besselj(a + 1, x) + besselj(a + 2, x)
  558. def test_Piecewise():
  559. e1 = x*(x + y) - y*(x + y)
  560. e2 = sin(x)**2 + cos(x)**2
  561. e3 = expand((x + y)*y/x)
  562. s1 = simplify(e1)
  563. s2 = simplify(e2)
  564. s3 = simplify(e3)
  565. assert simplify(Piecewise((e1, x < e2), (e3, True))) == \
  566. Piecewise((s1, x < s2), (s3, True))
  567. def test_polymorphism():
  568. class A(Basic):
  569. def _eval_simplify(x, **kwargs):
  570. return S.One
  571. a = A(S(5), S(2))
  572. assert simplify(a) == 1
  573. def test_issue_from_PR1599():
  574. n1, n2, n3, n4 = symbols('n1 n2 n3 n4', negative=True)
  575. assert simplify(I*sqrt(n1)) == -sqrt(-n1)
  576. def test_issue_6811():
  577. eq = (x + 2*y)*(2*x + 2)
  578. assert simplify(eq) == (x + 1)*(x + 2*y)*2
  579. # reject the 2-arg Mul -- these are a headache for test writing
  580. assert simplify(eq.expand()) == \
  581. 2*x**2 + 4*x*y + 2*x + 4*y
  582. def test_issue_6920():
  583. e = [cos(x) + I*sin(x), cos(x) - I*sin(x),
  584. cosh(x) - sinh(x), cosh(x) + sinh(x)]
  585. ok = [exp(I*x), exp(-I*x), exp(-x), exp(x)]
  586. # wrap in f to show that the change happens wherever ei occurs
  587. f = Function('f')
  588. assert [simplify(f(ei)).args[0] for ei in e] == ok
  589. def test_issue_7001():
  590. from sympy.abc import r, R
  591. assert simplify(-(r*Piecewise((pi*Rational(4, 3), r <= R),
  592. (-8*pi*R**3/(3*r**3), True)) + 2*Piecewise((pi*r*Rational(4, 3), r <= R),
  593. (4*pi*R**3/(3*r**2), True)))/(4*pi*r)) == \
  594. Piecewise((-1, r <= R), (0, True))
  595. def test_inequality_no_auto_simplify():
  596. # no simplify on creation but can be simplified
  597. lhs = cos(x)**2 + sin(x)**2
  598. rhs = 2
  599. e = Lt(lhs, rhs, evaluate=False)
  600. assert e is not S.true
  601. assert simplify(e)
  602. def test_issue_9398():
  603. from sympy.core.numbers import Number
  604. from sympy.polys.polytools import cancel
  605. assert cancel(1e-14) != 0
  606. assert cancel(1e-14*I) != 0
  607. assert simplify(1e-14) != 0
  608. assert simplify(1e-14*I) != 0
  609. assert (I*Number(1.)*Number(10)**Number(-14)).simplify() != 0
  610. assert cancel(1e-20) != 0
  611. assert cancel(1e-20*I) != 0
  612. assert simplify(1e-20) != 0
  613. assert simplify(1e-20*I) != 0
  614. assert cancel(1e-100) != 0
  615. assert cancel(1e-100*I) != 0
  616. assert simplify(1e-100) != 0
  617. assert simplify(1e-100*I) != 0
  618. f = Float("1e-1000")
  619. assert cancel(f) != 0
  620. assert cancel(f*I) != 0
  621. assert simplify(f) != 0
  622. assert simplify(f*I) != 0
  623. def test_issue_9324_simplify():
  624. M = MatrixSymbol('M', 10, 10)
  625. e = M[0, 0] + M[5, 4] + 1304
  626. assert simplify(e) == e
  627. def test_issue_9817_simplify():
  628. # simplify on trace of substituted explicit quadratic form of matrix
  629. # expressions (a scalar) should return without errors (AttributeError)
  630. # See issue #9817 and #9190 for the original bug more discussion on this
  631. from sympy.matrices.expressions import Identity, trace
  632. v = MatrixSymbol('v', 3, 1)
  633. A = MatrixSymbol('A', 3, 3)
  634. x = Matrix([i + 1 for i in range(3)])
  635. X = Identity(3)
  636. quadratic = v.T * A * v
  637. assert simplify((trace(quadratic.as_explicit())).xreplace({v:x, A:X})) == 14
  638. def test_issue_13474():
  639. x = Symbol('x')
  640. assert simplify(x + csch(sinc(1))) == x + csch(sinc(1))
  641. @_both_exp_pow
  642. def test_simplify_function_inverse():
  643. # "inverse" attribute does not guarantee that f(g(x)) is x
  644. # so this simplification should not happen automatically.
  645. # See issue #12140
  646. x, y = symbols('x, y')
  647. g = Function('g')
  648. class f(Function):
  649. def inverse(self, argindex=1):
  650. return g
  651. assert simplify(f(g(x))) == f(g(x))
  652. assert inversecombine(f(g(x))) == x
  653. assert simplify(f(g(x)), inverse=True) == x
  654. assert simplify(f(g(sin(x)**2 + cos(x)**2)), inverse=True) == 1
  655. assert simplify(f(g(x, y)), inverse=True) == f(g(x, y))
  656. assert unchanged(asin, sin(x))
  657. assert simplify(asin(sin(x))) == asin(sin(x))
  658. assert simplify(2*asin(sin(3*x)), inverse=True) == 6*x
  659. assert simplify(log(exp(x))) == log(exp(x))
  660. assert simplify(log(exp(x)), inverse=True) == x
  661. assert simplify(exp(log(x)), inverse=True) == x
  662. assert simplify(log(exp(x), 2), inverse=True) == x/log(2)
  663. assert simplify(log(exp(x), 2, evaluate=False), inverse=True) == x/log(2)
  664. def test_clear_coefficients():
  665. from sympy.simplify.simplify import clear_coefficients
  666. assert clear_coefficients(4*y*(6*x + 3)) == (y*(2*x + 1), 0)
  667. assert clear_coefficients(4*y*(6*x + 3) - 2) == (y*(2*x + 1), Rational(1, 6))
  668. assert clear_coefficients(4*y*(6*x + 3) - 2, x) == (y*(2*x + 1), x/12 + Rational(1, 6))
  669. assert clear_coefficients(sqrt(2) - 2) == (sqrt(2), 2)
  670. assert clear_coefficients(4*sqrt(2) - 2) == (sqrt(2), S.Half)
  671. assert clear_coefficients(S(3), x) == (0, x - 3)
  672. assert clear_coefficients(S.Infinity, x) == (S.Infinity, x)
  673. assert clear_coefficients(-S.Pi, x) == (S.Pi, -x)
  674. assert clear_coefficients(2 - S.Pi/3, x) == (pi, -3*x + 6)
  675. def test_nc_simplify():
  676. from sympy.simplify.simplify import nc_simplify
  677. from sympy.matrices.expressions import MatPow, Identity
  678. from sympy.core import Pow
  679. from functools import reduce
  680. a, b, c, d = symbols('a b c d', commutative = False)
  681. x = Symbol('x')
  682. A = MatrixSymbol("A", x, x)
  683. B = MatrixSymbol("B", x, x)
  684. C = MatrixSymbol("C", x, x)
  685. D = MatrixSymbol("D", x, x)
  686. subst = {a: A, b: B, c: C, d:D}
  687. funcs = {Add: lambda x,y: x+y, Mul: lambda x,y: x*y }
  688. def _to_matrix(expr):
  689. if expr in subst:
  690. return subst[expr]
  691. if isinstance(expr, Pow):
  692. return MatPow(_to_matrix(expr.args[0]), expr.args[1])
  693. elif isinstance(expr, (Add, Mul)):
  694. return reduce(funcs[expr.func],[_to_matrix(a) for a in expr.args])
  695. else:
  696. return expr*Identity(x)
  697. def _check(expr, simplified, deep=True, matrix=True):
  698. assert nc_simplify(expr, deep=deep) == simplified
  699. assert expand(expr) == expand(simplified)
  700. if matrix:
  701. m_simp = _to_matrix(simplified).doit(inv_expand=False)
  702. assert nc_simplify(_to_matrix(expr), deep=deep) == m_simp
  703. _check(a*b*a*b*a*b*c*(a*b)**3*c, ((a*b)**3*c)**2)
  704. _check(a*b*(a*b)**-2*a*b, 1)
  705. _check(a**2*b*a*b*a*b*(a*b)**-1, a*(a*b)**2, matrix=False)
  706. _check(b*a*b**2*a*b**2*a*b**2, b*(a*b**2)**3)
  707. _check(a*b*a**2*b*a**2*b*a**3, (a*b*a)**3*a**2)
  708. _check(a**2*b*a**4*b*a**4*b*a**2, (a**2*b*a**2)**3)
  709. _check(a**3*b*a**4*b*a**4*b*a, a**3*(b*a**4)**3*a**-3)
  710. _check(a*b*a*b + a*b*c*x*a*b*c, (a*b)**2 + x*(a*b*c)**2)
  711. _check(a*b*a*b*c*a*b*a*b*c, ((a*b)**2*c)**2)
  712. _check(b**-1*a**-1*(a*b)**2, a*b)
  713. _check(a**-1*b*c**-1, (c*b**-1*a)**-1)
  714. expr = a**3*b*a**4*b*a**4*b*a**2*b*a**2*(b*a**2)**2*b*a**2*b*a**2
  715. for _ in range(10):
  716. expr *= a*b
  717. _check(expr, a**3*(b*a**4)**2*(b*a**2)**6*(a*b)**10)
  718. _check((a*b*a*b)**2, (a*b*a*b)**2, deep=False)
  719. _check(a*b*(c*d)**2, a*b*(c*d)**2)
  720. expr = b**-1*(a**-1*b**-1 - a**-1*c*b**-1)**-1*a**-1
  721. assert nc_simplify(expr) == (1-c)**-1
  722. # commutative expressions should be returned without an error
  723. assert nc_simplify(2*x**2) == 2*x**2
  724. def test_issue_15965():
  725. A = Sum(z*x**y, (x, 1, a))
  726. anew = z*Sum(x**y, (x, 1, a))
  727. B = Integral(x*y, x)
  728. bdo = x**2*y/2
  729. assert simplify(A + B) == anew + bdo
  730. assert simplify(A) == anew
  731. assert simplify(B) == bdo
  732. assert simplify(B, doit=False) == y*Integral(x, x)
  733. def test_issue_17137():
  734. assert simplify(cos(x)**I) == cos(x)**I
  735. assert simplify(cos(x)**(2 + 3*I)) == cos(x)**(2 + 3*I)
  736. def test_issue_21869():
  737. x = Symbol('x', real=True)
  738. y = Symbol('y', real=True)
  739. expr = And(Eq(x**2, 4), Le(x, y))
  740. assert expr.simplify() == expr
  741. expr = And(Eq(x**2, 4), Eq(x, 2))
  742. assert expr.simplify() == Eq(x, 2)
  743. expr = And(Eq(x**3, x**2), Eq(x, 1))
  744. assert expr.simplify() == Eq(x, 1)
  745. expr = And(Eq(sin(x), x**2), Eq(x, 0))
  746. assert expr.simplify() == Eq(x, 0)
  747. expr = And(Eq(x**3, x**2), Eq(x, 2))
  748. assert expr.simplify() == S.false
  749. expr = And(Eq(y, x**2), Eq(x, 1))
  750. assert expr.simplify() == And(Eq(y,1), Eq(x, 1))
  751. expr = And(Eq(y**2, 1), Eq(y, x**2), Eq(x, 1))
  752. assert expr.simplify() == And(Eq(y,1), Eq(x, 1))
  753. expr = And(Eq(y**2, 4), Eq(y, 2*x**2), Eq(x, 1))
  754. assert expr.simplify() == And(Eq(y,2), Eq(x, 1))
  755. expr = And(Eq(y**2, 4), Eq(y, x**2), Eq(x, 1))
  756. assert expr.simplify() == S.false
  757. def test_issue_7971_21740():
  758. z = Integral(x, (x, 1, 1))
  759. assert z != 0
  760. assert simplify(z) is S.Zero
  761. assert simplify(S.Zero) is S.Zero
  762. z = simplify(Float(0))
  763. assert z is not S.Zero and z == 0.0
  764. @slow
  765. def test_issue_17141_slow():
  766. # Should not give RecursionError
  767. assert simplify((2**acos(I+1)**2).rewrite('log')) == 2**((pi + 2*I*log(-1 +
  768. sqrt(1 - 2*I) + I))**2/4)
  769. def test_issue_17141():
  770. # Check that there is no RecursionError
  771. assert simplify(x**(1 / acos(I))) == x**(2/(pi - 2*I*log(1 + sqrt(2))))
  772. assert simplify(acos(-I)**2*acos(I)**2) == \
  773. log(1 + sqrt(2))**4 + pi**2*log(1 + sqrt(2))**2/2 + pi**4/16
  774. assert simplify(2**acos(I)**2) == 2**((pi - 2*I*log(1 + sqrt(2)))**2/4)
  775. p = 2**acos(I+1)**2
  776. assert simplify(p) == p
  777. def test_simplify_kroneckerdelta():
  778. i, j = symbols("i j")
  779. K = KroneckerDelta
  780. assert simplify(K(i, j)) == K(i, j)
  781. assert simplify(K(0, j)) == K(0, j)
  782. assert simplify(K(i, 0)) == K(i, 0)
  783. assert simplify(K(0, j).rewrite(Piecewise) * K(1, j)) == 0
  784. assert simplify(K(1, i) + Piecewise((1, Eq(j, 2)), (0, True))) == K(1, i) + K(2, j)
  785. # issue 17214
  786. assert simplify(K(0, j) * K(1, j)) == 0
  787. n = Symbol('n', integer=True)
  788. assert simplify(K(0, n) * K(1, n)) == 0
  789. M = Matrix(4, 4, lambda i, j: K(j - i, n) if i <= j else 0)
  790. assert simplify(M**2) == Matrix([[K(0, n), 0, K(1, n), 0],
  791. [0, K(0, n), 0, K(1, n)],
  792. [0, 0, K(0, n), 0],
  793. [0, 0, 0, K(0, n)]])
  794. assert simplify(eye(1) * KroneckerDelta(0, n) *
  795. KroneckerDelta(1, n)) == Matrix([[0]])
  796. assert simplify(S.Infinity * KroneckerDelta(0, n) *
  797. KroneckerDelta(1, n)) is S.NaN
  798. def test_issue_17292():
  799. assert simplify(abs(x)/abs(x**2)) == 1/abs(x)
  800. # this is bigger than the issue: check that deep processing works
  801. assert simplify(5*abs((x**2 - 1)/(x - 1))) == 5*Abs(x + 1)
  802. def test_issue_19822():
  803. expr = And(Gt(n-2, 1), Gt(n, 1))
  804. assert simplify(expr) == Gt(n, 3)
  805. def test_issue_18645():
  806. expr = And(Ge(x, 3), Le(x, 3))
  807. assert simplify(expr) == Eq(x, 3)
  808. expr = And(Eq(x, 3), Le(x, 3))
  809. assert simplify(expr) == Eq(x, 3)
  810. @XFAIL
  811. def test_issue_18642():
  812. i = Symbol("i", integer=True)
  813. n = Symbol("n", integer=True)
  814. expr = And(Eq(i, 2 * n), Le(i, 2*n -1))
  815. assert simplify(expr) == S.false
  816. @XFAIL
  817. def test_issue_18389():
  818. n = Symbol("n", integer=True)
  819. expr = Eq(n, 0) | (n >= 1)
  820. assert simplify(expr) == Ge(n, 0)
  821. def test_issue_8373():
  822. x = Symbol('x', real=True)
  823. assert simplify(Or(x < 1, x >= 1)) == S.true
  824. def test_issue_7950():
  825. expr = And(Eq(x, 1), Eq(x, 2))
  826. assert simplify(expr) == S.false
  827. def test_issue_22020():
  828. expr = I*pi/2 -oo
  829. assert simplify(expr) == expr
  830. # Used to throw an error
  831. def test_issue_19484():
  832. assert simplify(sign(x) * Abs(x)) == x
  833. e = x + sign(x + x**3)
  834. assert simplify(Abs(x + x**3)*e) == x**3 + x*Abs(x**3 + x) + x
  835. e = x**2 + sign(x**3 + 1)
  836. assert simplify(Abs(x**3 + 1) * e) == x**3 + x**2*Abs(x**3 + 1) + 1
  837. f = Function('f')
  838. e = x + sign(x + f(x)**3)
  839. assert simplify(Abs(x + f(x)**3) * e) == x*Abs(x + f(x)**3) + x + f(x)**3
  840. def test_issue_23543():
  841. # Used to give an error
  842. x, y, z = symbols("x y z", commutative=False)
  843. assert (x*(y + z/2)).simplify() == x*(2*y + z)/2
  844. def test_issue_11004():
  845. def f(n):
  846. return sqrt(2*pi*n) * (n/E)**n
  847. def m(n, k):
  848. return f(n) / (f(n/k)**k)
  849. def p(n,k):
  850. return m(n, k) / (k**n)
  851. N, k = symbols('N k')
  852. half = Float('0.5', 4)
  853. z = log(p(n, k) / p(n, k + 1)).expand(force=True)
  854. r = simplify(z.subs(n, N).n(4))
  855. assert r == (
  856. half*k*log(k)
  857. - half*k*log(k + 1)
  858. + half*log(N)
  859. - half*log(k + 1)
  860. + Float(0.9189224, 4)
  861. )
  862. def test_issue_19161():
  863. polynomial = Poly('x**2').simplify()
  864. assert (polynomial-x**2).simplify() == 0
  865. def test_issue_22210():
  866. d = Symbol('d', integer=True)
  867. expr = 2*Derivative(sin(x), (x, d))
  868. assert expr.simplify() == expr
  869. def test_reduce_inverses_nc_pow():
  870. x, y = symbols("x y", commutative=True)
  871. Z = symbols("Z", commutative=False)
  872. assert simplify(2**Z * y**Z) == 2**Z * y**Z
  873. assert simplify(x**Z * y**Z) == x**Z * y**Z
  874. x, y = symbols("x y", positive=True)
  875. assert expand((x*y)**Z) == x**Z * y**Z
  876. assert simplify(x**Z * y**Z) == expand((x*y)**Z)
  877. def test_nc_recursion_coeff():
  878. X = symbols("X", commutative = False)
  879. assert (2 * cos(pi/3) * X).simplify() == X
  880. assert (2.0 * cos(pi/3) * X).simplify() == X