Solving cubics

An intro for Complex Variables

The imaginary unit i is often introduced as a tool to help us solve the quadratic equation (z^2)+1=0 and, ultimately, all quadratic equations.  Historically, though, early renaissance mathematicians viewed the extraction of negative square roots as nonsense and simply took it as a sign that the originating quadratic had no solutions.  In the solutions of cubics, however, sometimes imaginary quantities arise in the solution of problems with only real roots.  Thus, it was the solution of the cubic that truly motivated the development of complex numbers.

Historical outline

Quadratic formula

Euclid (circa 300 BC) was able to solve quadratic equations, though geometrically and not really completely.

Diophantus (circa 200 AD) knew improvements to Euclid’s technique that looked more algebraic but still incomplete.

Brahmagupta (circa 600 AD) presented a complete version of the quadratic formula, though he rejected negative roots.

al-Khwarismi (circa 820 AD) presented an algebraic version.  He did coin al-jabr , after all.

Descartes in 1637 published the quadratic formula as we know it today in La Geometrie.

Cubic equations

del Ferro is generally credited with the first solution of the cubic - circa 1500, though his work has been lost.

Tartaglia learns of del Ferro’s solution and passes it on to Cardano, though he supposedly swears him to secrecy.

In 1545 Cardano publishes the solution of the cubic in his Ars Magna.  It is this publication on which we base our work.

Quartic equations

Ferrari is credited with the solution of the general quartic in 1540.

Cardano publishes Ferrari’s solution in the same Ars Magna.

Higher degree equations

In 1826, Abel proves that the general quintic is not solvable by radicals.

In 1832, Galois provides a general framework to understand when a polynomial is solvable by radicals.  This framework is now called Galois Theory and is a fundamental part of graduate level abstract algebra.

The quadratic formula

We all know the quadratic formula and, not surprisingly, so does Mathematica.  Since any quadratic can be transformed to a monic quadratic by dividing by the lead coefficient, without changing the roots, it’s no loss of generality to write the quadratic formula for (x^2)+a x +b.

Solve[(x^2)+a*x+b==0,x]

Note that you can tell if the quadratic has real or complex roots simply by checking the sign of (a^2)-4b.

One standard way to derive the quadratic formula is to complete the square of the first two terms to get something easily solvable for x.

(x^2)+a x+b=((x^2)+a x+((a^2)/4))+b-((a^2)/4)=(((x+(a/2)))^2)+b-((a^2)/4)=0

so

x=-(a/2)+ ±(Sqrt[((a^2)/4)-b])=((-a +±(Sqrt[(a^2)-4b]))/2).

Here’s another approach that’s more analogous to how we’ll treat the cubic.  I call this the shifty trick.  We start with the substitution x->y+t (that’s the shift!).

Clear[f];
f[x_] = (x^2)+a*x+b;
f[y+t]

We expand this and collect the y-terms.

Collect[f[y+t],y]

Note that we can choose t in such a way as to make the y term vanish; just pick t=-a/2.

g[y_]=Collect[f[y-a/2],y]

Now, this is a different polynomial with different roots, but those roots are related to the roots of f in a simple way - if y is root of g, then x=y-a/2 is a root of f. Furthermore, the roots of g are easy to find, namely

gSolutions = Solve[g[y]==0,y]

Again, to get back the solutions of x, we simply add back the -a/2.

y-(a/2)/.gSolutions

Example

Of course, we can always apply the formula we just derived straight away but, again, in an effort to mirror what will happen for cubics, let’s outline the approach for a specific polynomial.  Suppose we want to solve

2(x^2)-x-3==0.

It’s not hard to see that x=-1 is one solution, after which you can factor the polynomial pretty easily to get that x=3/2 is the other.  Let’s try the shifty approach, though.  We first define f and divide through by the leading coefficient.

f[x_] = 2(x^2)-x-3;
f[x_] = Expand[f[x]/2]

Now, a=-1/2 and b=-3/2.  If we shifted f by -a/2, we would get the polynomial g(y)=(y^2)+b-(a^2)/4.

g[y_] = With[
{a=-1/2,b=-3/2},
(y^2)-((a^2)/4)+b]

