Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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