# Is atan2 in the base?

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

1 Like

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.

4 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:
 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:
 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 https://docs.julialang.org/en/v1/manual/functions/#man-vectorized-1

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.