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

Adds an expm1 function for complex values. #6539

Merged
merged 2 commits into from
May 20, 2014

Conversation

simonbyrne
Copy link
Contributor

Partially addresses #3141. It uses the property that

cos(x) - 1 = -2*sin(x/2)^2

There is some cancellation error when real(z) ~ imag(z)^2/2 for small z, but otherwise is fairly stable.

I also removed some unnecessary branching conditions from the complex exp.


function expm1(z::Complex)
zr,zi = reim(z)
if isfinite(zr) && !isfinite(zi) return Complex(oftype(zr, NaN), oftype(zi, NaN)) end
Copy link
Member

Choose a reason for hiding this comment

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

To reduce the number of comparisons in the common case of finite values, it would be better to check for infinities only once in this case. i.e.

if isfinite(zr)
    if isfinite(zi)
          ....
    else
          ....
    end
else # infinite or NaN zr
   ....
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's true, and it would make it much easier to understand.

@simonbyrne
Copy link
Contributor Author

I've refactored both exp and expm1, and also managed to get rid of another unnecessary branch.

@simonbyrne
Copy link
Contributor Author

I'm not sure why travis broke, but it appears to be unrelated to the PR.

…that

cos(x) - 1 = -2*sin(x/2)^2

There is some cancellation error when real(z) ~ imag(z)^2/2 for small z, but otherwise is fairly stable.
@simonbyrne
Copy link
Contributor Author

I've squashed the commits: is there anything holding this back?

wi = er*(isfinite(zi) ? sin(zi) : zi)
Complex(wr, wi)
if isnan(zr)
Complex(zr, zi==zero(zi) ? zi : zr)
Copy link
Member

Choose a reason for hiding this comment

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

Not important but just FYI, zero() is not necessary in this context (since it's an argument and not a return value).

JeffBezanson added a commit that referenced this pull request May 20, 2014
Adds an expm1 function for complex values.
@JeffBezanson JeffBezanson merged commit 4615a95 into JuliaLang:master May 20, 2014
This was referenced May 21, 2014
@simonbyrne simonbyrne deleted the expm1 branch March 10, 2015 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants