# Nth root of a complex number should return N results

As shown below:

``````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?

`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` (but there are also other packages for looking for roots of polynomials, like `Polynomials.jl`):

``````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.

``````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.

3 Likes

PolynomialRoots.jl is very nice!

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

``````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.

5 Likes