# Is atan2 in the base?

is atan2 in the base? or is part of another package?

3 Likes

Are you looking for two-argument atan? From the docs:

  atan(y)
atan(y, x)

Compute the inverse tangent of y or y/x, respectively.

For one argument, this is the angle in radians between the positive
x-axis and the point (1, y), returning a value in the interval
[-\pi/2, \pi/2].

For two arguments, this is the angle in radians between the positive
x-axis and the point (x, y), returning a value in the interval [-\pi, \pi].
This corresponds to a standard
atan2 (https://en.wikipedia.org/wiki/Atan2) function.

5 Likes

Yes, I am aware of it. I am trying to get the angle of each element for a complex matrix (1024x1024). The results are bound to exceed the -pi/2 to pi/2 provided by atan. Atan2 should provide the -pi to pi range for the calculations I am performing.

I tried the atan(x,y) that comes with the base of Julia, but it cannot solve matrices. I get this error:
RROR: MethodError: no method matching atan(::Array{Float64,2}, ::Array{Float64,2})
Closest candidates are:
Stacktrace:
[1] top-level scope at none:0

Posted the question in Github, and I was told that I should use atan2.(y,x). In this case they made the point that it should have a period at the end of â€śatan2.â€ť. Also, they sent me here because this was a question of usage, not an issue.

I tried atan2(y,x) with single elements, or atan2.(y,x) with matrices and I get:
ERROR: UndefVarError: atan2 not defined
Stacktrace:
[1] top-level scope at none:0

So I am to assume that I donâ€™t have the correct package for atan2; I was misdirected; or there is problem with my version of Julia.

Thanks.

Is angle.(x) what youâ€™re looking for?

2 Likes

Yes, that works. Thank you.

Is there some documentation where it is explained when to use the â€ś.â€ť?
atan doesnâ€™t need it to work on matrices, and some of the comments online were talking of â€śatan2â€ť and â€śatan2.â€ť which donâ€™t work.

Any way, Thank you again.

Thanks for the â€śangle.â€ť one too, that works even better.

Edit: this is even better Functions Â· The Julia Language

Short answer is that the dot â€śbroadcastsâ€ť any function across elements of the arguments, kind of like auto-vectorizing it. Note btw that atan(::Matrix) is not computing element-wise arc tangent. If you type ?atan to get the docs youâ€™ll see it explained that its doing the â€śthe inverse matrix tangent of a square matrix A.â€ť

1 Like

Shorter answer is that the . applies functions â€śpointwiseâ€ť or elementwise. Instead of acting on a matrix as a whole, it acts on each individual element. Thereâ€™s more to broadcast, but with 1-argument functions thatâ€™s it.

A great example is this square function:

julia> square(x) = x*x
square (generic function with 1 method)

julia> square([1 2; 3 4])
2Ă—2 Array{Int64,2}:
7  10
15  22

julia> square.([1 2; 3 4])
2Ă—2 Array{Int64,2}:
1   4
9  16


If you call square(A) directly with a matrix, itâ€™ll compute the matrix-matrix product. If you call square.(A), then itâ€™ll compute the squares of each element individually.

4 Likes

Great. Thanks. Iâ€™ll have to try all of those, and need to read more.