# Nth root of a complex number should return N results

**URL:** <https://discourse.julialang.org/t/nth-root-of-a-complex-number-should-return-n-results/3961>\
**Category:** General Usage\
**Tags:** complex-numbers\
**Created:** [May 28, 2017, 12:14pm UTC](https://discourse.julialang.org/t/nth-root-of-a-complex-number-should-return-n-results/3961 "2017-05-28T12:14:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kaiyin\_Zhong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kaiyin_zhong/32/1956_2.png) [@Kaiyin\_Zhong](https://discourse.julialang.org/u/Kaiyin_Zhong)\
**Post date:** [May 28, 2017, 12:14pm UTC](https://discourse.julialang.org/t/nth-root-of-a-complex-number-should-return-n-results/3961/1 "2017-05-28T12:14:02Z")

</div>

As shown below:

```julia
x = 27 * e^π
e^2im
e^(2+2pi)im
n = 3
Θ = pi/3
Q = 27
x = Q⋅e^(Θ⋅im)
ϕ₁ = Θ/n + 2π⋅1/n
ϕ₂ = Θ/n + 2π⋅2/n
ϕ₃ = Θ/n + 2π⋅3/n
R = Q^(1/n)
y₁ = R⋅e^(ϕ₁⋅im)
y₂ = R⋅e^(ϕ₂⋅im)
y₃ = R⋅e^(ϕ₃⋅im)
println(x)
println(y₁^3)
println(y₂^3)
println(y₃^3)
println(x^(1/3))
println(y₁)
println(y₂)
println(y₃)

```

I was expecting `x^(1/3)` to give me 3 roots, but it only returned one of them. Why? How can I get the others?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 28, 2017, 12:26pm UTC](https://discourse.julialang.org/t/nth-root-of-a-complex-number-should-return-n-results/3961/2 "2017-05-28T12:26:25Z")

</div>

`x ^ n` is the exponentiation operation, not the root finder. If you want to find all the roots of a complex number, you can for example search for the roots of the corresponding polynomial using [`PolynomialRoots.jl`](https://github.com/giordano/PolynomialRoots.jl) (but there are also other packages for looking for roots of polynomials, like [`Polynomials.jl`](https://github.com/JuliaMath/Polynomials.jl)):

```julia
julia> using PolynomialRoots

julia> roots([8, 0, 0, -1])
3-element Array{Complex{Float64},1}:
         -1.0-1.73205im
 2.0-9.61481e-17im     
         -1.0+1.73205im

```

The argument of `roots` is the vector of coefficients of the polynomial, from the lowest to the highest one. This gives you the 3rd roots of 8.

In your case:

```julia
julia> roots([x, 0, 0, -1])
3-element Array{Complex{Float64},1}:
   2.81908+1.02606im
 -0.520945-2.95442im
  -2.29813+1.92836im

```

Note also that for the exponentiation operation `x ^ (1/3)` with `x` real you can use instead `cbrt(x)`, which is often faster and more accurate.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 28, 2017, 1:26pm UTC](https://discourse.julialang.org/t/nth-root-of-a-complex-number-should-return-n-results/3961/3 "2017-05-28T13:26:38Z")

</div>

PolynomialRoots.jl is very nice!

---

<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:** [May 29, 2017, 9:41am UTC](https://discourse.julialang.org/t/nth-root-of-a-complex-number-should-return-n-results/3961/4 "2017-05-29T09:41:24Z")

</div>

`PolynomialRoots.jl` is indeed very nice, but possibly overkill for this problem, as something like

```julia
function allroots{T <: Real}(x::Complex{T}, n::Int)
    z = x^(1/n)
    [z*cis((i-1)*2*π/n) for i in 1:n]
end

```

should get all the roots.
