test_simplify.py 41 KB

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