jax.numpy.arctan2#

jax.numpy.arctan2(x1, x2, /)[source]#

计算 x1/x2 的反正切,选择正确的象限。

numpy.arctan2() 的 JAX 实现

参数:
  • x1 (ArrayLike) – 分子数组。

  • x2 (ArrayLike) – 分母数组;应与 x1 广播兼容。

返回:

x1 / x2 的逐元素反正切,跟踪正确的象限。

返回类型:

Array

另请参阅

示例

考虑一个弧度角序列,介于 0 和 \(2\pi\) 之间

>>> theta = jnp.linspace(-jnp.pi, jnp.pi, 9)
>>> with jnp.printoptions(precision=2, suppress=True):
...   print(theta)
[-3.14 -2.36 -1.57 -0.79  0.    0.79  1.57  2.36  3.14]

这些角可以等效地用单位圆上的 (x, y) 坐标表示

>>> x, y = jnp.cos(theta), jnp.sin(theta)

为了重建输入角,我们可能会尝试使用恒等式 \(\tan(\theta) = y / x\),并计算 \(\theta = \tan^{-1}(y/x)\)。不幸的是,这无法恢复输入角

>>> with jnp.printoptions(precision=2, suppress=True):
...   print(jnp.arctan(y / x))
[-0.    0.79  1.57 -0.79  0.    0.79  1.57 -0.79  0.  ]

问题在于 \(y/x\) 包含一些歧义:尽管 \((y, x) = (-1, -1)\)\((y, x) = (1, 1)\) 在笛卡尔空间中表示不同的点,但在两种情况下 \(y / x = 1\),因此简单的反正切方法会丢失有关角所在的象限的信息。arctan2() 旨在解决这个问题

>>> with jnp.printoptions(precision=2, suppress=True):
...  print(jnp.arctan2(y, x))
[ 3.14 -2.36 -1.57 -0.79  0.    0.79  1.57  2.36 -3.14]

结果与输入 theta 匹配,除了端点 \(+\pi\)\(-\pi\) 在单位圆上表示无法区分的点。按照惯例,arctan2() 始终返回介于 \(-\pi\)\(+\pi\)(含)之间的值。