jax.numpy.arctan2#
- jax.numpy.arctan2(x1, x2, /)[原始碼]#
計算 x1/x2 的反正切,並選擇正確的象限。
numpy.arctan2()
的 JAX 實作- 參數:
x1 (ArrayLike) – 分子陣列。
x2 (ArrayLike) – 分母陣列;應與 x1 廣播相容。
- 回傳:
x1 / x2 的逐元素反正切,追蹤正確的象限。
- 回傳型別:
參見
jax.numpy.tan()
:計算角度的正切jax.numpy.atan2()
:此函式的陣列 API 版本。
範例
考慮介於 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\) (含) 之間的值。