Finding roots of polynomials

certik edited this page Feb 8, 2011 · 1 revision
Clone this wiki locally

Example 1

Let's try <math>x^3 + 2x^2 + 8</math>:

In [1]: solve(x**3+2*x**2+8, x)
Out[1]: 
⎡        ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽                                        2/3       
⎢     3 ╱              ⎽⎽⎽⎽        2/3     3 ⎽⎽⎽ ⎛             ⎽⎽⎽⎽⎞        3 
⎢- 18*╲╱  1044 + 108*╲╱ 93   - 36*3    - 3*╲╱ 3 *⎝1044 + 108*╲╱ 93 ⎠     18*╲╱
⎢──────────────────────────────────────────────────────────────────────, ─────
⎢                            ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽                              
⎢                         3 ╱              ⎽⎽⎽⎽                               
⎣                      27*╲╱  1044 + 108*╲╱ 93                                

                                                       ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽    
⎽⎽⎽   ⎽⎽⎽⎽        3 ⎽⎽⎽   ⎽⎽⎽⎽       3 ⎽⎽⎽      2/3 3 ╱              ⎽⎽⎽⎽     
 3 *╲╱ 93  - 54*ⅈ*╲╱ 3 *╲╱ 31  + 174*╲╱ 3  + 2*3   *╲╱  1044 + 108*╲╱ 93   + 6
──────────────────────────────────────────────────────────────────────────────
                                                                              
                                                              ⎛             ⎽⎽
                                                            3*⎝1044 + 108*╲╱ 9

            ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽                        2/3                    
   6 ⎽⎽⎽ 3 ╱              ⎽⎽⎽⎽      ⎛             ⎽⎽⎽⎽⎞             5/6     3 
  • â…ˆ*╲╱ 3 *╲╱ 1044 + 108*╲╱ 93 - 2*⎝1044 + 108*╲╱ 93 ⎠ - 174*â…ˆ*3 18*╲╱
───────────────────────────────────────────────────────────────────────, ───── 2/3 ⎽⎽⎞ 3 ⎠ ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽ ⎽⎽⎽ ⎽⎽⎽⎽ 3 ⎽⎽⎽ ⎽⎽⎽⎽ 3 ⎽⎽⎽ 2/3 3 ╱ ⎽⎽⎽⎽ 3 *╲╱ 93 + 54*ⅈ*╲╱ 3 *╲╱ 31 + 174*╲╱ 3 + 2*3 *╲╱ 1044 + 108*╲╱ 93 - 6 ────────────────────────────────────────────────────────────────────────────── ⎛ ⎽⎽ 3*⎝1044 + 108*╲╱ 9 ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽ 2/3 ⎤ 6 ⎽⎽⎽ 3 ╱ ⎽⎽⎽⎽ ⎛ ⎽⎽⎽⎽⎞ 5/6⎥
  • â…ˆ*╲╱ 3 *╲╱ 1044 + 108*╲╱ 93 - 2*⎝1044 + 108*╲╱ 93 ⎠ + 174*â…ˆ*3 ⎥
───────────────────────────────────────────────────────────────────────⎥ 2/3 ⎥ ⎽⎽⎞ ⎥ 3 ⎠ ⎦

Too messy? Let's latex it:

