test_powsimp.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. from sympy.core.function import Function
  2. from sympy.core.mul import Mul
  3. from sympy.core.numbers import (E, I, Rational, oo, pi)
  4. from sympy.core.singleton import S
  5. from sympy.core.symbol import (Dummy, Symbol, symbols)
  6. from sympy.functions.elementary.exponential import (exp, log)
  7. from sympy.functions.elementary.miscellaneous import (root, sqrt)
  8. from sympy.functions.elementary.trigonometric import sin
  9. from sympy.functions.special.gamma_functions import gamma
  10. from sympy.functions.special.hyper import hyper
  11. from sympy.matrices.expressions.matexpr import MatrixSymbol
  12. from sympy.simplify.powsimp import (powdenest, powsimp)
  13. from sympy.simplify.simplify import (signsimp, simplify)
  14. from sympy.core.symbol import Str
  15. from sympy.abc import x, y, z, a, b
  16. def test_powsimp():
  17. x, y, z, n = symbols('x,y,z,n')
  18. f = Function('f')
  19. assert powsimp( 4**x * 2**(-x) * 2**(-x) ) == 1
  20. assert powsimp( (-4)**x * (-2)**(-x) * 2**(-x) ) == 1
  21. assert powsimp(
  22. f(4**x * 2**(-x) * 2**(-x)) ) == f(4**x * 2**(-x) * 2**(-x))
  23. assert powsimp( f(4**x * 2**(-x) * 2**(-x)), deep=True ) == f(1)
  24. assert exp(x)*exp(y) == exp(x)*exp(y)
  25. assert powsimp(exp(x)*exp(y)) == exp(x + y)
  26. assert powsimp(exp(x)*exp(y)*2**x*2**y) == (2*E)**(x + y)
  27. assert powsimp(exp(x)*exp(y)*2**x*2**y, combine='exp') == \
  28. exp(x + y)*2**(x + y)
  29. assert powsimp(exp(x)*exp(y)*exp(2)*sin(x) + sin(y) + 2**x*2**y) == \
  30. exp(2 + x + y)*sin(x) + sin(y) + 2**(x + y)
  31. assert powsimp(sin(exp(x)*exp(y))) == sin(exp(x)*exp(y))
  32. assert powsimp(sin(exp(x)*exp(y)), deep=True) == sin(exp(x + y))
  33. assert powsimp(x**2*x**y) == x**(2 + y)
  34. # This should remain factored, because 'exp' with deep=True is supposed
  35. # to act like old automatic exponent combining.
  36. assert powsimp((1 + E*exp(E))*exp(-E), combine='exp', deep=True) == \
  37. (1 + exp(1 + E))*exp(-E)
  38. assert powsimp((1 + E*exp(E))*exp(-E), deep=True) == \
  39. (1 + exp(1 + E))*exp(-E)
  40. assert powsimp((1 + E*exp(E))*exp(-E)) == (1 + exp(1 + E))*exp(-E)
  41. assert powsimp((1 + E*exp(E))*exp(-E), combine='exp') == \
  42. (1 + exp(1 + E))*exp(-E)
  43. assert powsimp((1 + E*exp(E))*exp(-E), combine='base') == \
  44. (1 + E*exp(E))*exp(-E)
  45. x, y = symbols('x,y', nonnegative=True)
  46. n = Symbol('n', real=True)
  47. assert powsimp(y**n * (y/x)**(-n)) == x**n
  48. assert powsimp(x**(x**(x*y)*y**(x*y))*y**(x**(x*y)*y**(x*y)), deep=True) \
  49. == (x*y)**(x*y)**(x*y)
  50. assert powsimp(2**(2**(2*x)*x), deep=False) == 2**(2**(2*x)*x)
  51. assert powsimp(2**(2**(2*x)*x), deep=True) == 2**(x*4**x)
  52. assert powsimp(
  53. exp(-x + exp(-x)*exp(-x*log(x))), deep=False, combine='exp') == \
  54. exp(-x + exp(-x)*exp(-x*log(x)))
  55. assert powsimp(
  56. exp(-x + exp(-x)*exp(-x*log(x))), deep=False, combine='exp') == \
  57. exp(-x + exp(-x)*exp(-x*log(x)))
  58. assert powsimp((x + y)/(3*z), deep=False, combine='exp') == (x + y)/(3*z)
  59. assert powsimp((x/3 + y/3)/z, deep=True, combine='exp') == (x/3 + y/3)/z
  60. assert powsimp(exp(x)/(1 + exp(x)*exp(y)), deep=True) == \
  61. exp(x)/(1 + exp(x + y))
  62. assert powsimp(x*y**(z**x*z**y), deep=True) == x*y**(z**(x + y))
  63. assert powsimp((z**x*z**y)**x, deep=True) == (z**(x + y))**x
  64. assert powsimp(x*(z**x*z**y)**x, deep=True) == x*(z**(x + y))**x
  65. p = symbols('p', positive=True)
  66. assert powsimp((1/x)**log(2)/x) == (1/x)**(1 + log(2))
  67. assert powsimp((1/p)**log(2)/p) == p**(-1 - log(2))
  68. # coefficient of exponent can only be simplified for positive bases
  69. assert powsimp(2**(2*x)) == 4**x
  70. assert powsimp((-1)**(2*x)) == (-1)**(2*x)
  71. i = symbols('i', integer=True)
  72. assert powsimp((-1)**(2*i)) == 1
  73. assert powsimp((-1)**(-x)) != (-1)**x # could be 1/((-1)**x), but is not
  74. # force=True overrides assumptions
  75. assert powsimp((-1)**(2*x), force=True) == 1
  76. # rational exponents allow combining of negative terms
  77. w, n, m = symbols('w n m', negative=True)
  78. e = i/a # not a rational exponent if `a` is unknown
  79. ex = w**e*n**e*m**e
  80. assert powsimp(ex) == m**(i/a)*n**(i/a)*w**(i/a)
  81. e = i/3
  82. ex = w**e*n**e*m**e
  83. assert powsimp(ex) == (-1)**i*(-m*n*w)**(i/3)
  84. e = (3 + i)/i
  85. ex = w**e*n**e*m**e
  86. assert powsimp(ex) == (-1)**(3*e)*(-m*n*w)**e
  87. eq = x**(a*Rational(2, 3))
  88. # eq != (x**a)**(2/3) (try x = -1 and a = 3 to see)
  89. assert powsimp(eq).exp == eq.exp == a*Rational(2, 3)
  90. # powdenest goes the other direction
  91. assert powsimp(2**(2*x)) == 4**x
  92. assert powsimp(exp(p/2)) == exp(p/2)
  93. # issue 6368
  94. eq = Mul(*[sqrt(Dummy(imaginary=True)) for i in range(3)])
  95. assert powsimp(eq) == eq and eq.is_Mul
  96. assert all(powsimp(e) == e for e in (sqrt(x**a), sqrt(x**2)))
  97. # issue 8836
  98. assert str( powsimp(exp(I*pi/3)*root(-1,3)) ) == '(-1)**(2/3)'
  99. # issue 9183
  100. assert powsimp(-0.1**x) == -0.1**x
  101. # issue 10095
  102. assert powsimp((1/(2*E))**oo) == (exp(-1)/2)**oo
  103. # PR 13131
  104. eq = sin(2*x)**2*sin(2.0*x)**2
  105. assert powsimp(eq) == eq
  106. # issue 14615
  107. assert powsimp(x**2*y**3*(x*y**2)**Rational(3, 2)
  108. ) == x*y*(x*y**2)**Rational(5, 2)
  109. def test_powsimp_negated_base():
  110. assert powsimp((-x + y)/sqrt(x - y)) == -sqrt(x - y)
  111. assert powsimp((-x + y)*(-z + y)/sqrt(x - y)/sqrt(z - y)) == sqrt(x - y)*sqrt(z - y)
  112. p = symbols('p', positive=True)
  113. reps = {p: 2, a: S.Half}
  114. assert powsimp((-p)**a/p**a).subs(reps) == ((-1)**a).subs(reps)
  115. assert powsimp((-p)**a*p**a).subs(reps) == ((-p**2)**a).subs(reps)
  116. n = symbols('n', negative=True)
  117. reps = {p: -2, a: S.Half}
  118. assert powsimp((-n)**a/n**a).subs(reps) == (-1)**(-a).subs(a, S.Half)
  119. assert powsimp((-n)**a*n**a).subs(reps) == ((-n**2)**a).subs(reps)
  120. # if x is 0 then the lhs is 0**a*oo**a which is not (-1)**a
  121. eq = (-x)**a/x**a
  122. assert powsimp(eq) == eq
  123. def test_powsimp_nc():
  124. x, y, z = symbols('x,y,z')
  125. A, B, C = symbols('A B C', commutative=False)
  126. assert powsimp(A**x*A**y, combine='all') == A**(x + y)
  127. assert powsimp(A**x*A**y, combine='base') == A**x*A**y
  128. assert powsimp(A**x*A**y, combine='exp') == A**(x + y)
  129. assert powsimp(A**x*B**x, combine='all') == A**x*B**x
  130. assert powsimp(A**x*B**x, combine='base') == A**x*B**x
  131. assert powsimp(A**x*B**x, combine='exp') == A**x*B**x
  132. assert powsimp(B**x*A**x, combine='all') == B**x*A**x
  133. assert powsimp(B**x*A**x, combine='base') == B**x*A**x
  134. assert powsimp(B**x*A**x, combine='exp') == B**x*A**x
  135. assert powsimp(A**x*A**y*A**z, combine='all') == A**(x + y + z)
  136. assert powsimp(A**x*A**y*A**z, combine='base') == A**x*A**y*A**z
  137. assert powsimp(A**x*A**y*A**z, combine='exp') == A**(x + y + z)
  138. assert powsimp(A**x*B**x*C**x, combine='all') == A**x*B**x*C**x
  139. assert powsimp(A**x*B**x*C**x, combine='base') == A**x*B**x*C**x
  140. assert powsimp(A**x*B**x*C**x, combine='exp') == A**x*B**x*C**x
  141. assert powsimp(B**x*A**x*C**x, combine='all') == B**x*A**x*C**x
  142. assert powsimp(B**x*A**x*C**x, combine='base') == B**x*A**x*C**x
  143. assert powsimp(B**x*A**x*C**x, combine='exp') == B**x*A**x*C**x
  144. def test_issue_6440():
  145. assert powsimp(16*2**a*8**b) == 2**(a + 3*b + 4)
  146. def test_powdenest():
  147. x, y = symbols('x,y')
  148. p, q = symbols('p q', positive=True)
  149. i, j = symbols('i,j', integer=True)
  150. assert powdenest(x) == x
  151. assert powdenest(x + 2*(x**(a*Rational(2, 3)))**(3*x)) == (x + 2*(x**(a*Rational(2, 3)))**(3*x))
  152. assert powdenest((exp(a*Rational(2, 3)))**(3*x)) # -X-> (exp(a/3))**(6*x)
  153. assert powdenest((x**(a*Rational(2, 3)))**(3*x)) == ((x**(a*Rational(2, 3)))**(3*x))
  154. assert powdenest(exp(3*x*log(2))) == 2**(3*x)
  155. assert powdenest(sqrt(p**2)) == p
  156. eq = p**(2*i)*q**(4*i)
  157. assert powdenest(eq) == (p*q**2)**(2*i)
  158. # -X-> (x**x)**i*(x**x)**j == x**(x*(i + j))
  159. assert powdenest((x**x)**(i + j))
  160. assert powdenest(exp(3*y*log(x))) == x**(3*y)
  161. assert powdenest(exp(y*(log(a) + log(b)))) == (a*b)**y
  162. assert powdenest(exp(3*(log(a) + log(b)))) == a**3*b**3
  163. assert powdenest(((x**(2*i))**(3*y))**x) == ((x**(2*i))**(3*y))**x
  164. assert powdenest(((x**(2*i))**(3*y))**x, force=True) == x**(6*i*x*y)
  165. assert powdenest(((x**(a*Rational(2, 3)))**(3*y/i))**x) == \
  166. (((x**(a*Rational(2, 3)))**(3*y/i))**x)
  167. assert powdenest((x**(2*i)*y**(4*i))**z, force=True) == (x*y**2)**(2*i*z)
  168. assert powdenest((p**(2*i)*q**(4*i))**j) == (p*q**2)**(2*i*j)
  169. e = ((p**(2*a))**(3*y))**x
  170. assert powdenest(e) == e
  171. e = ((x**2*y**4)**a)**(x*y)
  172. assert powdenest(e) == e
  173. e = (((x**2*y**4)**a)**(x*y))**3
  174. assert powdenest(e) == ((x**2*y**4)**a)**(3*x*y)
  175. assert powdenest((((x**2*y**4)**a)**(x*y)), force=True) == \
  176. (x*y**2)**(2*a*x*y)
  177. assert powdenest((((x**2*y**4)**a)**(x*y))**3, force=True) == \
  178. (x*y**2)**(6*a*x*y)
  179. assert powdenest((x**2*y**6)**i) != (x*y**3)**(2*i)
  180. x, y = symbols('x,y', positive=True)
  181. assert powdenest((x**2*y**6)**i) == (x*y**3)**(2*i)
  182. assert powdenest((x**(i*Rational(2, 3))*y**(i/2))**(2*i)) == (x**Rational(4, 3)*y)**(i**2)
  183. assert powdenest(sqrt(x**(2*i)*y**(6*i))) == (x*y**3)**i
  184. assert powdenest(4**x) == 2**(2*x)
  185. assert powdenest((4**x)**y) == 2**(2*x*y)
  186. assert powdenest(4**x*y) == 2**(2*x)*y
  187. def test_powdenest_polar():
  188. x, y, z = symbols('x y z', polar=True)
  189. a, b, c = symbols('a b c')
  190. assert powdenest((x*y*z)**a) == x**a*y**a*z**a
  191. assert powdenest((x**a*y**b)**c) == x**(a*c)*y**(b*c)
  192. assert powdenest(((x**a)**b*y**c)**c) == x**(a*b*c)*y**(c**2)
  193. def test_issue_5805():
  194. arg = ((gamma(x)*hyper((), (), x))*pi)**2
  195. assert powdenest(arg) == (pi*gamma(x)*hyper((), (), x))**2
  196. assert arg.is_positive is None
  197. def test_issue_9324_powsimp_on_matrix_symbol():
  198. M = MatrixSymbol('M', 10, 10)
  199. expr = powsimp(M, deep=True)
  200. assert expr == M
  201. assert expr.args[0] == Str('M')
  202. def test_issue_6367():
  203. z = -5*sqrt(2)/(2*sqrt(2*sqrt(29) + 29)) + sqrt(-sqrt(29)/29 + S.Half)
  204. assert Mul(*[powsimp(a) for a in Mul.make_args(z.normal())]) == 0
  205. assert powsimp(z.normal()) == 0
  206. assert simplify(z) == 0
  207. assert powsimp(sqrt(2 + sqrt(3))*sqrt(2 - sqrt(3)) + 1) == 2
  208. assert powsimp(z) != 0
  209. def test_powsimp_polar():
  210. from sympy.functions.elementary.complexes import polar_lift
  211. from sympy.functions.elementary.exponential import exp_polar
  212. x, y, z = symbols('x y z')
  213. p, q, r = symbols('p q r', polar=True)
  214. assert (polar_lift(-1))**(2*x) == exp_polar(2*pi*I*x)
  215. assert powsimp(p**x * q**x) == (p*q)**x
  216. assert p**x * (1/p)**x == 1
  217. assert (1/p)**x == p**(-x)
  218. assert exp_polar(x)*exp_polar(y) == exp_polar(x)*exp_polar(y)
  219. assert powsimp(exp_polar(x)*exp_polar(y)) == exp_polar(x + y)
  220. assert powsimp(exp_polar(x)*exp_polar(y)*p**x*p**y) == \
  221. (p*exp_polar(1))**(x + y)
  222. assert powsimp(exp_polar(x)*exp_polar(y)*p**x*p**y, combine='exp') == \
  223. exp_polar(x + y)*p**(x + y)
  224. assert powsimp(
  225. exp_polar(x)*exp_polar(y)*exp_polar(2)*sin(x) + sin(y) + p**x*p**y) \
  226. == p**(x + y) + sin(x)*exp_polar(2 + x + y) + sin(y)
  227. assert powsimp(sin(exp_polar(x)*exp_polar(y))) == \
  228. sin(exp_polar(x)*exp_polar(y))
  229. assert powsimp(sin(exp_polar(x)*exp_polar(y)), deep=True) == \
  230. sin(exp_polar(x + y))
  231. def test_issue_5728():
  232. b = x*sqrt(y)
  233. a = sqrt(b)
  234. c = sqrt(sqrt(x)*y)
  235. assert powsimp(a*b) == sqrt(b)**3
  236. assert powsimp(a*b**2*sqrt(y)) == sqrt(y)*a**5
  237. assert powsimp(a*x**2*c**3*y) == c**3*a**5
  238. assert powsimp(a*x*c**3*y**2) == c**7*a
  239. assert powsimp(x*c**3*y**2) == c**7
  240. assert powsimp(x*c**3*y) == x*y*c**3
  241. assert powsimp(sqrt(x)*c**3*y) == c**5
  242. assert powsimp(sqrt(x)*a**3*sqrt(y)) == sqrt(x)*sqrt(y)*a**3
  243. assert powsimp(Mul(sqrt(x)*c**3*sqrt(y), y, evaluate=False)) == \
  244. sqrt(x)*sqrt(y)**3*c**3
  245. assert powsimp(a**2*a*x**2*y) == a**7
  246. # symbolic powers work, too
  247. b = x**y*y
  248. a = b*sqrt(b)
  249. assert a.is_Mul is True
  250. assert powsimp(a) == sqrt(b)**3
  251. # as does exp
  252. a = x*exp(y*Rational(2, 3))
  253. assert powsimp(a*sqrt(a)) == sqrt(a)**3
  254. assert powsimp(a**2*sqrt(a)) == sqrt(a)**5
  255. assert powsimp(a**2*sqrt(sqrt(a))) == sqrt(sqrt(a))**9
  256. def test_issue_from_PR1599():
  257. n1, n2, n3, n4 = symbols('n1 n2 n3 n4', negative=True)
  258. assert (powsimp(sqrt(n1)*sqrt(n2)*sqrt(n3)) ==
  259. -I*sqrt(-n1)*sqrt(-n2)*sqrt(-n3))
  260. assert (powsimp(root(n1, 3)*root(n2, 3)*root(n3, 3)*root(n4, 3)) ==
  261. -(-1)**Rational(1, 3)*
  262. (-n1)**Rational(1, 3)*(-n2)**Rational(1, 3)*(-n3)**Rational(1, 3)*(-n4)**Rational(1, 3))
  263. def test_issue_10195():
  264. a = Symbol('a', integer=True)
  265. l = Symbol('l', even=True, nonzero=True)
  266. n = Symbol('n', odd=True)
  267. e_x = (-1)**(n/2 - S.Half) - (-1)**(n*Rational(3, 2) - S.Half)
  268. assert powsimp((-1)**(l/2)) == I**l
  269. assert powsimp((-1)**(n/2)) == I**n
  270. assert powsimp((-1)**(n*Rational(3, 2))) == -I**n
  271. assert powsimp(e_x) == (-1)**(n/2 - S.Half) + (-1)**(n*Rational(3, 2) +
  272. S.Half)
  273. assert powsimp((-1)**(a*Rational(3, 2))) == (-I)**a
  274. def test_issue_15709():
  275. assert powsimp(3**x*Rational(2, 3)) == 2*3**(x-1)
  276. assert powsimp(2*3**x/3) == 2*3**(x-1)
  277. def test_issue_11981():
  278. x, y = symbols('x y', commutative=False)
  279. assert powsimp((x*y)**2 * (y*x)**2) == (x*y)**2 * (y*x)**2
  280. def test_issue_17524():
  281. a = symbols("a", real=True)
  282. e = (-1 - a**2)*sqrt(1 + a**2)
  283. assert signsimp(powsimp(e)) == signsimp(e) == -(a**2 + 1)**(S(3)/2)
  284. def test_issue_19627():
  285. # if you use force the user must verify
  286. assert powdenest(sqrt(sin(x)**2), force=True) == sin(x)
  287. assert powdenest((x**(S.Half/y))**(2*y), force=True) == x
  288. from sympy.core.function import expand_power_base
  289. e = 1 - a
  290. expr = (exp(z/e)*x**(b/e)*y**((1 - b)/e))**e
  291. assert powdenest(expand_power_base(expr, force=True), force=True
  292. ) == x**b*y**(1 - b)*exp(z)
  293. def test_issue_22546():
  294. p1, p2 = symbols('p1, p2', positive=True)
  295. ref = powsimp(p1**z/p2**z)
  296. e = z + 1
  297. ans = ref.subs(z, e)
  298. assert ans.is_Pow
  299. assert powsimp(p1**e/p2**e) == ans
  300. i = symbols('i', integer=True)
  301. ref = powsimp(x**i/y**i)
  302. e = i + 1
  303. ans = ref.subs(i, e)
  304. assert ans.is_Pow
  305. assert powsimp(x**e/y**e) == ans