Skip to content

Commit 6b7de80

Browse files
committed
Relax complex function signatures to make them ForwardDiff compatible
Ref: JuliaDiff/ForwardDiff.jl#455
1 parent 5757012 commit 6b7de80

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

base/complex.jl

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ function inv(w::ComplexF64)
464464
return ComplexF64(p*s,q*s) # undo scaling
465465
end
466466

467-
function ssqs(x::T, y::T) where T<:AbstractFloat
467+
function ssqs(x::T, y::T) where T
468468
k::Int = 0
469469
ρ = x*x + y*y
470470
if !isfinite(ρ) && (isinf(x) || isinf(y))
@@ -478,7 +478,8 @@ function ssqs(x::T, y::T) where T<:AbstractFloat
478478
ρ, k
479479
end
480480

481-
function sqrt(z::Complex{<:AbstractFloat})
481+
function sqrt(z::Complex)
482+
z = float(z)
482483
x, y = reim(z)
483484
if x==y==0
484485
return Complex(zero(x),y)
@@ -503,7 +504,6 @@ function sqrt(z::Complex{<:AbstractFloat})
503504
end
504505
Complex(ξ,η)
505506
end
506-
sqrt(z::Complex) = sqrt(float(z))
507507

508508
# function sqrt(z::Complex)
509509
# rz = float(real(z))
@@ -560,10 +560,12 @@ julia> rad2deg(angle(-1 - im))
560560
"""
561561
angle(z::Complex) = atan(imag(z), real(z))
562562

563-
function log(z::Complex{T}) where T<:AbstractFloat
564-
T1::T = 1.25
565-
T2::T = 3
566-
ln2::T = log(convert(T,2)) #0.6931471805599453
563+
function log(z::Complex)
564+
z = float(z)
565+
T = typeof(real(z))
566+
T1 = convert(T,5)/convert(T,4)
567+
T2 = convert(T,3)
568+
ln2 = log(convert(T,2)) #0.6931471805599453
567569
x, y = reim(z)
568570
ρ, k = ssqs(x,y)
569571
ax = abs(x)
@@ -580,7 +582,6 @@ function log(z::Complex{T}) where T<:AbstractFloat
580582
end
581583
Complex(ρρ, angle(z))
582584
end
583-
log(z::Complex) = log(float(z))
584585

585586
# function log(z::Complex)
586587
# ar = abs(real(z))
@@ -633,7 +634,7 @@ function exp(z::Complex)
633634
end
634635
end
635636

636-
function expm1(z::Complex{T}) where T<:Real
637+
function expm1(z::Complex{T}) where T
637638
Tf = float(T)
638639
zr,zi = reim(z)
639640
if isnan(zr)
@@ -681,24 +682,26 @@ function log1p(z::Complex{T}) where T
681682
end
682683
end
683684

684-
function exp2(z::Complex{T}) where T<:AbstractFloat
685+
function exp2(z::Complex{T}) where T
686+
z = float(z)
685687
er = exp2(real(z))
686688
theta = imag(z) * log(convert(T, 2))
687689
s, c = sincos(theta)
688690
Complex(er * c, er * s)
689691
end
690-
exp2(z::Complex) = exp2(float(z))
691692

692-
function exp10(z::Complex{T}) where T<:AbstractFloat
693+
function exp10(z::Complex{T}) where T
694+
z = float(z)
693695
er = exp10(real(z))
694696
theta = imag(z) * log(convert(T, 10))
695697
s, c = sincos(theta)
696698
Complex(er * c, er * s)
697699
end
698-
exp10(z::Complex) = exp10(float(z))
699700

700701
# _cpow helper function to avoid method ambiguity with ^(::Complex,::Real)
701-
function _cpow(z::Union{T,Complex{T}}, p::Union{T,Complex{T}}) where {T<:AbstractFloat}
702+
function _cpow(z::Union{T,Complex{T}}, p::Union{T,Complex{T}}) where T
703+
z = float(z)
704+
p = float(p)
702705
if isreal(p)
703706
pᵣ = real(p)
704707
if isinteger(pᵣ) && abs(pᵣ) < typemax(Int32)
@@ -777,7 +780,6 @@ function _cpow(z::Union{T,Complex{T}}, p::Union{T,Complex{T}}) where {T<:Abstrac
777780
return Complex(T(NaN),T(NaN)) # non-finite phase angle or NaN input
778781
end
779782
end
780-
_cpow(z, p) = _cpow(float(z), float(p))
781783
^(z::Complex{T}, p::Complex{T}) where T<:Real = _cpow(z, p)
782784
^(z::Complex{T}, p::T) where T<:Real = _cpow(z, p)
783785
^(z::T, p::Complex{T}) where T<:Real = _cpow(z, p)
@@ -859,7 +861,8 @@ function asin(z::Complex)
859861
Complex(ξ,η)
860862
end
861863

862-
function acos(z::Complex{<:AbstractFloat})
864+
function acos(z::Complex)
865+
z = float(z)
863866
zr, zi = reim(z)
864867
if isnan(zr)
865868
if isinf(zi) return Complex(zr, -zi)
@@ -880,7 +883,6 @@ function acos(z::Complex{<:AbstractFloat})
880883
if isinf(zr) && isinf(zi) ξ -= oftype(η,pi)/4 * sign(zr) end
881884
Complex(ξ,η)
882885
end
883-
acos(z::Complex) = acos(float(z))
884886

885887
function atan(z::Complex)
886888
w = atanh(Complex(-imag(z),real(z)))
@@ -898,7 +900,8 @@ function cosh(z::Complex)
898900
cos(Complex(zi,-zr))
899901
end
900902

901-
function tanh(z::Complex{T}) where T<:AbstractFloat
903+
function tanh(z::Complex{T}) where T
904+
z = float(z)
902905
Ω = prevfloat(typemax(T))
903906
ξ, η = reim(z)
904907
if isnan(ξ) && η==0 return Complex(ξ, η) end
@@ -917,7 +920,6 @@ function tanh(z::Complex{T}) where T<:AbstractFloat
917920
end
918921
end
919922
end
920-
tanh(z::Complex) = tanh(float(z))
921923

922924
function asinh(z::Complex)
923925
w = asin(Complex(-imag(z),real(z)))
@@ -943,7 +945,8 @@ function acosh(z::Complex)
943945
Complex(ξ, η)
944946
end
945947

946-
function atanh(z::Complex{T}) where T<:AbstractFloat
948+
function atanh(z::Complex{T}) where T
949+
z = float(z)
947950
Ω = prevfloat(typemax(T))
948951
θ = sqrt(Ω)/4
949952
ρ = 1/θ
@@ -986,7 +989,6 @@ function atanh(z::Complex{T}) where T<:AbstractFloat
986989
end
987990
β * Complex(ξ, η)
988991
end
989-
atanh(z::Complex) = atanh(float(z))
990992

991993
#Rounding complex numbers
992994
#Requires two different RoundingModes for the real and imaginary components

0 commit comments

Comments
 (0)