get a list of factors in sympy.factor

强颜欢笑 提交于 2019-12-11 00:59:03

问题


I'm implementing an algorithm, and in it I need to get factor a polynomial f(x) = p(x)q(x) with p and q relatively prime. I can use, of course, sympy.factor, but I would like to construct a function that returns perhaps a list of factors in a sympy.factor result.

For example,

x = sympy.Symbol('x')
y = sympy.Symbol('y')
g = sympy.Poly(y**4 + x*y**3 + y**2 + x*y, y)
sympy.factor(g)

throws out

y*(x + y)*(y**2 + 1)

so perhaps I could build a parser that interprets the string and divides it between the parenthesis rendering the list [y, x+y, y * * 2 + 1], but I would like to know: is there an easier way?


回答1:


You are looking for factor_list, which returns a structured list of the factors. Unfortunately, it looks like there's a bug with this specific polynomail in Python 3. In Python 2, I get

In [1]: factor_list(y**4 + x*y**3 + y**2 + x*y, y)
Out[1]:
⎛   ⎡                    ⎛ 2       ⎞⎤⎞
⎝1, ⎣(y, 1), (x + y, 1), ⎝y  + 1, 1⎠⎦⎠

The first element is the constant coefficient. The second is a list of (factor, power) pairs (the final expression equals constant * factor**power * ... * factor**power).



来源:https://stackoverflow.com/questions/40248557/get-a-list-of-factors-in-sympy-factor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!