Skip to content

# Finding Roots of Polynomials

Jeromie Kirchoff edited this page May 31, 2019 · 1 revision

# Example 1

Let's try :

from sympy import roots
from sympy import solve
from sympy import symbols
from sympy import latex

x, y = symbols('x y')
solve(x**3+2*x**2+8, x)

> [-2/3 - 2/(3*(-1/2 - sqrt(3)*I/2)*(3*sqrt(93)/2 + 29/2)**(1/3)) - 2*(-1/2 - sqrt(3)*I/2)*(3*sqrt(93)/2 + 29/2)**(1/3)/3, -2/3 - 2*(-1/2 + sqrt(3)*I/2)*(3*sqrt(93)/2 + 29/2)**(1/3)/3 - 2/(3*(-1/2 + sqrt(3)*I/2)*(3*sqrt(93)/2 + 29/2)**(1/3)), -2*(3*sqrt(93)/2 + 29/2)**(1/3)/3 - 2/3 - 2/(3*(3*sqrt(93)/2 + 29/2)**(1/3))]

Too messy? Let's latex it:

latex(solve(x**3+2*x**2+8, x))

> '\\left[ - \\frac{2}{3} - \\frac{2}{3 \\left(- \\frac{1}{2} - \\frac{\\sqrt{3} i}{2}\\right) \\sqrt[3]{\\frac{3 \\sqrt{93}}{2} + \\frac{29}{2}}} - \\frac{2 \\left(- \\frac{1}{2} - \\frac{\\sqrt{3} i}{2}\\right) \\sqrt[3]{\\frac{3 \\sqrt{93}}{2} + \\frac{29}{2}}}{3}, \\  - \\frac{2}{3} - \\frac{2 \\left(- \\frac{1}{2} + \\frac{\\sqrt{3} i}{2}\\right) \\sqrt[3]{\\frac{3 \\sqrt{93}}{2} + \\frac{29}{2}}}{3} - \\frac{2}{3 \\left(- \\frac{1}{2} + \\frac{\\sqrt{3} i}{2}\\right) \\sqrt[3]{\\frac{3 \\sqrt{93}}{2} + \\frac{29}{2}}}, \\  - \\frac{2 \\sqrt[3]{\\frac{3 \\sqrt{93}}{2} + \\frac{29}{2}}}{3} - \\frac{2}{3} - \\frac{2}{3 \\sqrt[3]{\\frac{3 \\sqrt{93}}{2} + \\frac{29}{2}}}\\right]'

Python uses \\ in strings.

print(latex(roots(x**3+2*x**2+8, x)))

> \left\{ - \frac{2}{3} - \frac{2}{3 \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{3 \sqrt{93}}{2} + \frac{29}{2}}} - \frac{2 \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{3 \sqrt{93}}{2} + \frac{29}{2}}}{3} : 1, \  - \frac{2}{3} - \frac{2 \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{3 \sqrt{93}}{2} + \frac{29}{2}}}{3} - \frac{2}{3 \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{3 \sqrt{93}}{2} + \frac{29}{2}}} : 1, \  - \frac{2 \sqrt[3]{\frac{3 \sqrt{93}}{2} + \frac{29}{2}}}{3} - \frac{2}{3} - \frac{2}{3 \sqrt[3]{\frac{3 \sqrt{93}}{2} + \frac{29}{2}}} : 1\right\}

Is this correct? Let's check:

p = x**3+2*x**2+8
r = solve(p, x)
p.subs(x, r[0])

> 8 + (-2/3 - 2/(3*(-1/2 - sqrt(3)*I/2)*(3*sqrt(93)/2 + 29/2)**(1/3)) - 2*(-1/2 - sqrt(3)*I/2)*(3*sqrt(93)/2 + 29/2)**(1/3)/3)**3 + 2*(-2/3 - 2/(3*(-1/2 - sqrt(3)*I/2)*(3*sqrt(93)/2 + 29/2)**(1/3)) - 2*(-1/2 - sqrt(3)*I/2)*(3*sqrt(93)/2 + 29/2)**(1/3)/3)**2

Hm, is this zero? Let's use a "brute force":

p.subs(x, r[0]).evalf()

> 0.e-129 + 0.e-129*I

Cool, looks good. Let's check the other roots:

p.subs(x, r[1]).evalf()

> 0.e-129 - 0.e-129*I

p.subs(x, r[2]).evalf()

> 0.e-121

# Example 2

solve(x**3 + x**2 - x + 1, x)[1]

> -1/3 - (-1/2 + sqrt(3)*I/2)*(3*sqrt(33) + 19)**(1/3)/3 - 4/(3*(-1/2 + sqrt(3)*I/2)*(3*sqrt(33) + 19)**(1/3))

# Example 3

p = x**3+x**2+x-1
a = solve(p, x)
p.subs(x, a[0])
> -4/3 + (-1/2 - sqrt(3)*I/2)*(17/27 + sqrt(33)/9)**(1/3) + (-1/3 + (-1/2 - sqrt(3)*I/2)*(17/27 + sqrt(33)/9)**(1/3) - 2/(9*(-1/2 - sqrt(3)*I/2)*(17/27 + sqrt(33)/9)**(1/3)))**3 - 2/(9*(-1/2 - sqrt(3)*I/2)*(17/27 + sqrt(33)/9)**(1/3)) + (-1/3 + (-1/2 - sqrt(3)*I/2)*(17/27 + sqrt(33)/9)**(1/3) - 2/(9*(-1/2 - sqrt(3)*I/2)*(17/27 + sqrt(33)/9)**(1/3)))**2

p.subs(x, a[0]).evalf()
> 0.e-128 + 0.e-129*I

p.subs(x, a[1]).evalf()
> 0.e-128 - 0.e-129*I

p.subs(x, a[2]).evalf()
> -0.e-126

Category:Documentation

##### Clone this wiki locally
You can’t perform that action at this time.