当我尝试在像
x
这样的表达式中获取x + 2
的系数时,它是1,但是当我除以y
时,系数是0,我不明白为什么:
>>> from sympy import *
>>> from sympy.abc import x, y
>>> eq = x + 2
>>> eq.coeff(x)
1
>>> (eq/y).coeff(x)
0
如果您阅读
Expr.coeff
的文档字符串,它会提到
In addition, no factoring is done, so 1 + z*(1 + y) is not obtained
from the following:
>>> (x + z*(x + x*y)).coeff(x)
1
因此,它实际上是在检查表达式所写的中的项的因子以获取感兴趣的因子。
x + 2
中有两项,其中一项以 x
作为因子。在 (x + 2)/y
中,只有一项具有 x+2
和 1/y
因子 - 没有 x
因子,因此系数为 0。如果在表达式上使用 expand
,您将得到结果正在寻找。
>>> expand((x + 2)/y).coeff(x)
1/y