Search code examples
calculatorsympypolynomials

How do I show the whole root polynomial below in python?


How do I show the 5 root polynomials below in python? I want it to show every root.

x = Symbol('x')
det_p = x**5 - 5*x**3 + 5*x + 2
eq1 = Eq(det_p,0)
b_solve = solve(eq1)

print('Energias dos Orbitais: ', [s.evalf() for s in b_solve])

Results: Energias dos Orbitais: [-2.00000000000000, -0.618033988749895, 1.61803398874989]


Solution

  • If you are working with polynomials and want to see all roots in their multiplicity then you can use either roots, which gives a dict showing multiplicity or Poly.all_roots which gives a list of possibly repeated roots:

    In [3]: roots(det_p)
    Out[3]: 
    ⎧       1   √5     1   √5   ⎫
    ⎨-2: 1, ─ - ──: 2, ─ + ──: 2⎬
    ⎩       2   2      2   2    ⎭
    
    In [4]: Poly(det_p, x).all_roots()
    Out[4]: 
    ⎡    1   √5  1   √5  1   √5  1   √5⎤
    ⎢-2, ─ - ──, ─ - ──, ─ + ──, ─ + ──⎥
    ⎣    2   2   2   2   2   2   2   2 ⎦
    

    Relevant docs:

    https://docs.sympy.org/latest/modules/polys/reference.html#symbolic-root-finding-algorithms

    https://docs.sympy.org/latest/modules/polys/reference.html#sympy.polys.polytools.Poly.all_roots