2010-07-18 40 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 
相关问题