# How to extend cube root to complex numbers?

**URL:** <https://discourse.julialang.org/t/how-to-extend-cube-root-to-complex-numbers/42509>\
**Category:** New to Julia\
**Tags:** complex-numbers\
**Created:** [July 4, 2020, 4:17am UTC](https://discourse.julialang.org/t/how-to-extend-cube-root-to-complex-numbers/42509 "2020-07-04T04:17:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [July 4, 2020, 4:17am UTC](https://discourse.julialang.org/t/how-to-extend-cube-root-to-complex-numbers/42509/1 "2020-07-04T04:17:16Z")

</div>

I can find square root of complex numbers but not cube root

```julia
julia> sqrt(1 + 2im)
1.272019649514069 + 0.7861513777574233im

julia> cbrt(1 + 2im)
ERROR: MethodError: no method matching cbrt(::Complex{Int64})
Closest candidates are:
  cbrt(::BigFloat) at mpfr.jl:619
  cbrt(::Float16) at math.jl:1125
  cbrt(::Union{Float32, Float64}) at special/cbrt.jl:144
  ...
Stacktrace:
 [1] top-level scope at REPL[2]:1

```

So this is my attempt to extend cube root of complex numbers. Am I doing this right?

```julia
julia> import Base: cbrt

julia> cbrt(x::Complex{T}) where T = x^(one(T)/(one(T)+one(T)+one(T)))
cbrt (generic function with 6 methods)

julia> cbrt(1 + 2im)
1.2196165079717578 + 0.47171126778938893im

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [July 4, 2020, 5:34am UTC](https://discourse.julialang.org/t/how-to-extend-cube-root-to-complex-numbers/42509/2 "2020-07-04T05:34:40Z")

</div>

What about the two other complex roots?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 4, 2020, 11:54am UTC](https://discourse.julialang.org/t/how-to-extend-cube-root-to-complex-numbers/42509/3 "2020-07-04T11:54:11Z")

</div>

I guess `cbrt` could be extended to complex numbers. Maybe open an issue? In the meantime,

```julia
julia> (1+2im)^(1/3)
1.2196165079717578 + 0.47171126778938893im

```

> [@longemen3000](#):
>
> What about the two other complex roots?

Like `sqrt`, it would have to pick one. AFAIK it is commonly called the principal root.

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [July 4, 2020, 12:57pm UTC](https://discourse.julialang.org/t/how-to-extend-cube-root-to-complex-numbers/42509/4 "2020-07-04T12:57:47Z")

</div>

Things can get weird here. If you use the principal root, then it is not always true that

(ab)^(1/3) = a^(1/3) b^(1/3)

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [July 4, 2020, 3:06pm UTC](https://discourse.julialang.org/t/how-to-extend-cube-root-to-complex-numbers/42509/5 "2020-07-04T15:06:23Z")

</div>

It’s exactly the same situation as with `sqrt`:

```julia
a = complex(-1)
sqrt(a*a) == 1
sqrt(a)*sqrt(a) == -1

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 4, 2020, 4:13pm UTC](https://discourse.julialang.org/t/how-to-extend-cube-root-to-complex-numbers/42509/6 "2020-07-04T16:13:25Z")

</div>

I opened an issue: [https://github.com/JuliaLang/julia/issues/36534](https://github.com/JuliaLang/julia/issues/36534)

However, the choice of branch cut isn’t immediately clear to me, since the obvious choice is inconsistent with how `cbrt` is defined for real values:

```julia
# naive definition:
julia> Base.cbrt(z::Complex) = cbrt(abs(z)) * cis(angle(z)/3)

julia> cbrt(-3)
-1.4422495703074083

julia> cbrt(-3+0im)
0.7211247851537043 + 1.2490247664834064im

julia> (-3+0im)^(1//3)
0.7211247851537042 + 1.2490247664834064im

```

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [July 4, 2020, 4:28pm UTC](https://discourse.julialang.org/t/how-to-extend-cube-root-to-complex-numbers/42509/7 "2020-07-04T16:28:52Z")

</div>

I don’t think there is an obvious choice. If you want a positive real root for a positive real input, you are still in a hole. If you integrate around a contour about the origin, you’ll cross a branch cut and get different values as the argument increases. So if z=r exp(im\*theta) and cbrt(z) = r exp(im \*theta/3), one pass around the contour does not take you back to where you started. All you can do is keep the books correctly as you traverse a contour.

Same story z^(1/n) for n =2, 3, …

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [July 5, 2020, 11:55am UTC](https://discourse.julialang.org/t/how-to-extend-cube-root-to-complex-numbers/42509/8 "2020-07-05T11:55:14Z")

</div>

In Mathematica, cbrt is only defined to return the real value of cuberoot.

I don’t think it is a good idea to define cbrt for complex value because it would cause discontinuity (depending on whether the arguement is Real or Complex). But certainly the documentation for crbt should be updated to mention that

cbrt(-27) == -3 while (-27+0im)^(1/3) == 1.5 + 2.598076211353316im

so cbrt will only return a real result (and never a complex result)

 ![Screen Shot 2020-07-05 at 10.02.43 PM](https://global.discourse-cdn.com/julialang/original/3X/a/3/a3035a74f74a52580028a7be87c1b68128df595b.png)
