sympy 从根得出特定的多项式表达式

sympy reach the specific polinomial expression from roots

我有多项式: x^4+x^3+x^2+x+1。 我们可以分解下面的表达式: (x^2+ax+1)(x^2+bx+1)。 我通过以下方式找到了 4 个根: solve((x**4)+(x**3)+(x**2)+(x)+1) 有没有办法获得上面的表达式? 谢谢大家

多项式不考虑有理数。它完全分解包含所有 4 个根的任何域。在这种情况下,您显示的二次因式分解是 QQ<sqrt(5)>:

中的因式分解
In [15]: x = Symbol('x')

In [16]: p = x**4 + x**3 + x**2 + x + 1

In [17]: p
Out[17]: 
 4    3    2        
x  + x  + x  + x + 1

In [18]: roots(p)
Out[18]: 
⎧                 ________                      ________                      ________                      ________   ⎫
⎪  1   √5        ╱ √5   5        1   √5        ╱ √5   5        √5   1        ╱ 5   √5        √5   1        ╱ 5   √5    ⎪
⎨- ─ + ── - ⅈ⋅  ╱  ── + ─ : 1, - ─ + ── + ⅈ⋅  ╱  ── + ─ : 1, - ── - ─ - ⅈ⋅  ╱  ─ - ── : 1, - ── - ─ + ⅈ⋅  ╱  ─ - ── : 1⎬
⎪  4   4      ╲╱   8    8        4   4      ╲╱   8    8        4    4     ╲╱   8   8         4    4     ╲╱   8   8     ⎪
⎩                                                                                                                      ⎭

In [19]: factor(p, extension=sqrt(5))
Out[19]: 
⎛ 2     ⎛1   √5⎞    ⎞ ⎛ 2     ⎛1   √5⎞    ⎞
⎜x  + x⋅⎜─ - ──⎟ + 1⎟⋅⎜x  + x⋅⎜─ + ──⎟ + 1⎟
⎝       ⎝2   2 ⎠    ⎠ ⎝       ⎝2   2 ⎠    ⎠

你可以从根本上做到这一点:

In [21]: r1, r2, r3, r4 = roots(p)

In [22]: expand((x-r1)*(x-r2))
Out[22]: 
 2   x   √5⋅x    
x  + ─ + ──── + 1
     2    2      

In [23]: expand((x-r3)*(x-r4))
Out[23]: 
 2   √5⋅x   x    
x  - ──── + ─ + 1
      2     2  

以下方法为多项式的每个系数创建一个方程:

from sympy import Eq, solve, Poly
from sympy.abc import x, a, b

p1 = Poly((x ** 2 + a * x + 1) * (x ** 2 + b * x + 1), x)
p2 = Poly(x ** 4 + x ** 3 + x ** 2 + x + 1, x)
solve([Eq(p1_coef, p2_coef) for p1_coef, p2_coef in zip(p1.all_coeffs(), p2.all_coeffs())])

这导致两个解决方案(交换符号):

[{a: 1/2 - sqrt(5)/2, b: 1/2 + sqrt(5)/2},
 {a: 1/2 + sqrt(5)/2, b: 1/2 - sqrt(5)/2}]