Skip to content
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

specialized complex^real methods #24497

Merged
merged 2 commits into from
Nov 9, 2017

Conversation

stevengj
Copy link
Member

@stevengj stevengj commented Nov 6, 2017

This adds specialized methods for complex^real, which would previously have first promoted to complex^complex. Fixes #14342.

The performance improvement seems to be very small in typical cases (e.g. 6-8% for Complex{Float64}^Float64), but it fixes a longstanding bug in the sign of the zero imaginary part for (2.0+0im)^-2.1 and similar.

@stevengj stevengj added the maths Mathematical functions label Nov 6, 2017
exp10(z::Complex) = exp10(float(z))

# _cpow helper function to avoid method ambiguity with ^(::Complex,::Real)
function _cpow(z::Complex{T}, p::Union{T,Complex{T}})::Complex{T} where T<:AbstractFloat
if p == 2 #square
Copy link
Member Author

@stevengj stevengj Nov 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this p==2 special-case code dates all the way back to @jiahao in #2891, who I guess added it as a performance optimization?

The need for performance optimization for z^2 is probably mainly for literal powers, though, so maybe we should nowadays move this to a literal_pow method? For Complex64 and Complex128, z^2 is already inlined as z*z, but maybe you can save an operation or two by specializing it further.

@ararslan ararslan added the complex Complex numbers label Nov 7, 2017
@JeffBezanson JeffBezanson merged commit caa7f04 into JuliaLang:master Nov 9, 2017
@stevengj stevengj deleted the complex-to-real branch November 9, 2017 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complex Complex numbers maths Mathematical functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wrong sign of zero for (x+0im)^(negative real)
3 participants