Atan(y,x) for complex numbers

There doesn’t seem to be an implementation for atan when x and y are complex numbers. The currently implement atan(y/x) is not a replacement, as it loses the information on which quadrants x and y are in.

This is important for spherical harmonics and converting to spherical coordinates in the complex domain.

One potential solution is the default Mathematica uses, which is to define:

atan(x::Complex,y::Complex) = - im * log( (x+y*im)/sqrt(x^2+y^2) )

You can test this does get the right quadrants with:

Using LinearAlgebra
map(1:10000) do i
x = rand(-1.001:0.01:1.0) + rand(-1.001:0.01:1.0)*im
y = rand(-1.001:0.01:1.0) + rand(-1.001:0.01:1.0)im
r = sqrt(x^2 + y^2) # note this is a complex number
θ = atan(y,x)
norm( [x,y] - r .
[cos(θ),sin(θ)] )

For several packages I now add this definition to use (what is in my field) standard coordinate transforms.


It seems like this could easily be added just below this line:

Would probably make for quite a straightforward PR, if you’d like.


Seems like ruby’s cmath has this as well:

1 Like

Yes , if you can, please do create a PR. Am now trying to build Julia for the first time etc… before making my first PR.


You can make a one line PR easily from Github without evening building Julia locally: just click “edit”, make the change, and “propose file change”. The CI will test you’re change, though you’ll probably need to also add tests.