>>> latex(solve(x**3+2*x**2+8, x))
'$\\begin{bmatrix}\\frac{- 18 \\left(1044 + 108 \\sqrt{93}\\right)^{\\frac{1}{3}} - 36 {3}^{\\frac{2}{3}} - 3 {3}^{\\frac{1}{3}} \\left(1044 + 108 \\sqrt{93}\\right)^{\\frac{2}{3}}}{27 \\left(1044 + 108 \\sqrt{93}\\right)^{\\frac{1}{3}}}, & \\frac{- 174 \\mathbf{\\imath} {3}^{\\frac{5}{6}} - 2 \\left(1044 + 108 \\sqrt{93}\\right)^{\\frac{2}{3}} - 54 \\mathbf{\\imath} {3}^{\\frac{1}{3}} \\sqrt{31} + 2 {3}^{\\frac{2}{3}} \\left(1044 + 108 \\sqrt{93}\\right)^{\\frac{1}{3}} + 18 {3}^{\\frac{1}{3}} \\sqrt{93} + 6 \\mathbf{\\imath} {3}^{\\frac{1}{6}} \\left(1044 + 108 \\sqrt{93}\\right)^{\\frac{1}{3}} + 174 {3}^{\\frac{1}{3}}}{3 \\left(1044 + 108 \\sqrt{93}\\right)^{\\frac{2}{3}}}, & \\frac{- 2 \\left(1044 + 108 \\sqrt{93}\\right)^{\\frac{2}{3}} + 2 {3}^{\\frac{2}{3}} \\left(1044 + 108 \\sqrt{93}\\right)^{\\frac{1}{3}} + 54 \\mathbf{\\imath} {3}^{\\frac{1}{3}} \\sqrt{31} + 18 {3}^{\\frac{1}{3}} \\sqrt{93} - 6 \\mathbf{\\imath} {3}^{\\frac{1}{6}} \\left(1044 + 108 \\sqrt{93}\\right)^{\\frac{1}{3}} + 174 {3}^{\\frac{1}{3}} + 174 \\mathbf{\\imath} {3}^{\\frac{5}{6}}}{3 \\left(1044 + 108 \\sqrt{93}\\right)^{\\frac{2}{3}}}\\end{bmatrix}$'
Python uses \\ in strings. If you do "print latex(roots(x**3+2*x**2+8, x))", you can copy & paste it to the wiki, this gives: <math> \begin{bmatrix}\frac{- 18 \left(1044 + 108 \sqrt{93}\right)^{\frac{1}{3}} - 36 {3}^{\frac{2}{3}} - 3 {3}^{\frac{1}{3}} \left(1044 + 108 \sqrt{93}\right)^{\frac{2}{3}}}{27 \left(1044 + 108 \sqrt{93}\right)^{\frac{1}{3}}}, & \frac{- 174 \mathbf{\imath} {3}^{\frac{5}{6}} - 2 \left(1044 + 108 \sqrt{93}\right)^{\frac{2}{3}} - 54 \mathbf{\imath} {3}^{\frac{1}{3}} \sqrt{31} + 2 {3}^{\frac{2}{3}} \left(1044 + 108 \sqrt{93}\right)^{\frac{1}{3}} + 18 {3}^{\frac{1}{3}} \sqrt{93} + 6 \mathbf{\imath} {3}^{\frac{1}{6}} \left(1044 + 108 \sqrt{93}\right)^{\frac{1}{3}} + 174 {3}^{\frac{1}{3}}}{3 \left(1044 + 108 \sqrt{93}\right)^{\frac{2}{3}}}, & \frac{- 2 \left(1044 + 108 \sqrt{93}\right)^{\frac{2}{3}} + 2 {3}^{\frac{2}{3}} \left(1044 + 108 \sqrt{93}\right)^{\frac{1}{3}} + 54 \mathbf{\imath} {3}^{\frac{1}{3}} \sqrt{31} + 18 {3}^{\frac{1}{3}} \sqrt{93} - 6 \mathbf{\imath} {3}^{\frac{1}{6}} \left(1044 + 108 \sqrt{93}\right)^{\frac{1}{3}} + 174 {3}^{\frac{1}{3}} + 174 \mathbf{\imath} {3}^{\frac{5}{6}}}{3 \left(1044 + 108 \sqrt{93}\right)^{\frac{2}{3}}}\end{bmatrix} </math> Is this correct? Let's check:
In [1]: p = x**3+2*x**2+8

In [2]: r = solve(p, x)

In [3]: p.subs(x, r[0])
Out[3]: 
                                                                              
      ⎛        ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽                                        2/3⎞
      ⎜     3 ╱              ⎽⎽⎽⎽        2/3     3 ⎽⎽⎽ ⎛             ⎽⎽⎽⎽⎞   ⎟
    2*⎝- 18*╲╱  1044 + 108*╲╱ 93   - 36*3    - 3*╲╱ 3 *⎝1044 + 108*╲╱ 93 ⎠   ⎠
8 + ──────────────────────────────────────────────────────────────────────────
                                                    2/3                       
                                 ⎛             ⎽⎽⎽⎽⎞                          
                             729*⎝1044 + 108*╲╱ 93 ⎠                          

2                                                                           3
    ⎛        ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽                                        2/3⎞ 
    ⎜     3 ╱              ⎽⎽⎽⎽        2/3     3 ⎽⎽⎽ ⎛             ⎽⎽⎽⎽⎞   ⎟ 
    ⎝- 18*╲╱  1044 + 108*╲╱ 93   - 36*3    - 3*╲╱ 3 *⎝1044 + 108*╲╱ 93 ⎠   ⎠ 
─ + ─────────────────────────────────────────────────────────────────────────
                                  ⎛             ⎽⎽⎽⎽⎞                        
                            19683*⎝1044 + 108*╲╱ 93 ⎠                        

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

In [4]: p.subs(x, r[0]).evalf()
Out[4]: 2.1316282072803e-14
Cool, looks good. Let's check the other roots:
In [5]: p.subs(x, r[1]).evalf()
Out[5]: -1.77635683940025e-15 - 3.10862446895044e-15*â…ˆ

In [6]: p.subs(x, r[2]).evalf()
Out[6]: -3.5527136788005e-15 + 2.66453525910038e-15*â…ˆ

Example 2

In [1]: solve(x**3 + x**2 - x + 1, x)[1]
Out[1]: 
                                                          ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽   
  3 ⎽⎽⎽   ⎽⎽⎽⎽        3 ⎽⎽⎽   ⎽⎽⎽⎽      3 ⎽⎽⎽      2/3 3 ╱            ⎽⎽⎽⎽    