This is easy to solve for y

gSolutions= Solve[g[y]==0,y]

We translate back to get the roots of f.

With[{a=-1/2},
y -a/2 /. gSolutions
]

A cubic mystery

Here’s a bit of a mystery:

roots = Solve[(x^3)-3x+1==0,x];
Column[Framed /@ roots, AllowScriptLevelChange->False]

Why the imaginary portions?  It seems clear that the roots are all real.  Just look at a plot.

Plot[(x^3)-3x+1,{x,-2,2}]

We can check, though, that the roots really work.  Like the real parts, the imaginary parts all cancel out leaving zero upon expansion.

Simplify[(x^3)-3x+1 /. roots]

We can also check the numerical values and see that the imaginary parts of the roots are all zero.

N[roots]

So, why can’t we express these roots without the is?

Solving cubics Cardano’s way

The depressed cubic

The general monic cubic can be written

f[x_] = (x^3)+a*(x^2)+b*x+c;

A depressed cubic is one where the coefficient of (x^2) is zero.  Any cubic can be transformed into a depressed cubic by shifting x appropriately.  In the case of a monic cubic, we get

Collect[f[y+t],y]

The coefficient of (y^2) will be zero if t=-a/3.  Explicitly,

g[y_] = Collect[f[y-a/3],y]

Thus, we can understand the general, monic cubic (x^3)+a (x^2)+b x+c by studying (y^3)+p y+q, where

p=-((a^2)/3)+b     and    q=((2 (a^3))/27)-((a b)/3)+c.

If we know the roots of g, we simply shift back by -a/3 to get the roots of f.

Example

Let f(x)=(x^3)+3(x^2)+9x-13.  This shifted polynomial g(y) is

a=3; b = 9; c=-13;
f[x_] = (x^3)+a*(x^2)+b*x+c;
g[y_] = (y^3)+(-((a^2)/3)+b)y+((2 (a^3))/27)-((a b)/3)+c

Note that g(2)=0.

g[2]

Since a=3 and x=y-a/3, one root of f should be 2-3/3.  Thus, f(1)=0.

f[1]

Solving the depressed cubic

Now, let’s focus on g(x)=(x^3)+p x+q.  Might as well try the shifty trick again.

Clear[p,q];
g[y_]=(y^3)+p*y+q;
expanded = Expand[g[u+v]]

Now, we’re going to leave the (u^3), the (v^3), and the constant q and try to factor the remaining part.

expanded /. Plus[u^3,v^3,q,theRest___] :> u^3+v^3+q+Factor[Plus[theRest]]

Note that the last term is gone if v=-p/(3u).

h[u_] = Collect[g[u-p/(3u)],u]

If we multiply this through by (u^3), we obtain a quadratic in (u^3) which we can solve for (u^3) using the quadratic formula.  This is exactly where the imaginary unit might sneak in!!

Mathematica’s Solve command solves only for symbols, not more complicated expressions.  So we’re going to replace the u^3 with a single symbol u3.

Clear[u3];
h[u3_] = h[u] /. {u^3 -> u3,u^(-3)->1/u3}

Again, if we solve this for u3, we should obtain something that looks like the quadratic formula was applied.  There should be two roots for u3, which I’m mysteriously going to call u3 and v3 - even though h only involved u3.

Clear[u3,v3];
{u3,v3} =u3/.Solve[h[u3]==0,u3]

This expression is just fine but it might be worth mentioning that, in most texts, it’s written as follows.

{u3,v3} = Expand[Simplify[{u3,v3}]] /. Sqrt[s_]:>18Sqrt[Expand[s/(18^2)]]

Now, remember that u3 was shorthand for (u^3) and that v was already defined via v=-p/(3u).  Thus, I guess that v3 should be shorthand for (v^3)=-(p^3)/(27(u^3)).  The crazy thing is that this v3 is the same as the one I’ve just defined via the Solve command above.  Put another way, the following should be zero.

-(p^3)/(27u3)-v3

Hmm... Well, how about this:

Simplify[%]

Now remember that x=u+v and we now know (u^3) and (v^3).  If we take the cube roots of (u^3) and (v^3) and add the results, we get a root of g.  Thus,

