Skip to content

Commit

Permalink
Use sincos in cis and other complex math functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyichao committed May 21, 2017
1 parent 24086de commit 4ed22b1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
29 changes: 19 additions & 10 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,10 @@ sqrt(z::Complex) = sqrt(float(z))
# end

# compute exp(im*theta)
cis(theta::Real) = Complex(cos(theta),sin(theta))
function cis(theta::Real)
s, c = sincos(theta)
Complex(c, s)
end

"""
cis(z)
Expand All @@ -433,7 +436,8 @@ Return ``\\exp(iz)``.
"""
function cis(z::Complex)
v = exp(-imag(z))
Complex(v*cos(real(z)), v*sin(real(z)))
s, c = sincos(real(z))
Complex(v * c, v * s)
end

"""
Expand Down Expand Up @@ -510,7 +514,8 @@ function exp(z::Complex)
if iszero(zi)
Complex(er, zi)
else
Complex(er*cos(zi), er*sin(zi))
s, c = sincos(zi)
Complex(er * c, er * s)
end
end
end
Expand Down Expand Up @@ -538,7 +543,8 @@ function expm1(z::Complex{T}) where T<:Real
wr = erm1 - 2 * er * (sin(convert(Tf, 0.5) * zi))^2
return Complex(wr, er * sin(zi))
else
return Complex(er * cos(zi), er * sin(zi))
s, c = sincos(zi)
return Complex(er * c, er * s)
end
end
end
Expand Down Expand Up @@ -600,13 +606,15 @@ end
function exp2(z::Complex{T}) where T
er = exp2(real(z))
theta = imag(z) * log(convert(T, 2))
Complex(er*cos(theta), er*sin(theta))
s, c = sincos(theta)
Complex(er * c, er * s)
end

function exp10(z::Complex{T}) where T
er = exp10(real(z))
theta = imag(z) * log(convert(T, 10))
Complex(er*cos(theta), er*sin(theta))
s, c = sincos(theta)
Complex(er * c, er * s)
end

function ^(z::T, p::T) where T<:Complex
Expand All @@ -628,8 +636,7 @@ function ^(z::T, p::T) where T<:Complex
rp = rp*exp(-pim*theta)
ntheta = ntheta + pim*log(r)
end
cosntheta = cos(ntheta)
sinntheta = sin(ntheta)
sinntheta, cosntheta = sincos(ntheta)
re, im = rp*cosntheta, rp*sinntheta
if isinf(rp)
if isnan(re)
Expand Down Expand Up @@ -689,7 +696,8 @@ function sin(z::Complex{T}) where T
Complex(F(NaN), F(NaN))
end
else
Complex(sin(zr)*cosh(zi), cos(zr)*sinh(zi))
s, c = sincos(zr)
Complex(s * cosh(zi), c * sinh(zi))
end
end

Expand All @@ -708,7 +716,8 @@ function cos(z::Complex{T}) where T
Complex(F(NaN), F(NaN))
end
else
Complex(cos(zr)*cosh(zi), -sin(zr)*sinh(zi))
s, c = sincos(zr)
Complex(c * cosh(zi), -s * sinh(zi))
end
end

Expand Down
5 changes: 4 additions & 1 deletion base/fastmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,10 @@ sincos_fast(v) = (sin_fast(v), cos_fast(v))

# complex numbers

cis_fast(x::T) where {T<:FloatTypes} = Complex{T}(cos(x), sin(x))
function cis_fast(x::T) where {T<:FloatTypes}
s, c = sincos_fast(x)
Complex{T}(c, s)
end

# See <http://en.cppreference.com/w/cpp/numeric/complex>
pow_fast(x::T, y::T) where {T<:ComplexTypes} = exp(y*log(x))
Expand Down

0 comments on commit 4ed22b1

Please sign in to comment.