Add是否有一个 sympy 函数可以将、Mul和表达式的方程的所有项提取Div为列表或集合?
例如:
(x**2 +(x-1)*ln(x)+(x+2)/(x-1))
Run Code Online (Sandbox Code Playgroud)
我想得到:
[x**2,(x+1)*ln(x),(x+2)/(x-1)]
Run Code Online (Sandbox Code Playgroud)
穆尔也有同样的事情:
(x-1)*ln(x) : [(x-1),ln(x)]
Run Code Online (Sandbox Code Playgroud)
和部门:
(x+2)/(x-1) : [x+2,x-1]
Run Code Online (Sandbox Code Playgroud)
对于总和或乘积,您可以使用expr.args:
In [1]: ((x**2 +(x-1)*ln(x)+(x+2)/(x-1))).args\nOut[1]:\n\xe2\x8e\x9b 2 x + 2 \xe2\x8e\x9e\n\xe2\x8e\x9cx , \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80, (x - 1)\xe2\x8b\x85log(x)\xe2\x8e\x9f\n\xe2\x8e\x9d x - 1 \xe2\x8e\xa0\n\nIn [2]: ((x-1)*ln(x)).args\nOut[2]: (x - 1, log(x))\nRun Code Online (Sandbox Code Playgroud)\n\n对于除法,SymPy 表示x/y为x*y**-1(没有除法类,只有Mul和Pow)。
In [3]: ((x+2)/(x-1)).args\nOut[3]:\n\xe2\x8e\x9b 1 \xe2\x8e\x9e\n\xe2\x8e\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80, x + 2\xe2\x8e\x9f\n\xe2\x8e\x9dx - 1 \xe2\x8e\xa0\nRun Code Online (Sandbox Code Playgroud)\n\n但是,您可以使用fraction拆分它
In [4]: fraction((x+2)/(x-1))\nOut[4]: (x + 2, x - 1)\nRun Code Online (Sandbox Code Playgroud)\n