Solving cubic polynomials: Difference between revisions

From ASCEND
Jump to navigation Jump to search
Line 30: Line 30:
== Case of negative determinant <math>\Delta<0</math> ==
== Case of negative determinant <math>\Delta<0</math> ==


If <math>\Delta < 0</math> then <math>P^3</math> and <math>P</math> must also be negative, and we must solve
If <math>\Delta = Q^2 - P^3 < 0</math> then <math>P^3</math> and hence <math>P</math> must be positive, and we must enter the complex domain to solve


:<math>z^3 = R</math> or <math>z^3 = \overline{R}</math>
:<math>z^3 = R</math> or <math>z^3 = \overline{R}</math>
Line 36: Line 36:
where
where


:<math>R = Q + i \sqrt{-Q^2 - P^3}</math>
:<math>R = Q \pm i \sqrt{P^3 - Q^2}</math>
:<math>\left|R\right| = \sqrt{Q^2 - Q^2 - P^3} = \sqrt{-P^3} </math>
:<math>\arg R = \theta = \cos^{-1} \left(\frac{Q}{\sqrt{-P^3}}\right)</math>


and the solution for <math>z</math> is then
or equivalently,


:<math>z = \sqrt{-P} \cos\left(\frac{\theta + 2\pi k}{3}\right) + i \sin\left(\frac{\theta + 2\pi k}{3}\right)</math> where <math>k \in \left\lbrace 0,1,2 \right\rbrace</math>, for <math>z^3 = R</math>
:<math>\left|R\right| = \sqrt{Q^2 + P^3 - Q^2} = \sqrt{P^3} </math>
or
:<math>\arg R = \theta = \pm \cos^{-1} \left(\frac{Q}{\sqrt{P^3}}\right)</math>.
:<math>z = \sqrt{-P} \cos\left(\frac{-\theta + 2\pi k}{3}\right) + i \sin\left(\frac{-\theta + 2\pi k}{3}\right)</math> where <math>k \in \left\lbrace 0,1,2 \right\rbrace</math>, for <math>z^3 = \overline{R}</math>


Clearly the these solutions are conjugates of each other.
The solution for <math>z</math> is then


Evaluating <math>y</math>, we have
:<math>z = \sqrt{P} \exp \left(i \frac{\pm \theta + 2\pi k}{3}\right)</math> where <math>k \in \left\lbrace 0,1,2 \right\rbrace</math>, for <math>z^3 = R</math>
 
:<math> y =
 
 
 
[3]{\left|R\right|}~\mathrm{cis}\left(\frac{\theta}{3} + \frac{2\pi}{3}k\right)</math> where <math>k \in \left\lbrace 0,1,2 \right\rbrace</math>  
 
 
 
 
 
, and determines the nature of the roots of the equation.
 
If <math>\Delta</math> is negative, the right hand size of (1) is a complex conjugate pair. Taking the cube root results in six different values (three from each conjugate). When those six values are used to calculate <math>y</math>, the result is:
 
 
 
 
 
 
 
 
 
In the general case, the right side of this equation <math>R = Q \pm \sqrt{\Delta}</math> is complex, and can be written as
 
:<math>z^3 = r~\mathrm{cis}~\theta = r \cos \theta + i r \sin \theta</math>
 
where <math>r = \left|R\right|</math>, and <math>\theta = \arg R</math>.
 
:<math> \sqrt[3]{z} = \sqrt[3]{r} \mathrm{cis}~\left(\frac{\theta}{n} + \frac{2\pi}{n} k \right)</math> for <math>k \in \left\lbrace 1,2,...,n-1 \right\rbrace</math>
 
 
 
 
 
If <math>\Delta = 0</math>, then our solution is the real equation
 
:<math>z^3 = Q</math>
 
which, propagating back to the original cubic, gives the triple root
 
:<math>x = -\frac{a}{3} + \sqrt[3]{Q} - \frac{P}{\sqrt[3]{Q}}</math>


There are six values for <math>z</math> here, arising from combining the three roots <math>k \in \left\lbrace 0,1,2 \right\rbrace</math> and the <math>\pm</math> cases for <math>\theta</math>.


