简体   繁体   中英

numpy arctan2 bug or usage issues?

I'm translating some functions from an Excel spreadsheet into python. I need to use atan2 which is arctan2 according to numpy docs: arctan2 . The problem is that the two results are not even close:

oc = 23.4384863405
sal = 89.7814630647
sra = np.arctan2(np.cos(np.deg2rad(sal)),
         np.cos(np.deg2rad(oc))*np.sin(np.deg2rad(sal)))
results: Excel = 1.566714757 Numpy = 0.00415720646  ??

I trust the Excel results as it is correct. It is numpy that is wrong.

Now either I'm not using arctan2 correctly, or atan2 is not arctan2 in numpy, or there is a bug in numpy, or I'm just completely lost here.

I'm using python version 2.7.2 and numpy 1.6.2

Any ideas, please? Thanks

From the Excel docs :

The syntax for the ATAN2 function is:

ATAN2( x-coordinate, y-coordinate )

From the numpy docs :

numpy.arctan2(x1, x2[, out])

Element-wise arc tangent of x1/x2 choosing the quadrant correctly.

The quadrant (ie, branch) is chosen so that arctan2(x1, x2) is the signed angle in radians between the ray ending at the origin and passing through the point (1,0), and the ray ending at the origin and passing through the point (x2, x1). (Note the role reversal: the “y-coordinate” is the first function parameter, the “x-coordinate” is the second.)

They take their arguments in opposite order. Thus:

In [31]: arctan2(cos(deg2rad(sal)), cos(deg2rad(oc))*sin(deg2rad(sal)))
Out[31]: 0.0041572064598812417

In [32]: arctan2(cos(deg2rad(oc))*sin(deg2rad(sal)), cos(deg2rad(sal)))
Out[32]: 1.5666391203350154

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM