# [Numpy-discussion] arctan2 with complex args

David Goldsmith David.L.Goldsmith@noaa....
Sun Apr 29 22:38:31 CDT 2007

```Far be it from me to challenge the mighty Wolfram, but I'm not sure that
using the *formula* for calculating the arctan of a *single* complex
argument from its real and imaginary parts makes any sense if x and/or y
are themselves complex (in particular, does Lim(formula), as the
imaginary part of complex x and/or y approaches zero, approach
arctan2(realpart(x), realpart(y)?) - without going to the trouble to
determine it one way or another, I'd be surprised if "their"
continuation of the arctan2 function from RxR to CxC is (a. e.)
continuous (not that I know for sure that "mine" is...).

DG

lorenzo bolla wrote:
> You make your point, but I would expect a behaviour similar to
> Mathematica or Matlab.
>
> From http://documents.wolfram.com/mathematica/functions/ArcTan
> <http://documents.wolfram.com/mathematica/functions/ArcTan>
> "If x or y is complex, then ArcTan[x , y] gives . When , ArcTan[x, y]
> gives the number such that and ."
>
> Lorenzo.
>
> On 4/29/07, *David Goldsmith* < David.L.Goldsmith@noaa.gov
> <mailto:David.L.Goldsmith@noaa.gov>> wrote:
>
>     I'll take a stab at this one; if I miss the mark, people, please
>     chime in.
>
>     What's "strange" here is not numpy's behavior but octave's (IMO).
>     Remember that, over R, arctan is used in two different ways: one is
>     simply as a map from (-inf, inf) -> (-pi/2,pi/2) - here, let's
>     call that
>     invtan; the other is as a means to determine "the angle"
>     (conventionally
>     taken to be between -pi and pi) of a point in the plane - but
>     since, for
>     example, tan(pi/4) = tan(-3pi/4) (and in general tan(x) =
>     tan(x-pi)) to
>     uniquely determine said angle, we need to keep track of and take into
>     account the quadrant in which the point lies; this is (the only
>     reason)
>     why arctan2 is a function of two arguments, one representing the
>     abscissa, the other the ordinate of the point.  But when the
>     argument is
>     complex (arctan2, as the inverse of the tangent function, *is* a valid
>     function on C), this geometric use no longer makes sense, so there's
>     really no reason to implement arctan2(z,w), z, w complex.  If for
>     some
>     reason, e.g., uniformity of algorithmic expression - I don't see any
>     (simple) way to preserve uniformity of code expression - as near as I
>     can tell, you're going to have to implement an if/else if you need to
>     allow for the invtan of two complex arguments - you need to handle
>     arctan2(z,w), implement it as arctan(w/z):
>
>     >>> import numpy
>     >>> numpy.arctan(1j/1j)
>     (0.78539816339744828+0j)
>
>     DG
>
>     lorenzo bolla wrote:
>     > Weird behaviour with arctan2(complex,complex).
>     > Take  a look at this:
>     >
>     > In [11]: numpy.arctan2(1.,1.)
>     > Out[11]: 0.785398163397
>     >
>     > In [12]: numpy.arctan2 (1j,1j)
>     >
>     ---------------------------------------------------------------------------
>     > exceptions.AttributeError                            Traceback (most
>     > recent call last)
>     >
>     > AttributeError: 'complex' object has no attribute 'arctan2'
>     >
>     > same error for:
>     >
>     > In [13]: numpy.arctan2(1j,1.)
>     > In [14]: numpy.arctan2(1.,1j)
>     >
>     > But arctan2 is defined for complex arguments, as far as Octave
>     knows :-) :
>     >
>     > octave:7> atan2(1,1)
>     > ans = 0.78540
>     > octave:8> atan2(1j,1j)
>     > ans = 0
>     > octave:9> atan2(1j,1)
>     > ans = 0
>     > octave:10> atan2(1,1j)
>     > ans = 1.5708
>     >
>     > bug or wanted behaviour?
>     > Lorenzo.
>     >
>     ------------------------------------------------------------------------
>     >
>     > _______________________________________________
>     > Numpy-discussion mailing list
>     > Numpy-discussion@scipy.org <mailto:Numpy-discussion@scipy.org>
>     > http://projects.scipy.org/mailman/listinfo/numpy-discussion
>     >
>
>     _______________________________________________
>     Numpy-discussion mailing list
>     Numpy-discussion@scipy.org <mailto:Numpy-discussion@scipy.org>
>     http://projects.scipy.org/mailman/listinfo/numpy-discussion
>     <http://projects.scipy.org/mailman/listinfo/numpy-discussion>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>

```