jax.numpy.arctan2#

jax.numpy.arctan2(x1, x2, /)[源代码]#

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

numpy.arctan2() 的 JAX 实现

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

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

返回:

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

返回类型:

数组

另请参阅

示例

考虑一个在 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\) (包括)之间的值。