# Finding real roots of negative numbers (for example, $\sqrt{-8}$)

Say I want to quickly calculate $\sqrt{-8}$, to which the most obvious solution is $-2$.

When I input $\sqrt{-8}$ or Power[-8, 3^-1], Mathematica gives the result $2 (-1)^{1/3}$. Not what I want.

When I input Power[-8, 3^-1] // N or Power[-8., 3^-1], Mathematica gives the result $1. + 1.73205i$. While technically correct, this is messy and not always useful.

How can I get the real cube root of a negative number? Or more generally, how can I get a list of all valid roots?

• For your first question, some day one will be able to do as below. (But not today, unless you are here.) In := NumericalMathCubeRoot[-8] Out = -2 – Daniel Lichtblau May 22 '12 at 17:32
• @Daniel - Looks great, looking forward to it. Er, unless the previous 98 steps are also required :-/ – stevenvh Sep 28 '12 at 18:04

Mathematica 9 introduces two new functions, CubeRoot and Surd, that give real-valued roots:

In:= CubeRoot[-8]
Out= -2

In:= Surd[-32, 5]
Out= -2


You can use these to plot real roots:

Plot[CubeRoot[x], {x, -3, 3}] Note that these functions are undefined for complex numbers:

In:= CubeRoot[1 + I]

CubeRoot::preal: The parameter 1+I should be real valued. >>

Out= Surd[1 + I, 3]


and the typeset form has a small "tail" at the end of the overbar to visually distinguish them from the usual roots: • The name Surd stands for? – enzotib Jun 22 at 12:17
• @enzotib A root containing an unresolved radical sign. From Wikipedia: "The term surd traces back to al-Khwārizmī (c. 825), who referred to rational and irrational numbers as audible and inaudible, respectively. This later led to the Arabic word "أصم‎" (asamm, meaning "deaf" or "dumb") for irrational number being translated into Latin as "surdus" (meaning "deaf" or "mute"). Gerard of Cremona (c. 1150), Fibonacci (1202), and then Robert Recorde (1551) all used the term to refer to unresolved irrational roots." – flinty Aug 21 at 16:41

In general, a typical root of a negative number is complex, so you need to get rid of most roots. A nice approach would be Root, e.g.

