2015-04-03 21 views
2

在SymPy 0.7.5(Python 2.7.8-64位)。當常數項爲零或缺失時,我無法從表達式中得到常數項(缺省常數與零常數相同,對嗎?)。sympy:如何得到零缺少常數項

我可以從expr.coeff(x, 0)的表達式中獲得常數項。例如:

isympy ## Load sympy etc... 

(x + 3).coeff(x, 0)  #-> 3 ok 
(x).coeff(x, 0)   #-> 0 ok 
(4*x**2 + 3*x).coeff(x, 0) #-> 0 ok 

現在,如何在這些情況下得到0?

(4*x).coeff(x, 0)  #-> 4 
(4*x**2).coeff(x, 0) #-> 4 why not 0?! 

我敢肯定,這已被要求&以前證明,但至少沒有一些尷尬的解決方法我無法找到答案。 感謝 達里奧

編輯從交互式會話全輸出:

python 
Python 2.7.8 (default, Sep 14 2014, 18:20:38) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> 
>>> import sympy 
>>> 
>>> x= sympy.symbols('x') 
>>> (4*x).coeff(x, 0) 
4 
>>> sympy.__version__ 
'0.7.5' 
+1

那麼它給我0 ...' >>>(4 * X ** 2).coeff(X,0)打印' – Srinath 2015-04-03 15:25:47

+1

@maandoo - 有趣的。它不適合我。請參閱編輯。你介意發佈你的python和sympy版本嗎? – dariober 2015-04-03 17:18:47

回答

0

解決方案:升級到sympy 0.7.6。除非我在做傻事,否則這是一個錯誤0.7.5。

python 
Python 2.7.8 (default, Sep 14 2014, 18:20:38) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import sympy 
>>> x= sympy.symbols('x') 
>>> (4*x).coeff(x, 0) 
0 
>>> sympy.__version__ 
'0.7.6'