Skip to main content
 首页 » 编程设计

python之Sympy coeff 与 as_independent 不一致

2025年04月02日9shihaiming

我有以下代码片段

import sympy 
 
a = sympy.symbols('a') 
b = sympy.symbols('b') 
c = sympy.symbols('c') 
 
print((a*b).coeff(c,0)) 
print((a*b).as_independent(c)[0]) 

我不明白为什么两个打印语句打印不同的输出。根据 coeff 的文档:

You can select terms independent of x by making n=0; in this case 
expr.as_independent(x)[0] is returned (and 0 will be returned instead 
of None): 
 
>>> (3 + 2*x + 4*x**2).coeff(x, 0) 
3 

这是 sympy 中的错误,还是我遗漏了什么?

请您参考如下方法:

这是一个错误。我有一个修复它的请求请求 here .