((-(q/2)-(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3))+((-(q/2)+(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3))

is a solution.  Note that complex quantities will enter the solution precisely if (p^3)/27+(q^2)/4<0, but that doesn’t mean the final combination is complex!  Note that (p^3)/27+(q^2)/4 is a constant multiple of the discriminant of the polynomial, which can be used to characterize the type of roots, as we’ll see in a bit.

Discriminant[(x^3)+p*x+q,x]

Example

Let’s return to g(x)=(x^3)+6x-20, with p=6 and q=-20.  We already know that 2 is a root.  Simply applying the formula leads to

root1 = With[{p=6,q=-20},
((-(q/2)-(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3))+((-(q/2)+(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3))]

Looks complicated!  Then again,

root1//FullSimplify

Now, since we know that 2 is a root, we should be able to divide off the x-2 to express g(x) as a product:

(x^3)+6x-20=(x-2)((x^2)+2x+10).

There should be two more roots obtainable by applying the quadratic formula to (x^2)+2x+10.  We should get the same result apply’s Cardano’s technique with different branches of the cube root.  Exactly how to pick those branches, though, is a bit subtle and we’ll discuss that in a bit.

root2 = With[{p=6,q=-20},
(E^(2π I/3))((-(q/2)-(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3))+(E^(-2π I/3))((-(q/2)+(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3))];
root3 = With[{p=6,q=-20},
(E^(-2π I/3))((-(q/2)-(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3))+(E^(2π I/3))((-(q/2)+(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3))];
FullSimplify[{root2,root3}]

Example

Let’s return to g(x)=(x^3)-3x+1.  Now, computing all three roots in one fell swoop, we get

{root1,root2,root3} = With[{p=-3,q=1},
{((-(q/2)-(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3))+((-(q/2)+(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3)),
(E^(2π I/3))((-(q/2)-(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3))+(E^(-2π I/3))((-(q/2)+(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3)),
(E^(-2π I/3))((-(q/2)-(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3))+(E^(2π I/3))((-(q/2)+(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3))}]

N[{root1,root2,root3}]

Now, they look complex valued but, in fact, are real.  In fact they’re the same real numbers we computed with our original Solve command.  Again, the complex expressions arise because the expression 12(p^3)+81(q^2) underneath the square root is negative.

With[{p=-3,q=1},(p^3)/27+(q^2)/4]

Characterizing the roots

Recall that there are three branches of the complex cube root and, in our formula, we have two cube roots:

((u^3)^(1/3))+((v^3)^(1/3))=((-(q/2)-(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3))+((-(q/2)-(Sqrt[((p^3)/27)+((q^2)/4)]))^(1/3))

If the branches could be chosen independently of one another, that would lead to nine different solutions but, in fact, they cannot be chosen independently.  Recall that v was chosen so that p+3 u v=0 and this implies that the product u v is real.  Given one branch of the cube root to apply to (u^3), there’s only one branch to apply to (v^3) so that the resulting product is real, yielding three roots.

When (p^3)/27+(q^2)/4>0, we know that (u^3) and (v^3) are both real.  Let (Subscript[u, 1]) and (Subscript[v, 1]) denote the real cube roots of (u^3) and (v^3).  Then (Subscript[u, 1])+(Subscript[v, 1]) is one real root of g.  The other two solutions are given by choosing different branches of the cube root, which we do by multiplying (Subscript[u, 1]) by (e^(2π i/3)) or (e^(-2π i/3)).  Suppose (Subscript[u, 2])=(e^(2π i/3))(Subscript[u, 1]) and we wish to choose (Subscript[v, 2]) to be one of the three cube roots of (v^3) so that (Subscript[u, 2])+(Subscript[v, 2]) is a root of g.  Then, as mentioned in the last paragraph, we must have (Subscript[u, 2])(Subscript[v, 2])∈ℝ.  Thus we, must have (Subscript[v, 2])=(e^(-2π i/3))(Subscript[v, 1]) so that

(Subscript[u, 2])(Subscript[v, 2])=(e^(2π i/3))(Subscript[u, 1])(e^(-2π i/3))(Subscript[v, 1])=(e^0)(Subscript[u, 1])(Subscript[v, 1])=(Subscript[u, 1])(Subscript[v, 1])∈ℝ.

Similarly, we pick (Subscript[u, 3])=(e^(-2π i/3))(Subscript[u, 1]) and (Subscript[v, 3])=(e^(2π i/3))(Subscript[v, 1]).  Note that (Subscript[u, 2])+(Subscript[v, 2]) and (Subscript[u, 3])+(Subscript[v, 3]) are complex conjugates of one another.

When (p^3)/27+(q^2)/4<0, we know that (u^3) and (v^3) are complex conjugates.  Let (Subscript[u, 1]) denote the principal cube root of (u^3) - i.e., the one obtained by dividing the argument by 3.  Now, to ensure that (Subscript[u, 1])(Subscript[v, 1])∈ℝ, we must have that (Subscript[v, 1]) is the complex conjugate of (Subscript[u, 1]).  Note then that (Subscript[u, 1])+(Subscript[v, 1])∈ℝ.  Similar considerations apply to the other roots; thus, in this case, we have three real roots - all expressed in terms of complex numbers!

When (p^3)/27+(q^2)/4=0, we have (u^3)=(v^3)=-q/2.  We take (Subscript[u, 1])=(Subscript[v, 1])=((-q/2)^(1/3)), using the real cube root.  Note then that

(Subscript[u, 2])+(Subscript[v, 2])=(e^(2π i/3))(Subscript[u, 1])+(e^(-2π i/3))(Subscript[v, 1])=(e^(-2π i/3))(Subscript[u, 1])+(e^(2π i/3))(Subscript[v, 1])=(Subscript[u, 3])+(Subscript[v, 3]).

We therefore have a repeated real root.  If q=0, in fact, we have a triple root.

The following silly tool illustrates the relationship between (u^3), (v^3) and their roots in terms of p and q.  It might or might not help.

Manipulate[
p3q2=(p^3)/27+(q^2)/4;
uCubed = -q/2-(Sqrt[p3q2]);
vCubed = -q/2+(Sqrt[p3q2]);
uCubedPoint = {Re[uCubed],Im[uCubed]};
vCubedPoint = {Re[vCubed],Im[vCubed]};
If[p3q2>0 (* Just use the real cube root *),
{u1,v1} =CubeRoot[{uCubed,vCubed}],
(* Otherwise, use the principal root and check *)
{u1,v1}=(({uCubed,vCubed})^(1/3));
(* Choose the correct branch for v so that u*v is real *)
Do[If[Abs[Im[N[u1*v1*Exp[2Pi*k*I/3]]]]<(10.0^(-10)),b=k],{k,0,2}];
v1=v1*ComplexExpand[Exp[2Pi*b*I/3]]
];
{u2,v2}={u1*ComplexExpand[Exp[2Pi*I/3]],v1*ComplexExpand[Exp[4Pi*I/3]]};
{u3,v3}={u1*ComplexExpand[Exp[4Pi*I/3]],v1*ComplexExpand[Exp[2Pi*I/3]]};
rootPoints={Re[#],Im[#]}& /@ {u1+v1,u2+v2,u3+v3};
uPoints = {Re[#],Im[#]}& /@ {u1,u2,u3};
vPoints = {Re[#],Im[#]}& /@ {v1,v2,v3};
Labeled[Graphics[{
{PointSize[0.025],Point[rootPoints[[which]]], 
Opacity[0.3],Point[rootPoints]},
{Opacity[0.5],PointSize[0.025],
{Lighter[Blue],Point[uPoints[[which]]]},
{Lighter[Green],Point[vPoints[[which]]]}},
{Darker[Blue,0.4],PointSize[0.03],Point[uCubedPoint],
PointSize[Medium],Point[ uPoints]},
{Darker[Green,0.4],PointSize[0.03],Point[vCubedPoint],
PointSize[Medium],Point[vPoints]}
}, Axes->True, PlotRange -> 2],
Pane[Column[{
TraditionalForm[(x^3)+p*x+q],
Row[{HoldForm[(("p")^3)/27+(("q")^2)/4],"=",
Style[NumberForm[N[p3q2],{3,2}],If[p3q2<0,Red,Black]]}]
}],{120,50}], Top],
{{p,-3},-5,5},{{q,1},-5,5},{{which,1,"highlight root"},{1,2,3}}]

Outline of the whole procedure

The objective is to find the roots of f(x)=(x^3)+a (x^2)+b x+c via the following outline.

Translate to obtain g(y)=(x^3)+p x+q, where

p=-((a^2)/3)+b     and    q=((2 (a^3))/27)-((a b)/3)+c.

If y is a root of g, then x=y-a/3 will be a root of f.

Compute (u^3) and (v^3) via

(u^3)=-(q/2)-(Sqrt[((p^3)/27)+((q^2)/4)])  and  (v^3)=-(q/2)+(Sqrt[((p^3)/27)+((q^2)/4)]).

If (p^3)/27+(q^2)/4>0

Let u and v be the real cube roots of (u^3) and (v^3).

u+v will be a real root of g and the other roots will be complex conjugates.

If (p^3)/27+(q^2)/4<0

Let u be the principal cube root of (u^3) and let v be the conjugate of u.

All roots of g will be real, though expressed in terms of complex numbers.

If (p^3)/27+(q^2)/4=0

In this case, we have a repeated root

If p=q=0, then we have a triple root

Otherwise, u=v and (in the formulae below) we see that (Subscript[y, 2])=(Subscript[y, 3]) so that we have a double root.

In all cases, the roots of g can be listed in terms of u, v, and ω=(e^(2π i/3)).

(Subscript[y, 1])=u+v

(Subscript[y, 2])=ω u+(ω^2)v

(Subscript[y, 3])=(ω^2)u+ω v

The roots of f can then be listed in terms of the roots of g

(Subscript[x, 1])=(Subscript[y, 1])-a/3

(Subscript[x, 2])=(Subscript[y, 2])-a/3

(Subscript[x, 3])=(Subscript[y, 3])-a/3

Example

Suppose we wish to solve 5 (x^3)+30 (x^2)+45 x-5=0.  Expressing this in terms of a monic polynomial, we must find the roots of f(x)=(x^3)+6 (x^2)+9 x-1 with a=6, b=9 and c=-1.  We set

p=-((a^2)/3)+b=-((6^2)/3)+9=-3

and

q=((2 (a^3))/27)-((a b)/3)+c=((2 (3^3))/27)-((6 9)/3)-1=-3.

Thus, the associated depressed cubic is g(y)=(y^3)-3y-3 and, if (Subscript[y, 0]) is a root of g, then (Subscript[x, 0])=(Subscript[y, 0])-2 is a root of f.  Note that (p^3)/27+(q^2)/4=-27/27+9/4=5/4>0.  Since this is positive, we should find one (explicitly) real root and two complex conjugate roots.  We let

(u^3)=-(q/2)-(Sqrt[((p^3)/27)+((q^2)/4)])=(3/2)-((Sqrt[5])/2)

and

(v^3)=-(q/2)+(Sqrt[((p^3)/27)+((q^2)/4)])=(3/2)+((Sqrt[5])/2) .

Then, one root of g is

(Subscript[y, 1])=u+v=((u^3)^(1/3))+((v^3)^(1/3))=(((3/2)-((Sqrt[5])/2) )^(1/3))+(((3/2)+((Sqrt[5])/2) )^(1/3)),

where the cube root indicates the real branch of the cube root.  The other roots are then

(Subscript[y, 2])=ω u+(ω^2)v=(-(1/2)+((i (Sqrt[3]))/2)) (((3/2)-((Sqrt[5])/2) )^(1/3))+(-(1/2)-((i (Sqrt[3]))/2)) (((3/2)+((Sqrt[5])/2) )^(1/3))

and

(Subscript[y, 3])=(ω^2) u+ω v=(-(1/2)-((i (Sqrt[3]))/2)) (((3/2)-((Sqrt[5])/2) )^(1/3))+(-(1/2)+((i (Sqrt[3]))/2)) (((3/2)+((Sqrt[5])/2) )^(1/3)),

which are easily seen to be complex.  Finally, we subtract off a two to get the roots of f.

Example

Suppose we wish to solve (x^3)+3(x^2)-2x-1=0.  Expressing this in terms of a monic polynomial, we must find the roots of f(x)=(x^3)+3(x^2)-2x-1 with a=3, b=-2 and c=-1.  We set

p=-((a^2)/3)+b=-((3^2)/3)-2=-5

and

q=((2 (a^3))/27)-((a b)/3)+c=((2 (3^3))/27)-((3(-2))/3)-1=3

Thus, the associated depressed cubic is g(y)=(y^3)-5y+3 and, if (Subscript[y, 0]) is a root of g, then (Subscript[x, 0])=(Subscript[y, 0])-1 is a root of f.  Note that (p^3)/27+(q^2)/4=-257/108<0.  Thus, we expect to find three real roots, potentially expressed in terms of complex numbers.  We now let

(u^3)=-(q/2)-(Sqrt[((p^3)/27)+((q^2)/4)])=-(3/2)-(1/6) i (Sqrt[(257/3)])

and

(v^3)=-(q/2)+(Sqrt[((p^3)/27)+((q^2)/4)])=-(3/2)+(1/6) i (Sqrt[(257/3)])

Then, one root of g is

(Subscript[y, 1])=u+v=((u^3)^(1/3))+((v^3)^(1/3))=((-(3/2)-(1/6) i (Sqrt[(257/3)]))^(1/3))+((-(3/2)+(1/6) i (Sqrt[(257/3)]))^(1/3)),

where the cube root indicates the principal branch of the cube root.  The other roots are then

(Subscript[y, 2])=ω u+(ω^2)v=(-(1/2)+((i (Sqrt[3]))/2)) ((-(3/2)-(1/6) i (Sqrt[(257/3)]))^(1/3))+(-(1/2)-((i (Sqrt[3]))/2)) ((-(3/2)+(1/6) i (Sqrt[(257/3)]))^(1/3))

and

(Subscript[y, 3])=(ω^2) u+ω v=(-(1/2)-((i (Sqrt[3]))/2)) ((-(3/2)-(1/6) i (Sqrt[(257/3)]))^(1/3))+(-(1/2)+((i (Sqrt[3]))/2)) ((-(3/2)+(1/6) i (Sqrt[(257/3)]))^(1/3)),

which are easily seen to be complex.  Finally, we subtract off a one to get the roots of f.

cardanoSolve

Here’s a little function that implements Cardano’s solution to the cubic.  There are two versions depending on whether you want to solve the depressed cubic or the full blown monic.

(* Solve the depressed cubic x^3 + p*x + q = 0, given p and q *)
cardanoSolve[p_,q_] := Module[{p3q2,u,v,b (* for branch *), roots},
(* The basic formulae for u and v *)
p3q2=(p^3)/27+(q^2)/4;
If[p3q2>0 (* Just use the real cube root *),
{u,v} =CubeRoot[{-(q/2)-(Sqrt[((p^3)/27)+((q^2)/4)]),-(q/2)+(Sqrt[((p^3)/27)+((q^2)/4)])}],
(* Otherwise, use the principal root and check *)
{u,v}=(({-(q/2)-(Sqrt[((p^3)/27)+((q^2)/4)]),-(q/2)+(Sqrt[((p^3)/27)+((q^2)/4)])})^(1/3));
(* Choose the correct branch for v so that u*v is real *)
Do[If[Abs[Im[N[u*v*Exp[2Pi*k*I/3]]]]<(10.0^(-10)),b=k],{k,0,2}];
v=v*ComplexExpand[Exp[2Pi*b*I/3]]
];
(* List the solutions *)
roots = {u+v,
u*ComplexExpand[Exp[2Pi*I/3]]+v*ComplexExpand[Exp[4Pi*I/3]],
u*ComplexExpand[Exp[4Pi*I/3]]+v*ComplexExpand[Exp[2Pi*I/3]]};
Quiet[roots /. 
r:Power[_?(#<0&),_Rational?(Denominator[#]==3||Denominator[#]==6&)]:>
ComplexExpand[r], Less::nord]];

(* Solve the monic cubic, x^3 + a*x^2 + b*x + c = 0, given a, b, and c *)
cardanoSolve[a_,b_,c_] := Module[{p,q},
p=b-(a^2)/3;
q=c-a*b/3+2(a^3)/27;
cardanoSolve[p,q]-a/3];

Example

Suppose that we want to solve (x-1)(x-2)(x-4)=0.  We need to expand this to see the correct entries to cardanoSolve.

Expand[(x-1)(x-2)(x-4)]

cardanoSolve[-7,14,-8]

I guess these should be complicated ways to write 1, 2, and 4.

FullSimplify[%]

Of course, a major point is that there are cubics with all real solutions where the expression cannot be simplified into something involving no imaginary terms, if we’re restricted to root extraction.

Example

Here are the roots of f(x)=(x^3)-3x+1

cardanoSolve[-3,1]

FullSimplify[%]

Well, that’s interesting!

Solving cubics the Real way?

Here’s an alternative formula for the roots of the depressed cubic (x^3)+p x+q, at least when p<0.

realCubicRoots[p_,q_] := Table[
2(Sqrt[-p/3])Cos[ArcCos[((3q)/(2p))(Sqrt[-(3/p)])]/3-2*k*π/3],
{k,0,2}];

Here’s “proof” that it works.

Simplify[(x^3)+p*x+q /. x -> realCubicRoots[p,q],Assumptions -> p<0]

For example,

realCubicRoots[-3,1]

N[%]

NSolve[(x^3)-3x+1==0,x]

Recall that complex quantities enter the Cardano method precisely when 4(p^3)+27(q^2)<0, which implies that p<0.  Thus, this approach can yield roots involving only real quantities even when Cardano’s technique does not, though not in an algebraic form.

Exercises

Apply Cardano’s technique to the following polynomials.  Feel free to apply the cardanoSolve function to explore these but you should think about the process.  Here are some specific questions about that process: What is the associated depressed cubic? How are the solutions of this depressed cubic related to the original f?  What are the intermediate terms (u^3) and (v^3), as well as the appropriate cube roots u and v?

f(x)=(x-1)(x-2)(x-3) (Obviously, you already kow the roots; the point is to explore the process.)

f(x)=(x^3)+6(x^2)+7x-6

f(x)=(x^3)+9(x^2)+22x+8

Use the sign of (p^3)/27+(q^2)/4 to characterize the roots of the following cubics as three distinct real roots, three real roots with repetition, or one real with two complex.

f(x)=(x^3)-2x-1

f(x)=(x^3)-2x-2

f(x)=(x^3)-9(x^2)+23x-16

Let’s call a cubic of the form f(x)=a (x^3)+b (x^2)+c suppressed.

Show that a supressed cubic f can be transformed to a depressed cubic g via g(y)=(y^3)f(1/y).

How are the roots of f related to the roots of g?

Apply the suppression/depression technique to 1-13 (y^2)-12 (y^3)=0.

If x=p/q is a root of f(x)=(Subscript[a, n]) (x^n)+(Subscript[a, n-1])(x^(n-1))+⋯+(Subscript[a, 1])x+(Subscript[a, 0]), then (q x-p) is a factor of f.  It follows that q|(Subscript[a, n]) and p|(Subscript[a, 0]).  This is called the rational root theorem and makes it very easy to check for small, rational roots of a polynomial.  Once you have one root, you can factor the polynomial to reduce the overall problem to one of lower degree.  Apply this technique to find any rational roots of the following and factor the polynomial over the integers, if possible.

2 (x^3)+(x^2)-5 x+2=0

2 (x^3)-(x^2)+2 x+1=0

(x^3)+(x^2)+x+3=0

Let f(x)=(x^3)+p x+q and suppose that (p^3)/27+(q^2)/4<0.  As we know, f has three real roots but Cardano’s technique is guaranteed to generated complex numbers which might or might not simplify to explicitly real numbers.  It turns out that those numbers simplify to explicitly real numbers precisely when f is factorable, as is easily testable by the rational root theorem.

Apply the rational root theorem to (x^3)+(x^2)-4x+1=0, as it’s not factorable, apply cardanoSolve.  What do you determine about the nature of the roots?

cardanoSolve[1,-4,1]