问题
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