# Understand how to pass the correct input(s), e.g. for atan (atan2)

**URL:** https://discourse.julialang.org/t/understand-how-to-pass-the-correct-input-s-e-g-for-atan-atan2/33410
**Category:** New to Julia
**Tags:** question
**Created:** [January 15, 2020, 5:34pm UTC](https://discourse.julialang.org/t/understand-how-to-pass-the-correct-input-s-e-g-for-atan-atan2/33410 "2020-01-15T17:34:39Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Pablo\_J\_Gonzalez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablo_j_gonzalez/32/12321_2.png) [@Pablo\_J\_Gonzalez](https://discourse.julialang.org/u/Pablo_J_Gonzalez)
#### Post date: [January 15, 2020, 5:34pm UTC](https://discourse.julialang.org/t/understand-how-to-pass-the-correct-input-s-e-g-for-atan-atan2/33410/1 "2020-01-15T17:34:39Z")

</div>

Hi there, I am newbie to Julia.

I am learning Julia, and so I want to create a simple function to convert a set of coordinates from cartesian to polar, and backwards. This is a trivial task, but I am stuck on the correct syntaxis for the `atan` function to be general to accept a value, and an array.

I run easily `atan` with two float numbers (e.g., `atan(2.,4.)`). However, I want to be able to pass also a vector (Array{Float64,1}), matrix of complex numbers, etc. It could be broadcasting but I do not get the right syntax.

```julia
function cart2pol(x,y,z)
  theta = atan(y,x)
  rho = sqrt(x*x + y*y)
  return theta, rho, z
end

x = [1,2.1213,0,-5]
y = [0,2.1213,4,0]
z = [7.,8,9,10]

theta,rho,z = cart2pol(x,y,z)

```

However, I get the following error.

```julia
MethodError: no method matching atan(::Array{Float64,1}, ::Array{Float64,1})

Stacktrace:
 [1] cart2pol(::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}) at .\In[24]:4
 [2] top-level scope at In[26]:1

```

Any help will be appreaciated (solution or point to the right section in the manual to understand it)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 15, 2020, 5:51pm UTC](https://discourse.julialang.org/t/understand-how-to-pass-the-correct-input-s-e-g-for-atan-atan2/33410/2 "2020-01-15T17:51:09Z")

</div>

You want `cart2pol.(x,y,z)`. See [this section](https://docs.julialang.org/en/latest/manual/mathematical-operations/#man-dot-operators-1) of the manual.

However, note that `pol = cart2pol.(x,y,z)` returns an array of tuples, not a tuple of arrays. (This is usually a better data structure for working with coordinate vectors anyway because it will have better locality for most operations that you want to do with the coordinates. [StaticArrays](https://github.com/JuliaArrays/StaticArrays.jl) may be an even nicer option than tuples.)

(In general, if you are coming from another language, I would re-think a focus on vectorized operations involving multiple passes doing simple operations on arrays of numbers. You can do this in Julia, too, but there are [often better approaches](https://julialang.org/blog/2017/01/moredots/) since performance in Julia does not impose vectorization on you.)

You could also do `atan.(x,y)` and `hypot.(x,y)` in your `cart2pol` function, which would allow you to return a tuple of arrays as desired in your original code, but this is sticking with a suboptimal vectorized style.

PS. I would also recommend using `hypot(x, y)` instead of `sqrt(x*x + y*y)`, as the latter can suffer from overflow or underflow for very large or small `x` and `y`, e.g. `x=1e200`.

PPS. In Julia you can have variables `θ = atan(y,x)` and `ρ = hypot(x,y)`, you know. Try typing `\theta` followed by the tab character at the interactive prompt.

---

<div class="post-metadata">

### Author: ![Pablo\_J\_Gonzalez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablo_j_gonzalez/32/12321_2.png) [@Pablo\_J\_Gonzalez](https://discourse.julialang.org/u/Pablo_J_Gonzalez)
#### Post date: [January 15, 2020, 8:33pm UTC](https://discourse.julialang.org/t/understand-how-to-pass-the-correct-input-s-e-g-for-atan-atan2/33410/3 "2020-01-15T20:33:15Z")

</div>

Thank you for taking the time to give us a lot of suggestions to learn by doing.
