假设我有一些表达
from sympy import *
a,b,c,x,y = symbols('a c b x y')
eq=a*x + b*x*y + c*y**2
需要拆分为包含单项式的数组。
我目前的解决方案是
parts = str(eq).split(' + ')
然后,我在数组部分的每个元素上使用eval函数来解释为表达式。
如果不首先将表达式转换为字符串,我可以将多元多项式分割成单项部分?
答案 0 :(得分:0)
你可以explore a sympy
expression using .func
and .args
:
eq.func
> <class 'sympy.core.add.Add'>
eq.args
> (a*x, b*y**2, c*x*y)
这些args中的每一个都是sympy
表达式,可以用同样的方式进行探索:
eq.args[0].func
> <class 'sympy.core.mul.Mul'
eq.args[0].args
> (a, x)
等等。请注意,在表达式树的最终级别,您将需要除.func
和.args
之外的其他函数,例如:
eq.args[0].args[0].name # the a in a*x
> 'a'
eq.args[1].args[1].args[1].n() # the 2 in y**2
> 2.00000000000000