The Python Oracle

how to combine exponents? (x**a)**b => x**(a*b)?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Switch On Looping

--

Chapters
00:00 Question
00:53 Accepted answer (Score 7)
01:41 Answer 2 (Score 2)
01:51 Answer 3 (Score 0)
02:02 Thank you

--

Full question
https://stackoverflow.com/questions/3274...

Accepted answer links:
[only if m, n are real]: http://en.wikipedia.org/wiki/Exponent#Fa...
[sympy supports complex numbers]: http://code.google.com/p/sympy/#Features

Answer 3 links:
[this bug]: http://code.google.com/p/sympy/issues/de...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #simplify #sympy #exponent

#avk47



ACCEPTED ANSWER

Score 7


(xm)n = xmn is true only if m, n are real.

>>> import math
>>> x = math.e
>>> m = 2j*math.pi
>>> (x**m)**m      # (e^(2πi))^(2πi) = 1^(2πi) = 1
(1.0000000000000016+0j)
>>> x**(m*m)       # e^(2πi×2πi) = e^(-4π²) ≠ 1
(7.157165835186074e-18-0j)

AFAIK, sympy supports complex numbers, so I believe this simplification should not be done unless you can prove b is real.


Edit: It is also false if x is not positive.

>>> x = -2
>>> m = 2
>>> n = 0.5
>>> (x**m)**n
2.0
>>> x**(m*n)
-2.0

Edit(by gnibbler): Here is the original example with Kenny's restrictions applied

>>> from sympy import symbols 
>>> a,b=symbols('ab', real=True, positive=True)
>>> j=(a**b**5)**(b**10)
>>> print j
a**(b**15)



ANSWER 2

Score 2


a,b,c=symbols('abc',real=True,positive=True)
(a**b**5)**b**10
a**(b**15)#ans



ANSWER 3

Score 0


This may be related to this bug.