Root[ x^3 + 8, #] & /@ Range

{-2, 1 - I Sqrt, 1 + I Sqrt}


To get only real roots you can do :

Select[Root[ x^3 + 8, #] & /@ Range, Re[#] == # &]

{-2}


This is a handy approach when you have roots of lower orders.

However you'd rather use Reduce or Solve for higher order roots, in this case it works like this :

Reduce[ x^3 + 8 == 0, x]

x == -2 || x == 1 - I Sqrt || x == 1 + I Sqrt

Solve[ x^3 + 8 == 0, x]

{{x -> -2}, {x -> 2 (-1)^(1/3)}, {x -> -2 (-1)^(2/3)}}


To get only real roots one can use for example : Reduce[x^3 + 8 == 0, x, Reals] or Solve[x^3 + 8 == 0, x, Reals]. They do almost the same, but their outputs are a bit different, respectively : in the boolean form and in the form of rules.

As a more appropriate example of what you want to do I could choose this one : (-3)^(1/7). Mathematica treats variables (in general) as complex. So one gets seven roots and there is the only one real.

Solve[ x^7 + 3 == 0, x, Reals]

{{x -> -3^(1/7)}}


To get the full output one can do this :

points = {Re @ #, Im @ #} & /@ Last @@@ Solve[x^7 + 3 == 0, x] Absolute values of the roots are the same, so they are found on the circle of a given radius (== 3^(1/7)) :

{ Equal @@ #, radius = #[] } & @ Simplify @ (Norm /@ points)

 {True, 3^(1/7)}


To visualize the structure of the output one makes use of ContourPlot of real and imaginary parts of the function (x + I y)^7 + 3 (we write the function explicitly in the complex form since we make plots in real domains of x and y ) :

GraphicsRow[{
ContourPlot[ Re[(x + I y)^7 + 3], {x, -2, 2}, {y, -2, 2}, PlotPoints -> 25, MaxRecursion -> 5,
Epilog -> {Darker@Green, Thick, Line[{{0, 0}, #}] & /@ points,
Gray, Dashed, Circle[{0, 0}, radius], PointSize[0.02], Blue, Point[points]}],

ContourPlot[ Im[(x + I y)^7 + 3], {x, -2, 2}, {y, -2, 2}, PlotPoints -> 25, MaxRecursion -> 5,
Epilog -> {Darker@Green, Thick, Line[{{0, 0}, #}] & /@ points,
Gray, Dashed, Circle[{0, 0}, radius], PointSize[0.02], Blue, Point[points]}]}] Clarification

• The blue points --- roots
• lengths of the green lines --- absolute values of the roots
• the dashed circle --- a set of all complex numbers z such that Abs[z] == radius

The only one real root lies on the line y == 0.

You may use a function, which gives you the "real Power":

rprule=(b_?Negative)^Rational[m_,n_?OddQ]:>(-(-b)^(1/n))^m;
Attributes[realPower]={Listable, NumericFunction,OneIdentity}  (* same as Power *)
realPower[b_?Negative, Rational[m_, n_?OddQ]] := (-(-b)^(1/n))^m;
realPower[x_,y_]:=Power[x,y];
realPower[x_]:=x//.rprule;


Then you'll get:

realPower[{8^(1/3),(-8)^(1/3)]


{2,-2}

The "two arguments" form is needed eg. in plots:

Plot[realPower[x, 1/3], {x, -2, 2}]

• Was very useful to me. +1 – Patrick Da Silva Aug 2 '12 at 5:53

In general, to get a list of all the cube roots of -8 (or the $m$ roots of any number $n$), you can use either the the Roots or Solve or Reduce functions.

Roots[x^3 == -8, x]
(* Out=  x == 2 || x == 2 (-1)^(2/3) || x == -2 (-1)^(1/3) *)


Reduce and Solve are perhaps more flexible because you can specify the domain that you want or leave it out for all the solutions.

Reduce[x^3 == 8, x]
(* Out= x == 2 || x == -1 - I Sqrt || x == -1 + I Sqrt *)

Reduce[x^3 == 8, x, Reals]
(* Out= x == 2

Solve[x^3 == 8, x]
(* Out= {{x -> 2}, {x -> -2 (-1)^(1/3)}, {x -> 2 (-1)^(2/3)}} *)

Solve[x^3 == 8, x, Reals]
(* Out= {{x -> 2}} *)


Note that the structure of the output returned for Reduce and Solve are different.

I quite prefer realPower[x_, r_] := Sign[x]*Abs[x]^r myself. (A similar thing is done in the old package MiscellaneousRealOnly.) realPower[-8, 1/3] yields -2, as expected. To get all the real roots, Artes's solution is best.

Let us introduce a rule:

cubicRootRule = a_^(1/3) /; Negative[a] -> -(Abs[a])^(1/3);


To check it let us form a list with cubic roots of various numbers, positive and negative:

 lst = Table[(RandomInteger[{-16, 16}])^(1/3), {20}]

(* {(-11)^(1/3), 10^(1/3), (-10)^(1/3), 6^(1/3), (-5)^(1/3),
2 (-1)^(1/3), (-10)^(1/3), (-3)^(1/3), 2^(1/3), (-15)^(1/3),
3^( 1/3), (-7)^(1/3), (-7)^(1/3), 15^(1/3), (-5)^(1/3),
2 (-1)^(1/3), 13^(1/3), (-13)^(1/3), 2 (-2)^(1/3), 2^(2/3) 3^(1/3)} *)


and apply the rule to the list:

lst /. cubicRootRule

(* {-11^(1/3), 10^(1/3), -10^(1/3), 6^(
1/3), -5^(1/3), -2, -10^(1/3), -3^(1/3), 2^(1/3), -15^(1/3), 3^(
1/3), -7^(1/3), -7^(1/3), 15^(1/3), -5^(1/3), -2, 13^(
1/3), -13^(1/3), -2 2^(1/3), 2^(2/3) 3^(1/3)} *)
`

That's it?