YouTip LogoYouTip

Pytorch Torch Atan2

## PyTorch torch.atan2 The `torch.atan2` function in PyTorch computes the element-wise, two-argument arctangent (inverse tangent) of $y / x$. It calculates the angle (in radians) between the positive x-axis and the point $(x, y)$, returning values in the range $[-\pi, \pi]$. Unlike the standard single-argument arctangent (`torch.atan`), which cannot distinguish between opposite quadrants (e.g., Quadrant I vs. Quadrant III), `torch.atan2` uses the signs of both the $y$ (numerator) and $x$ (denominator) coordinates to determine the correct quadrant of the resulting angle. --- ### Function Signature ```python torch.atan2(input, other, *, out=None) -> Tensor ``` ### Parameter Descriptions * **`input` (Tensor)**: The dividend tensor representing the $y$-coordinates (numerator). * **`other` (Tensor)**: The divisor tensor representing the $x$-coordinates (denominator). It must be broadcastable with the shape of `input`. * **`out` (Tensor, optional)**: The output destination tensor. ### Return Value * Returns a new tensor containing the computed angles in radians, with values in the range $[-\pi, \pi]$. * The data type of the returned tensor matches the promoted data type of `input` and `other`. --- ## Code Examples ### Basic Usage The following example demonstrates how `torch.atan2` calculates angles for points located on the axes and in different quadrants. ```python import torch import math # Create tensors representing y-coordinates (input) and x-coordinates (other) y = torch.tensor([0.0, 1.0, -1.0, 1.0]) x = torch.tensor([1.0, 0.0, 0.0, -1.0]) # Compute the 2-argument arctangent result = torch.atan2(y, x) print("Result (in radians):") print(result) print("\nResult (in degrees):") print(torch.rad2deg(result)) ``` **Output:** ```text Result (in radians): tensor([ 0.0000, 1.5708, -1.5708, 2.3562]) Result (in degrees): tensor([ 0., 90., -90., 135.]) ``` ### Broadcasting Support `torch.atan2` supports PyTorch's broadcasting semantics. This means the input tensors do not need to have the exact same shape, as long as they are compatible for broadcasting. ```python import torch # y-coordinates: 2D tensor of shape (2, 3) y = torch.tensor([[1.0, 1.0, 1.0], [-1.0, -1.0, -1.0]]) # x-coordinates: 1D tensor of shape (3,) which will be broadcasted to (2, 3) x = torch.tensor([1.0, 0.0, -1.0]) result = torch.atan2(y, x) print(result) ``` **Output:** ```text tensor([[ 0.7854, 1.5708, 2.3562], [-0.7854, -1.5708, -2.3562]]) ``` --- ## Key Considerations ### 1. Quadrant Mapping Reference The sign of both inputs determines the quadrant of the output angle: | $y$ (`input`) | $x$ (`other`) | Quadrant | Output Range (Radians) | Output Range (Degrees) | | :--- | :--- | :--- | :--- | :--- | | Positive ($>0$) | Positive ($>0$) | Quadrant I | $(0, \pi/2)$ | $(0^\circ, 90^\circ)$ | | Positive ($>0$) | Negative ($<0$) | Quadrant II | $(\pi/2, \pi)$ | $(90^\circ, 180^\circ)$ | | Negative ($<0$) | Negative ($<0$) | Quadrant III | $(-\pi, -\pi/2)$ | $(-180^\circ, -90^\circ)$ | | Negative ($<0$) | Positive ($>0$) | Quadrant IV | $(-\pi/2, 0)$ | $(-90^\circ, 0^\circ)$ | ### 2. Handling Zero Values * If both $y$ and $x$ are zero, the result depends on the sign of the zeros (e.g., $+0.0$ vs $-0.0$ in IEEE 754 floating-point standard). Typically, `torch.atan2(0.0, 0.0)` returns `0.0`. * If $x$ is positive and $y$ is `0.0`, the result is `0.0`. * If $x$ is negative and $y$ is `0.0`, the result is $\pi$ ($3.1416$).
← Pytorch Torch Atleast_3DPytorch Torch Asarray β†’