# Atan(y,x) for complex numbers

**URL:** <https://discourse.julialang.org/t/atan-y-x-for-complex-numbers/33692>\
**Category:** Internals & Design\
**Tags:** math, complex-numbers\
**Created:** [January 23, 2020, 10:52am UTC](https://discourse.julialang.org/t/atan-y-x-for-complex-numbers/33692 "2020-01-23T10:52:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arturgower](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arturgower/32/3967_2.png) [@arturgower](https://discourse.julialang.org/u/arturgower)\
**Post date:** [January 23, 2020, 10:52am UTC](https://discourse.julialang.org/t/atan-y-x-for-complex-numbers/33692/1 "2020-01-23T10:52:14Z")

</div>

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](https://reference.wolfram.com/language/ref/ArcTan.html?q=ArcTan), 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  
> maximum(  
> 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(θ)] )  
> end  
> )

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

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [January 23, 2020, 11:14am UTC](https://discourse.julialang.org/t/atan-y-x-for-complex-numbers/33692/2 "2020-01-23T11:14:40Z")

</div>

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

[https://github.com/JuliaLang/julia/blob/c2abaeedf8316a392035ff63fa6460595a9a5dd3/base/math.jl#L698](https://github.com/JuliaLang/julia/blob/c2abaeedf8316a392035ff63fa6460595a9a5dd3/base/math.jl#L698)

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

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [January 23, 2020, 11:24am UTC](https://discourse.julialang.org/t/atan-y-x-for-complex-numbers/33692/3 "2020-01-23T11:24:34Z")

</div>

Seems like ruby’s cmath has this as well:

> <https://github.com/ruby/cmath/blob/8a06c854265238ce4d6d8154a5e7d71e9472a8fd/lib/cmath.rb#L319>

---

<div class="post-metadata">

**Author:** ![arturgower](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arturgower/32/3967_2.png) [@arturgower](https://discourse.julialang.org/u/arturgower)\
**Post date:** [January 23, 2020, 11:58am UTC](https://discourse.julialang.org/t/atan-y-x-for-complex-numbers/33692/4 "2020-01-23T11:58:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [January 23, 2020, 5:15pm UTC](https://discourse.julialang.org/t/atan-y-x-for-complex-numbers/33692/5 "2020-01-23T17:15:49Z")

</div>

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.
