简体   繁体   English

Python atan 或 atan2,我应该使用什么?

[英]Python atan or atan2, what should I use?

My formula f=arctan(ImZ/ReZ)我的公式 f=arctan(ImZ/ReZ)

There are two options:有两种选择:

Option 1 (atan):选项 1(atan):

ImZ=-4.593172163003
ImR=-4.297336384845

>>> z=y/x
>>> f1=math.atan(z)
>>> f1
0.8186613519278327

Option 2 (atan2)选项 2 (atan2)

>>> f=math.atan2(y,x)
>>> f
-2.3229313016619604

Why are these two results different?为什么这两个结果不同?

Atan takes single argument and Atan2 takes two arguments.The purpose of using two arguments instead of one is to gather information on the signs of the inputs in order to return the appropriate quadrant of the computed angle, which is not possible for the single-argument Atan Atan 采用单个参数,Atan2 采用两个参数。使用两个参数而不是一个参数的目的是收集有关输入符号的信息,以便返回计算出的角度的适当象限,这对于单参数是不可能的晒黑

每个 x,y 的 atan2 结果

Atan2 result is always between -pi and pi. Atan2 结果总是在 -pi 和 pi 之间。

Reference: https://en.wikipedia.org/wiki/Atan2参考: https : //en.wikipedia.org/wiki/Atan2

docstring for math.atan: math.atan 的文档字符串:

atan(x) Return the arc tangent (measured in radians) of x. atan(x) 返回 x 的反正切(以弧度为单位)。

docstring for math.atan2: math.atan2 的文档字符串:

atan2(y, x) Return the arc tangent (measured in radians) of y/x. atan2(y, x) 返回 y/x 的反正切(以弧度为单位)。 Unlike atan(y/x), the signs of both x and y are considered.与 atan(y/x) 不同,x 和 y 的符号都被考虑。

To be very complete, here's what the doc says about atan2:为了非常完整,以下是文档对 atan2 的说明:

math.atan2(y, x) Return atan(y / x), in radians. math.atan2(y, x) 以弧度为单位返回 atan(y / x)。 The result is between -pi and pi.结果介于 -pi 和 pi 之间。 The vector in the plane from the origin to point (x, y) makes this angle with the positive X axis.平面中从原点到点 (x, y) 的向量与正 X 轴形成这个角度。 The point of atan2() is that the signs of both inputs are known to it, so it can compute the correct quadrant for the angle. atan2() 的要点是它知道两个输入的符号,因此它可以计算角度的正确象限。 For example, atan(1) and atan2(1, 1) are both pi/4, but atan2(-1, -1) is -3*pi/4.例如,atan(1) 和 atan2(1, 1) 都是 pi/4,但 atan2(-1, -1) 是 -3*pi/4。

So it's pretty clear: the outputs are different because of the signs of ImZ and ImR .所以,这是很明确的:由于输出的标志是不同的ImZImR atan2 returns the appropriate quadrant, unlike atan .atan不同, atan2返回适当的象限。

As others said, the atan2 takes two arguments to be able to determine the quadrant of the output angle properly...正如其他人所说,atan2 需要两个参数才能正确确定输出角度的象限......

However, it still outputs an angle between [-pi,pi] , which is not always useful (positive [0,pi] for 1st and 2nd quadrants; and negative [-pi,0] for 3rd and 4th).但是,它仍然输出[-pi,pi]之间的角度,这并不总是有用(第一和第二象限为正[0,pi] ;第三和第四象限为负[-pi,0] )。

It's possible to define an atan function that returns an angle in [0,2pi] , as theodore panagos showed.正如 theodore panagos 所示,可以定义一个返回[0,2pi]的角度的atan函数。

Improving on theodore panagos' answer, here is a python version using numpy改进 theodore panagos 的回答,这是一个使用 numpy 的 python 版本

import numpy

# defining the atan function
myatan = lambda x,y: numpy.pi*(1.0-0.5*(1+numpy.sign(x))*(1-numpy.sign(y**2))\
         -0.25*(2+numpy.sign(x))*numpy.sign(y))\
         -numpy.sign(x*y)*numpy.arctan((numpy.abs(x)-numpy.abs(y))/(numpy.abs(x)+numpy.abs(y)))

#testing
u = numpy.array([[numpy.sqrt(3.0)/2.0,0.5],   # expected: 30
                 [0.5,numpy.sqrt(3.0)/2.0],   # expected: 60
                 [0.0,1.0],                   # expected: 90
                 [-0.5,numpy.sqrt(3.0)/2.0],  # expected: 120
                 [-numpy.sqrt(3.0)/2.0,0.5],  # expected: 150
                 [-1.0,0.0],                  # expected: 180
                 [-numpy.sqrt(3.0)/2.0,-0.5], # expected: 210
                 [-0.5,-numpy.sqrt(3.0)/2.0], # expected: 240
                 [0.0,-1.0],                  # expected: 270
                 [0.5,-numpy.sqrt(3.0)/2.0],  # expected: 300
                 [numpy.sqrt(3.0)/2.0,-0.5],  # expected: 330
                 [1.0,0.0]])                  # expected: 0 or 360
theta = myatan(u[:,0],u[:,1])
print(theta * 180.0/numpy.pi) # converting to degrees

output:输出:

[ 30.  60.  90. 120. 150. 180. 210. 240. 270. 300. 330.   0.]

it does not output 360 exactly, but it goes through up to it, then it cycles, as expected它不会准确地输出 360,但它会一直到它,然后按预期循环

A formula to have an angle counter clockwise from 0,that is the positive axis of x,一个公式从 0 逆时针方向有一个角度,即 x 的正轴,

to 2pi for any value of x and y.到 2pi 对于任何 x 和 y 值。 For x=y=0 the result is undefined.对于 x=y=0,结果未定义。

f(x,y)=pi-pi/2*(1+sign(x))*(1-sign(y^2))-pi/4*(2+sign(x))*sign(y)
       -sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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