Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
1  
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 –  Daniel Lichtblau May 22 '12 at 17:32
3  
@Daniel - Looks great, looking forward to it. Er, unless the previous 98 steps are also required :-/ –  stevenvh Sep 28 '12 at 18:04
add comment

6 Answers

up vote 10 down vote accepted

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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}] 
share|improve this answer
    
Was very useful to me. +1 –  Patrick Da Silva Aug 2 '12 at 5:53
add comment

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.

share|improve this answer
add comment

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?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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