I'm trying to port some Octave code to Java. I'm trying out atan2 so I have the following tries
octave3.2:53> m1 = [1,2;3,4]
m1 =
1 2
3 4
octave3.2:54> m2=[5,6;7,8]
m2 =
5 6
7 8
octave3.2:55> atan2(m1,m2)
ans =
0.19740 0.32175
0.40489 0.46365
octave3.2:56> atan(1/5)
ans = 0.19740
octave3.2:57> atan(2/6)
ans = 0.32175
octave3.2:58> atan(3/7)
ans = 0.40489
octave3.2:59> atan(4/8)
ans = 0.46365
So it seems to me that atan2 applies atan cell-wise, to the quotient of the first argument divided by the second argument. I got this well in Java. However, the following piece of code gives off something weird...
y
x
theta = atan2(y,x)
For which I get a trace something like,
y =
-0.50000 -0.50000 -0.50000 -0.50000 -0.50000 -0.50000 -0.50000 -0.50000 -0.50000 -0.50000
-0.40000 -0.40000 -0.40000 -0.40000 -0.40000 -0.40000 -0.40000 -0.40000 -0.40000 -0.40000
-0.30000 -0.30000 -0.30000 -0.30000 -0.30000 -0.30000 -0.30000 -0.30000 -0.30000 -0.30000
-0.20000 -0.20000 -0.20000 -0.20000 -0.20000 -0.20000 -0.20000 -0.20000 -0.20000 -0.20000
-0.10000 -0.10000 -0.10000 -0.10000 -0.10000 -0.10000 -0.10000 -0.10000 -0.10000 -0.10000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000
0.20000 0.20000 0.20000 0.20000 0.20000 0.20000 0.20000 0.20000 0.20000 0.20000
0.30000 0.30000 0.30000 0.30000 0.30000 0.30000 0.30000 0.30000 0.30000 0.30000
0.40000 0.40000 0.40000 0.40000 0.40000 0.40000 0.40000 0.40000 0.40000 0.40000
x =
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
-0.50000 -0.40000 -0.30000 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
theta =
-2.35619 -2.24554 -2.11122 -1.95130 -1.76819 -1.57080 -1.37340 -1.19029 -1.03038 -0.89606
-2.46685 -2.35619 -2.21430 -2.03444 -1.81577 -1.57080 -1.32582 -1.10715 -0.92730 -0.78540
-2.60117 -2.49809 -2.35619 -2.15880 -1.89255 -1.57080 -1.24905 -0.98279 -0.78540 -0.64350
-2.76109 -2.67795 -2.55359 -2.35619 -2.03444 -1.57080 -1.10715 -0.78540 -0.58800 -0.46365
-2.94420 -2.89661 -2.81984 -2.67795 -2.35619 -1.57080 -0.78540 -0.46365 -0.32175 -0.24498
3.14159 3.14159 3.14159 3.14159 3.14159 0.00000 0.00000 0.00000 0.00000 0.00000
2.94420 2.89661 2.81984 2.67795 2.35619 1.57080 0.78540 0.46365 0.32175 0.24498
2.76109 2.67795 2.55359 2.35619 2.03444 1.57080 1.10715 0.78540 0.58800 0.46365
2.60117 2.49809 2.35619 2.15880 1.89255 1.57080 1.24905 0.98279 0.78540 0.64350
2.46685 2.35619 2.21430 2.03444 1.81577 1.57080 1.32582 1.10715 0.92730 0.78540
What's with that? I think the first cell of theta should ~0.785398. The other cells seem wrong too. Is there anything I missed?
Found the answer on my own. atan2
isn't simply atan
applied cell-wise. The sign of the operands matter as well.
For more info see: http://sunsite.univie.ac.at/textbooks/octave/octave_17.html
And then for how to implement atan2
by yourself (as I'm doing) see: http://en.wikipedia.org/wiki/Atan2