Using these values of <math>z</math>, we can now evaluate the solutions <math>y</math> of the depressed cubic, via <math>y = z + \frac{P}{z}</math>. Here, we make use of the facts that <math>\frac{1}{z} = \frac{\bar{z}}{\left|z\right|}</math> and <math>\left|e^{i\phi}\right|=1</math> and <math>\overline{e^{i\phi}} = e^{-i\phi}</math> and <math>e^{i\phi}+e^{-i\phi}=2\cos\phi</math>


:<math>
\begin{alignat}{2}
y &= \sqrt{P} \exp \left(i \frac{\pm \theta + 2\pi k}{3}\right)  +  \frac{P}{\sqrt{P} \exp \left(i \frac{\pm \theta + 2\pi k}{3}\right)} \\
&= \sqrt{P} \exp \left(i \frac{\pm \theta + 2\pi k}{3}\right)  +  \sqrt{P} \exp \left(-i \frac{\pm \theta + 2\pi k}{3}\right) \\
&= 2\sqrt{P} \cos \left(\frac{\pm \theta + 2\pi k}{3}\right)
\end{alignat}
</math>


It can readily be seen that the six solutions have reduced to three, and that all of the solutions are real,




:<math>y = 2\sqrt{P} \cos \left(\frac{\theta + 2\pi k}{3}\right)</math>


== Case of positive determinant <math>\Delta<0</math> ==


== Calculation checking ==
== Calculation checking ==

Revision as of 08:30, 1 July 2022

There are many sources for how to solve cubic polynomials, often with many peripheral details, so a summarised and simplfied version is helpful when implementing a new code. This page is an attempt to record that.

General solution

We will consider the monic cubic polynomial in <math>x</math>, with <math>a</math>, <math>b</math> and <math>c</math> real-valued:

<math> x^3 + a x^2 + b x + c = 0 </math>

To solve for <math>x</math>, we first calculate the coefficients of an equivalent 'depressed cubic' <math>y^3 - 3P y - 2Q = 0</math>, without loss of generality, by substituting <math>x = y - a/3</math>:

<math>3P = \frac{a^2}{3} - b</math>
<math>2Q = \frac{2}{27} a^3 - \frac{1}{3} ab + c</math>

If <math>P</math> and <math>Q</math> are both zero, then we can stop here with a triple root at <math>y = 0</math>, which is equivalent to <math>x = -\frac{a}{3}</math>.

If only <math>P</math> is zero, then <math>y^3 = 2Q</math>, so <math>x = \sqrt[3]{2Q}-\frac{a}{3}</math>.

If only <math>Q</math> is zero, the depressed cubic is simply <math>y^3 - 3Py = y \left(y^2 - 3P \right) = 0</math>, which has a real root at <math>y=0</math> and two more at <math>y = \pm \sqrt{3P}</math>, which may be either real or imaginary, depending on the sign of <math>P</math>.

Otherwise, the depressed cubic can then be converted into a quadratic in <math>z^3</math> via the 'magic' Vieta substitution <math>y = z + \frac{P}{z}</math>,

<math>\left(z^3\right)^2 - 2Q \left(z^3\right) + P^3 = 0</math>

for which the solution is

<math>z^3 = Q \pm \sqrt{Q^2 - P^3}</math>
(1)

The original cubics (in <math>x</math> and <math>y</math>) have real coefficients, which means that any complex roots must appear with their complex conjugate. As such, the cubic equations must have either one or three real roots, and two or zero complex roots. The determinant <math>\Delta = Q^2 - P^3</math> is critical in determining which case arises.

Case of negative determinant <math>\Delta<0</math>

If <math>\Delta = Q^2 - P^3 < 0</math> then <math>P^3</math> and hence <math>P</math> must be positive, and we must enter the complex domain to solve

<math>z^3 = R</math> or <math>z^3 = \overline{R}</math>

where

<math>R = Q \pm i \sqrt{P^3 - Q^2}</math>

or equivalently,

<math>\left|R\right| = \sqrt{Q^2 + P^3 - Q^2} = \sqrt{P^3} </math>
<math>\arg R = \theta = \pm \cos^{-1} \left(\frac{Q}{\sqrt{P^3}}\right)</math>.

The solution for <math>z</math> is then

<math>z = \sqrt{P} \exp \left(i \frac{\pm \theta + 2\pi k}{3}\right)</math> where <math>k \in \left\lbrace 0,1,2 \right\rbrace</math>, for <math>z^3 = R</math>

