53
$\begingroup$

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

When I input $\sqrt[3]{-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?

$\endgroup$
3
  • 1
    $\begingroup$ For your first question, some day one will be able to do as below. (But not today, unless you are here.) In[99] := NumericalMath`CubeRoot[-8] Out[99] = -2 $\endgroup$ May 22, 2012 at 17:32
  • 9
    $\begingroup$ @Daniel - Looks great, looking forward to it. Er, unless the previous 98 steps are also required :-/ $\endgroup$
    – stevenvh
    Sep 28, 2012 at 18:04
  • $\begingroup$ Table[N[((x)^(1/3))], {x,-50,50,10}] vs Table[N[CubeRoot[x]], {x,-50,50,10}]. This is kind of crazy $\endgroup$ May 27, 2021 at 15:37

6 Answers 6

35
$\begingroup$

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

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

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

You can use these to plot real roots:

Plot[CubeRoot[x], {x, -3, 3}]

enter image description here

Note that these functions are undefined for complex numbers:

In[5]:= CubeRoot[1 + I]

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

Out[5]= 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:

enter image description here

$\endgroup$
3
  • $\begingroup$ The name Surd stands for? $\endgroup$
    – enzotib
    Jun 22, 2020 at 12:17
  • 4
    $\begingroup$ @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." $\endgroup$
    – flinty
    Aug 21, 2020 at 16:41
  • $\begingroup$ @flinty So the Wolfram implementation of Surd is at odds with the traditional and most often used definition, e.g., an unresolved irrational root? $\endgroup$
    – gwr
    Apr 13, 2023 at 9:14
57
$\begingroup$

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[3]
{-2, 1 - I Sqrt[3], 1 + I Sqrt[3]}    

To get only real roots you can do :

Select[Root[ x^3 + 8, #] & /@ Range[3], 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[3] || x == 1 + I Sqrt[3]
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]

enter image description here

Absolute values of the roots are the same, so they are found on the circle of a given radius (== 3^(1/7)) :

{ Equal @@ #, radius = #[[1]] } & @ 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]}]}]

enter image description here

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.

$\endgroup$
1
  • $\begingroup$ I would recommend using Root[#^3 + 8&, #]& /@ Range[3] instead of Root[ x^3 + 8, #]& /@ Range[3]. Using a symbol like x is going to cause errors when it's already defined. $\endgroup$ Jan 29, 2021 at 11:36
26
$\begingroup$

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}] 
$\endgroup$
1
  • $\begingroup$ Was very useful to me. +1 $\endgroup$ Aug 2, 2012 at 5:53
16
$\begingroup$

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[1]=  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[2]= x == 2 || x == -1 - I Sqrt[3] || x == -1 + I Sqrt[3] *)

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

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

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

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

$\endgroup$
0
8
$\begingroup$

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

$\endgroup$
4
$\begingroup$

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?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.