2010-07-18 41 views
4

如何簡化方程指數在sympy如何結合指數? (x ** a)** b => x **(a * b)?

from sympy import symbols 
a,b,c,d,e,f=symbols('abcdef') 
j=(a**b**5)**(b**10) 
print j 
(a**(b**5))**(b**10) #ans even after using expand simplify 
# desired output 
a**(b**15) 

,如果它是不可能的sympy哪個模塊,我應該導入蟒蛇?

編輯 即使我定義「B」作爲真實的,以及所有其它符號

B =符號(「B」,真=真) 沒有得到簡化指數 它簡化僅當指數常數

a=symbols('a',real=True) 
b=symbols('b',real=True) 
(a**5)**10 
a**50 #simplifies only if exp are numbers 
(a**b**5)**b**10 


(a**(b**5))**b**10 #no simplification 

回答

7

(XÑ = X MN爲真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) 

據我所知,sympy supports complex numbers,所以我相信這種簡化不應該這樣做,除非你能證明b是真實的。


編輯:如果x不是正值,那也是錯誤的。

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

編輯(由gnibbler):這裏是肯尼的限制,原來的例子應用

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

很好的回答,但輸出是'A,B,C,d相同, e,f =符號(「abcdef」,real = True) – 2010-07-18 06:52:09

+0

@gnib:糟糕,看起來我錯過了另一個限制(x> 0)。 – kennytm 2010-07-18 06:57:24

+1

thanx!kenny和gnib,它在我們定義符號時起作用; a = symbols('a',real = True,positive = True) – user394706 2010-07-18 07:28:07

2
a,b,c=symbols('abc',real=True,positive=True) 
(a**b**5)**b**10 
a**(b**15)#ans 
相關問題