There are six values for <math>z</math> here, arising from combining the three roots <math>k \in \left\lbrace 0,1,2 \right\rbrace</math> and the <math>\pm</math> cases for <math>\theta</math>.

Using these values of <math>z</math>, we can now evaluate the solutions <math>y</math> of the depressed cubic, via <math>y = z + \frac{P}{z}</math>. Here, we make use of the facts that <math>\frac{1}{z} = \frac{\bar{z}}{\left|z\right|}</math> and <math>\left|e^{i\phi}\right|=1</math> and <math>\overline{e^{i\phi}} = e^{-i\phi}</math> and <math>e^{i\phi}+e^{-i\phi}=2\cos\phi</math>

<math>

\begin{alignat}{2} y &= \sqrt{P} \exp \left(i \frac{\pm \theta + 2\pi k}{3}\right) + \frac{P}{\sqrt{P} \exp \left(i \frac{\pm \theta + 2\pi k}{3}\right)} \\ &= \sqrt{P} \exp \left(i \frac{\pm \theta + 2\pi k}{3}\right) + \sqrt{P} \exp \left(-i \frac{\pm \theta + 2\pi k}{3}\right) \\ &= 2\sqrt{P} \cos \left(\frac{\pm \theta + 2\pi k}{3}\right) \end{alignat} </math>

It can readily be seen that the six solutions have reduced to three, and that all of the solutions are real,


<math>y = 2\sqrt{P} \cos \left(\frac{\theta + 2\pi k}{3}\right)</math>

Case of positive determinant <math>\Delta<0</math>

Calculation checking

The following sympy code can be used to confirm the above results:

Invalid language.

You need to specify a language like this: <source lang="html">...</source>

Supported languages for syntax highlighting:

a4c, abap, abc, abnf, actionscript, ada, agda, alan, algol, ampl, amtrix, applescript, arc, arm, as400cl, ascend, asciidoc, asp, aspect, assembler, ats, autohotkey, autoit, avenue, awk, ballerina, bat, bbcode, bcpl, bibtex, biferno, bison, blitzbasic, bms, bnf, boo, c, carbon, ceylon, charmm, chill, chpl, clean, clearbasic, clipper, clojure, clp, cmake, cobol, coffeescript, coldfusion, conf, cpp2, critic, crk, crystal, cs_block_regex, csharp, css, d, dart, delphi, diff, dockerfile, dts, dylan, ebnf, ebnf2, eiffel, elixir, elm, email, erb, erlang, euphoria, exapunks, excel, express, factor, fame, fasm, felix, fish, fortran77, fortran90, frink, fsharp, fstab, fx, gambas, gdb, gdscript, go, graphviz, haml, hare, haskell, haxe, hcl, html, httpd, hugo, icon, idl, idlang, inc_luatex, informix, ini, innosetup, interlis, io, jam, jasmin, java, javascript, js_regex, json, jsp, jsx, julia, kotlin, ldif, less, lhs, lilypond, limbo, lindenscript, lisp, logtalk, lotos, lotus, lua, luban, makefile, maple, markdown, matlab, maya, mercury, meson, miranda, mod2, mod3, modelica, moon, ms, msl, mssql, mxml, n3, nasal, nbc, nemerle, netrexx, nginx, nice, nim, nix, nsis, nxc, oberon, objc, ocaml, octave, oorexx, org, os, oz, paradox, pas, pdf, perl, php, pike, pl1, plperl, plpython, pltcl, po, polygen, pony, pov, powershell, pro, progress, ps, psl, pure, purebasic, purescript, pyrex, python, q, qmake, qml, qu, r, rebol, rego, rexx, rnc, rpg, rpl, rst, ruby, rust, s, sam, sas, scad, scala, scilab, scss, shellscript, slim, small, smalltalk, sml, snmp, snobol, solidity, spec, spn, sql, squirrel, styl, svg, swift, sybase, tcl, tcsh, terraform, tex, toml, tsql, tsx, ttcn3, txt, typescript, upc, vala, vb, verilog, vhd, vimscript, vue, wat, whiley, wren, xml, xpp, yaiff, yaml, yaml_ansible, yang, zig, znn