Skip to content
fn

arctan2

→Tensor
arctan2(input: Tensor, other: Tensor | Scalar)

Quadrant-correct two-argument arctangent.

Verbose alias of lucid.atan2. Returns the polar angle θ\theta of the point (x,y)(x, y) in the Cartesian plane, resolving the correct quadrant from the signs of both arguments.

Parameters

inputTensor
Ordinate (imaginary / y-coordinate). Must broadcast with other.
otherTensor | Scalar
Abscissa (real / x-coordinate). Must broadcast with input.

Returns

Tensor

Element-wise angle in radians on [−π,π][-\pi, \pi], with the broadcast shape of input and other.

Notes

Mathematical definition:

arctan⁡2(y,x)={arctan⁡(y/x),x>0,arctan⁡(y/x)+π,x<0, y≥0,arctan⁡(y/x)−π,x<0, y<0,+π/2,x=0, y>0,−π/2,x=0, y<0,0,x=0, y=0.\arctan2(y, x) = \begin{cases} \arctan(y/x), & x > 0, \\ \arctan(y/x) + \pi, & x < 0,\ y \geq 0, \\ \arctan(y/x) - \pi, & x < 0,\ y < 0, \\ +\pi/2, & x = 0,\ y > 0, \\ -\pi/2, & x = 0,\ y < 0, \\ 0, & x = 0,\ y = 0. \end{cases}

The naive arctan⁡(y/x)\arctan(y / x) collapses both half-planes into (−π/2,π/2)(-\pi/2, \pi/2); arctan2 instead inspects the sign of each argument to recover the full [−π,π][-\pi, \pi] range, which is essential for converting Cartesian to polar coordinates.

Examples

>>> import lucid
>>> y = lucid.tensor([1.0,  1.0, -1.0, -1.0])
>>> x = lucid.tensor([1.0, -1.0, -1.0,  1.0])
>>> lucid.arctan2(y, x)
tensor([0.7854, 2.356, -2.356, -0.7854])