@@ -631,7 +631,24 @@ function log1p(z::Complex{T}) where T
631
631
end
632
632
end
633
633
634
- function ^ (z:: Complex{T} , p:: Complex{T} ):: Complex{T} where T<: AbstractFloat
634
+ function exp2 (z:: Complex{T} ) where T<: AbstractFloat
635
+ er = exp2 (real (z))
636
+ theta = imag (z) * log (convert (T, 2 ))
637
+ s, c = sincos (theta)
638
+ Complex (er * c, er * s)
639
+ end
640
+ exp2 (z:: Complex ) = exp2 (float (z))
641
+
642
+ function exp10 (z:: Complex{T} ) where T<: AbstractFloat
643
+ er = exp10 (real (z))
644
+ theta = imag (z) * log (convert (T, 10 ))
645
+ s, c = sincos (theta)
646
+ Complex (er * c, er * s)
647
+ end
648
+ exp10 (z:: Complex ) = exp10 (float (z))
649
+
650
+ # _cpow helper function to avoid method ambiguity with ^(::Complex,::Real)
651
+ function _cpow (z:: Complex{T} , p:: Union{T,Complex{T}} ):: Complex{T} where T<: AbstractFloat
635
652
if p == 2 # square
636
653
zr, zi = reim (z)
637
654
x = (zr- zi)* (zr+ zi)
@@ -665,24 +682,7 @@ function ^(z::Complex{T}, p::Complex{T})::Complex{T} where T<:AbstractFloat
665
682
Complex (one (T), zer)
666
683
end
667
684
end
668
-
669
- function exp2 (z:: Complex{T} ) where T<: AbstractFloat
670
- er = exp2 (real (z))
671
- theta = imag (z) * log (convert (T, 2 ))
672
- s, c = sincos (theta)
673
- Complex (er * c, er * s)
674
- end
675
- exp2 (z:: Complex ) = exp2 (float (z))
676
-
677
- function exp10 (z:: Complex{T} ) where T<: AbstractFloat
678
- er = exp10 (real (z))
679
- theta = imag (z) * log (convert (T, 10 ))
680
- s, c = sincos (theta)
681
- Complex (er * c, er * s)
682
- end
683
- exp10 (z:: Complex ) = exp10 (float (z))
684
-
685
- function ^ (z:: T , p:: T ) where T<: Complex
685
+ function _cpow (z:: Complex{T} , p:: Union{T,Complex{T}} ) where T<: Real
686
686
if isinteger (p)
687
687
rp = real (p)
688
688
if rp < 0
@@ -738,6 +738,8 @@ function ^(z::T, p::T) where T<:Complex
738
738
739
739
Complex (re, im)
740
740
end
741
+ ^ (z:: Complex{T} , p:: Complex{T} ) where T<: Real = _cpow (z, p)
742
+ ^ (z:: Complex{T} , p:: T ) where T<: Real = _cpow (z, p)
741
743
742
744
^ (z:: Complex , n:: Bool ) = n ? z : one (z)
743
745
^ (z:: Complex , n:: Integer ) = z^ Complex (n)
749
751
n>= 0 ? power_by_squaring (z,n) : power_by_squaring (inv (z),- n)
750
752
^ (z:: Complex{<:Integer} , n:: Integer ) = power_by_squaring (z,n) # DomainError for n<0
751
753
754
+ function ^ (z:: Complex{T} , p:: S ) where {T<: Real ,S<: Real }
755
+ P = promote_type (T,S)
756
+ return Complex {P} (z) ^ P (p)
757
+ end
758
+
752
759
function sin (z:: Complex{T} ) where T
753
760
F = float (T)
754
761
zr, zi = reim (z)
0 commit comments