4
$\begingroup$

I have a coordinate system generated by :

coord = Table[{x, y}, {x, 5}, {y, 5}]

I would like to calculate ArcTan(y/x) and apply to coord:

ArcTan[#2/#1] & /@ coord

I am not sure what's wrong with my function. Could you help me to fix it?

Thanks!

$\endgroup$
2
  • $\begingroup$ Take a look at Apply third argument. $\endgroup$
    – Kuba
    Commented Jan 19, 2018 at 20:49
  • 5
    $\begingroup$ Additionally, have a look at the two-arguments version of ArcTan. $\endgroup$ Commented Jan 19, 2018 at 20:51

5 Answers 5

3
$\begingroup$

Noone's addressed the actual question yet. There are three things wrong with your approach:

  1. You are expecting an output {{x1,y1},{x2,y2}.... {x25,y25}} but when you use Table with multiple iterators, it creates a nested list:

    {
        {
            {1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}
        }, 
        {
            {2, 1}, {2, 2}, {2, 3}, {2, 4}, {2, 5}
        }, 
        {   
            {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 5}
        }, 
        {
            {4, 1}, {4, 2}, {4, 3}, {4, 4}, {4, 5}
        }, 
        {
            {5, 1}, {5, 2}, {5, 3}, {5, 4}, {5, 5}
        }
    }
    
  2. Even if the list were in the form you expected, you are doing:

    {ArcTan[    (#2/#1)& [{xn,yn}]    ]}
    

    Can you see what's wrong with that? As the error message suggests, you cannot fill slot #2 when you only have a single argument. I know what you intended to do, and for this you need to use Apply to replace the head of List[xn,yn] so that it becomes (#2/#1)&[xn,yn].

  3. $(-x,-y)$ will be assigned the same result as $(x,y)$. The $\arctan$ function of one argument only has a unique range of $\pi$. This is a common issue (especially programming), and so sometimes we talk about an $\arctan$ function with two arguments $\arctan(x,y)$ which can give proper results for all values of $x,y$. In Mathematica, we can use this with ArcTan[x,y].


Solution:

coord = Table[{x, y}, {x, 5}, {y, 5}];
coord = Flatten[coord,1]; (* In general, replace 1 with the number of iterators minus 1. i.e. use a 2 for 3D coordinates*)
ArcTan@@@coord

Output:

{\[Pi]/4, ArcTan[2], ArcTan[3], ArcTan[4], ArcTan[5], ArcTan[1/2], \[Pi]/4, ArcTan[3/2], ArcTan[2], ArcTan[5/2], ArcTan[1/3], ArcTan[2/3], \[Pi]/4, ArcTan[4/3], ArcTan[5/3], ArcTan[1/4], ArcTan[1/2], ArcTan[3/4], \[Pi]/4, ArcTan[5/4], ArcTan[1/5], ArcTan[2/5], ArcTan[3/5], ArcTan[4/5], \[Pi]/4}

Update per comment:

If you want to preserve the nested structure of the coordinates, then you can use Apply and specify the level. For the nested list above, we use a level of 2 because the the coordinate lists {x,y} are contained within a list within a list (i.e. are at level 2).

Apply[ArcTan,coord,{2}]

Output:
{{\[Pi]/4, ArcTan[2], ArcTan[3], ArcTan[4], ArcTan[5]}, {ArcTan[1/2], \[Pi]/4, ArcTan[3/2], ArcTan[2], ArcTan[5/2]}, {ArcTan[1/3], ArcTan[2/3], \[Pi]/4, ArcTan[4/3], ArcTan[5/3]}, {ArcTan[1/4], ArcTan[1/2], ArcTan[3/4], \[Pi]/4, ArcTan[5/4]}, {ArcTan[1/5], ArcTan[2/5], ArcTan[3/5], ArcTan[4/5], \[Pi]/4}}
$\endgroup$
5
  • $\begingroup$ wow, you're the one that fully address my code issue in a clear and logical way. Appreciate your answer very much. But your code will reduce one dimension in the result but still I get your idea! $\endgroup$
    – cj9435042
    Commented Jan 20, 2018 at 12:49
  • $\begingroup$ @cj9435042 - I don't know what you mean. $\endgroup$
    – Myridium
    Commented Jan 20, 2018 at 12:51
  • $\begingroup$ I think your code should has something like : Partition[ArcTan@@@coord,5] in this case to make the dimension exactly. Sorry for not mentioning this clearly in my description! $\endgroup$
    – cj9435042
    Commented Jan 20, 2018 at 13:00
  • $\begingroup$ @cj9435042 - okay, see my edit. Although my answer is now basically the same as Carl's. $\endgroup$
    – Myridium
    Commented Jan 20, 2018 at 13:05
  • $\begingroup$ Thank you for the updating! $\endgroup$
    – cj9435042
    Commented Jan 20, 2018 at 13:05
7
$\begingroup$

Using the 2-arg version of ArcTan (as suggested by @SjoerdCdeVries):

Apply[ArcTan, coord, {2}]

{{π/4, ArcTan[2], ArcTan[3], ArcTan[4], ArcTan[5]}, {ArcTan[1/2], π/4, ArcTan[3/2], ArcTan[2], ArcTan[5/2]}, {ArcTan[1/3], ArcTan[2/3], π/4, ArcTan[4/3], ArcTan[5/3]}, {ArcTan[1/4], ArcTan[1/2], ArcTan[3/4], π/4, ArcTan[5/4]}, {ArcTan[1/5], ArcTan[2/5], ArcTan[3/5], ArcTan[4/5], π/4}}

Or, you can tweak @Coolwater's fine answer:

coordNew = coord;
coordNew[[All, All, 0]] = ArcTan;
coordNew

{{π/4, ArcTan[2], ArcTan[3], ArcTan[4], ArcTan[5]}, {ArcTan[1/2], π/4, ArcTan[3/2], ArcTan[2], ArcTan[5/2]}, {ArcTan[1/3], ArcTan[2/3], π/4, ArcTan[4/3], ArcTan[5/3]}, {ArcTan[1/4], ArcTan[1/2], ArcTan[3/4], π/4, ArcTan[5/4]}, {ArcTan[1/5], ArcTan[2/5], ArcTan[3/5], ArcTan[4/5], π/4}}

$\endgroup$
1
  • $\begingroup$ Also ArcTan @@@ # & /@ coord $\endgroup$
    – Bob Hanlon
    Commented Jan 20, 2018 at 1:25
6
$\begingroup$

Other ways:

ArcTan[coord[[All, All, 2]]/coord[[All, All, 1]]]

(* Or *)

coordNew = coord;
coordNew[[All, All, 0]] = ArcTan[#2/#] &
coordNew 
$\endgroup$
1
  • 1
    $\begingroup$ Or even simpler is coordNew[[All, All, 0]] = ArcTan. $\endgroup$
    – Carl Woll
    Commented Jan 19, 2018 at 21:02
3
$\begingroup$
ArcTan[#2/#1] & @@@ # & /@ coord

or

 ArcTan[#2/#1] & @@@ Transpose /@ coord

or

Map[ArcTan[#[[2]]/#[[1]]] &, coord, {-2}]

{{Pi /4, ArcTan[2], ArcTan[3], ArcTan[4], ArcTan[5]},
{ArcTan[1/2], Pi/4, ArcTan[3/2], ArcTan[2], ArcTan[5/2]},
{ArcTan[1/3], ArcTan[2/3], Pi/4, ArcTan[4/3], ArcTan[5/3]},
{ArcTan[1/4], ArcTan[1/2], ArcTan[3/4], Pi /4, ArcTan[5/4]},
{ArcTan[1/5], ArcTan[2/5], ArcTan[3/5], ArcTan[4/5], Pi/4}}

$\endgroup$
0
$\begingroup$

You can also use pattern matching

coord /. {x_, y_} :> ArcTan[y/x]

Edit: I just realised this would fail if the list had just two pairs, in which case x_ would match the first pair and y_ the second.

But you could do something like this:

Table[Pair[x, y], {x, 5}, {y, 5}] /. Pair[x_, y_] :> ArcTan[y/x]

[btw, are you sure you mean ArcTan[y/x], as opposed to ArcTan[y,x]? The latter takes into account the quadrant of (x, y)]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.