fn

arctan2

Tensor
arctan2(y: Tensor, x: Tensor)
source

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

yTensor
Ordinate (imaginary / y-coordinate). Must broadcast with x.
xTensor
Abscissa (real / x-coordinate). Must broadcast with y.

Returns

Tensor

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

Notes

Mathematical definition:

arctan2(y,x)={arctan(y/x),x>0,arctan(y/x)+π,x<0, y0,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.3562, -2.3562, -0.7854])