9*╲╱ 3 *╲╱ 33  + 27*ⅈ*╲╱ 3 *╲╱ 11  + 57*╲╱ 3  + 4*3   *╲╱  171 + 27*╲╱ 33   - 
──────────────────────────────────────────────────────────────────────────────
                                                                              
                                                                ⎛           ⎽⎽
                                                              6*⎝171 + 27*╲╱ 3

              ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽                      2/3            
     6 ⎽⎽⎽ 3 ╱            ⎽⎽⎽⎽      ⎛           ⎽⎽⎽⎽⎞            5/6
12*ⅈ*╲╱ 3 *╲╱  171 + 27*╲╱ 33   - 2*⎝171 + 27*╲╱ 33 ⎠    + 57*ⅈ*3   
────────────────────────────────────────────────────────────────────
   2/3                                                              
⎽⎽⎞                                                                 
3 ⎠                                                                 

Example 3

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

In [2]: a = solve(p, x)

In [3]: p.subs(x, a[0])
Out[3]: 
                                                                              
                                                               ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽
       3 ⎽⎽⎽   ⎽⎽⎽⎽        3 ⎽⎽⎽   ⎽⎽⎽⎽      3 ⎽⎽⎽      2/3 3 ╱             ⎽⎽
     9*╲╱ 3 *╲╱ 33  - 27*ⅈ*╲╱ 3 *╲╱ 11  - 51*╲╱ 3  - 2*3   *╲╱  -153 + 27*╲╱ 3
-1 + ─────────────────────────────────────────────────────────────────────────
                                                                              
                                                                     ⎛        
                                                                   6*⎝-153 + 2

                                                                              
⎽⎽⎽                ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽                       2/3               
⎽⎽        6 ⎽⎽⎽ 3 ╱             ⎽⎽⎽⎽      ⎛            ⎽⎽⎽⎽⎞            5/6   
3   - 6*ⅈ*╲╱ 3 *╲╱  -153 + 27*╲╱ 33   - 2*⎝-153 + 27*╲╱ 33 ⎠    + 51*ⅈ*3      
─────────────────────────────────────────────────────────────────────────── + 
         2/3                                                                  
    ⎽⎽⎽⎽⎞                                                                     
7*╲╱ 33 ⎠                                                                     

                                                                              
⎛                                                          ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽ 
⎜  3 ⎽⎽⎽   ⎽⎽⎽⎽        3 ⎽⎽⎽   ⎽⎽⎽⎽      3 ⎽⎽⎽      2/3 3 ╱             ⎽⎽⎽⎽  
⎝9*╲╱ 3 *╲╱ 33  - 27*ⅈ*╲╱ 3 *╲╱ 11  - 51*╲╱ 3  - 2*3   *╲╱  -153 + 27*╲╱ 33   
──────────────────────────────────────────────────────────────────────────────
                                                                              
                                                                   ⎛          
                                                                36*⎝-153 + 27*

                                                                        2     
               ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽                       2/3            ⎞    ⎛ 
      6 ⎽⎽⎽ 3 ╱             ⎽⎽⎽⎽      ⎛            ⎽⎽⎽⎽⎞            5/6⎟    ⎜ 
- 6*ⅈ*╲╱ 3 *╲╱  -153 + 27*╲╱ 33   - 2*⎝-153 + 27*╲╱ 33 ⎠    + 51*ⅈ*3   ⎠    ⎝9
───────────────────────────────────────────────────────────────────────── + ──
       4/3                                                                    
  ⎽⎽⎽⎽⎞                                                                       
╲╱ 33 ⎠                                                                       

                                                                              
                                                         ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽   
 3 ⎽⎽⎽   ⎽⎽⎽⎽        3 ⎽⎽⎽   ⎽⎽⎽⎽      3 ⎽⎽⎽      2/3 3 ╱             ⎽⎽⎽⎽    
  • ╲╱ 3 *╲╱ 33 - 27*â…ˆ*╲╱ 3 *╲╱ 11 - 51*╲╱ 3 - 2*3 *╲╱ -153 + 27*╲╱ 33 -
────────────────────────────────────────────────────────────────────────────── ⎛ 216*⎝-153 + 27*╲ 3 ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽ 2/3 ⎞ 6 ⎽⎽⎽ 3 ╱ ⎽⎽⎽⎽ ⎛ ⎽⎽⎽⎽⎞ 5/6⎟ 6*ⅈ*╲╱ 3 *╲╱ -153 + 27*╲╱ 33 - 2*⎝-153 + 27*╲╱ 33 ⎠ + 51*ⅈ*3 ⎠ ─────────────────────────────────────────────────────────────────────── 2 ⎽⎽⎽⎽⎞ ╱ 33 ⎠ In [4]: p.subs(x, a[0]).evalf() Out[4]: 1.11022302462516e-14 + 1.17683640610267e-14*ⅈ In [5]: p.subs(x, a[1]).evalf() Out[5]: 9.54791801177635e-15 In [6]: p.subs(x, a[2]).evalf() Out[6]: 1.33226762955019e-14 - 1.32116539930394e-14*ⅈ In [7]:

Category:Documentation