Skip to content

Resolves #47565 and #36534. nthroot() for complex variables implemented. #47860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
14 changes: 13 additions & 1 deletion base/complex.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

"""
Complex{T<:Real} <: Number

Expand Down Expand Up @@ -561,6 +560,19 @@ end
# return Complex(abs(iz)/r/2, copysign(r,iz))
# end

struct RootsVector{T<:Union{Real,Complex}} <: AbstractVector{T}
z::T
n::Int
option::Char
end
RootsVector(z::Complex, n::Int) = RootsVector(z, n, 'm')
RootsVector(z::Real, n::Int) = RootsVector(z, n, 's')

Base.size(S::RootsVector) = (S.option == 's') ? (1,) : ((S.option == 'm') && ((S.z != zero(S.z)) ? (typeof(S.z)<:Complex) ? (S.n,) : ((iseven(S.n) && (S.z > zero(S.z)) ? (2,) : (1,))) : (1,)))
Base.IndexStyle(S::RootsVector) = IndexLinear()
Base.getindex(S::RootsVector, i::Int) = (typeof(S.z)<:Complex) ? abs(S.z)^(1/S.n)*cis((angle(S.z)+2*pi*(i-1))/S.n) : (isodd(S.n) ? copysign(abs(S.z)^(1/S.n), S.z) : ((S.z > zero(S.z)) ? copysign(abs(S.z)^(1/S.n), (-1)^(i-1)) : NaN))
# Base.getindex(S::RootsVector, i::Int) = (isa(typeof(S.z), Complex)) ? abs(S.z)^(1/S.n)*cis((angle(S.z)+2*pi*(i-1))/S.n) : (isodd(S.n) ? copysign(abs(S.z)^(1/S.n), S.z) : ((S.z > zero(S.z)) ? copysign(abs(S.z)^(1/S.n), (-1)^(i-1)) : throw(DomainError(S.z, LazyString(:RootsVector," will only return a complex result if called with a complex argument. Try ", :RootsVector,"(Complex(x), n).")))))

"""
cis(x)

Expand Down
2 changes: 2 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export
Regex,
RegexMatch,
Returns,
RootsVector,
RoundFromZero,
RoundDown,
RoundingMode,
Expand Down Expand Up @@ -349,6 +350,7 @@ export
sinh,
sinpi,
sqrt,
nthroot,
tan,
tand,
tanh,
Expand Down